C # sort 1 (bubble sort, direct sort, quick sort)

Source: Internet
Author: User

Bubble sort: This is a good understanding of two two, and the code is better written.

Its principle is the adjacent two two comparison, if the previous number is larger than the back, then the exchange, it can be compared to a time when the maximum number of a single, and then loop, each outer loop within the number of cycles less than the original.

      


#region Bubble Sort 2// <summary> 3// bubble sort 4// </summary> 5// <param name= "list" >< /param> 6 //<returns></returns> 7 public list<int> Bubblesort (list<int> List) 8 {9 for (int i = 0; i < list.) Count-1; i++) {one for (int j = 0; j < list. Count-1-I; J + +) ({list[j] > list[j + 1]) (+/- int temp = list[j];16 List[j] = list[j + 1];17 list[j + 1] = temp;18 }19 }20 }21 return list;22 }23 #endregion

The direct sort principle is that the first element and each subsequent element are compared to each other, and if a large one is exchanged, the first element can be the minimum value at a time.

#region Direct sort///<summary>/////</summary>//        <param name= "list" ></ param> public        void Selectionsort (list<int> list)        {for            (int i = 0; i < list. Count; i++)            {for                (int j = i + 1; j < list. Count; J + +)                {                    if (List[i] > List[j])                    {                        int temp = list[i];                        List[i] = list[j];                        LIST[J] = temp;        }}}} #endregion


Quick sort: It seems that the efficiency is the highest, and the class library in C # is sorted by it:

Its principle is: there are two pointers left and right leave the default point to the leftmost element right by default to the rightmost element, and then the pointer to the left element is defined as the standard, the implementation of the standard on the other side of the element is smaller than the standard, the element on the far side of the standard, and then passed the constant standard of building, and implementation sort

Here are some of my understanding:

For example, to sort the array is 643581 of these numbers, yellow for the left pointer; red for right pointer; Blue for left and right coincident

Start: Left points to the first element, right points to the last element

1th time: The left pointer is compared to the left+1, the 6>4,left pointer moves to the right;

2nd time: The left pointer is compared to the left+1, the 6>3,left pointer moves to the right;

3rd time: The left pointer is compared to the left+1, the 6>5,left pointer moves to the right;

4th time: The left pointer is compared to left+1, the 6<8,left pointer is fixed, right moves to the right, and the value of right is interchanged with the value of left+1

5th time: The left pointer is compared to the left+1, the 6>1,left pointer moves to the right;

Found left and right coincident, the implementation of 6 is smaller than 6, 6 is greater than 6. And then redefine

Left= the original left;right=left-1

Left=left+1 and right= The original right followed by the next traversal

Here is the C # code:

#region Quick Sort///<summary>////</summary>//<param name= "list" &GT;&L        t;/param>//<param name= "left" ></param>///<param Name= "right" ></param>                public void QuickSort (List<int> List, int. left, int right) {if (left < right) {                int i = Division (list, left, right);                QuickSort (list, i + 1, right);            QuickSort (list, left, i-1); }}///<summary>///list according to List[left] to the left and right sides///</summary>/<param NA Me= "List" ></param>//<param name= "left" ></param>//<param name= "right" ></p         aram>//<returns></returns> private int Division (list<int> List, int left, int. right)                {while (left < right) {int num = List[left]; if (nuM > list[left + 1]) {List[left] = list[left + 1];                    List[left + 1] = num;                left++;                    } else {int temp = List[right];                    List[right] = list[left + 1];                    List[left + 1] = temp;                right--; } Console.WriteLine (String.            Join (",", list));            } Console.WriteLine ("--------------\ n");        return left; } #endregion

C # sort 1 (bubble sort, direct sort, quick sort)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.