The individual sorting method in C,

Source: Internet
Author: User

The individual sorting method in C,

 

C # the sorting of some algorithms in learning is incomplete. @ ^ _ ^ @

-------- 2016-10-23

 

**************************************** **************************************** *******************

1. Bubble Sorting

Compare adjacent element values, exchange the positions of the two elements when the conditions are met, move the smaller value to the front of the array, and move the larger number to the back of the array, such a small element is like a bubble rising to the front, and a large bubble moving to the back.

 1     class Program 2     { 3         static void Maopao(int[] arr) 4         { 5             bool f = false; 6             do 7             { 8                 f = false; 9                 for (int i = 0; i < arr.Length - 1; i++)10                 {11                     if (arr[i] > arr[i + 1])12                     {13                         int temp = arr[i];14                         arr[i] = arr[i + 1];15                         arr[i + 1] = temp;16                         f = true;17                     }18                 }19             } while (f);20         } 21         static void Main(string[] args)22         {23             int[] list = new int[] { 2, 5, 1, 8, 45, 23, 20, 12 };24             Maopao(list);25             foreach (var item in list)26             {27                 Console.WriteLine(item);28             }29             Console.ReadKey();30         }31     }

 

 1     class Program 2     { 3         static void Maopao(int[] arr) 4         { 5             for (int i = 1; i < arr.Length; i++)// 6             { 7                 for (int j = 0; j < arr.Length-i; j++) 8                 { 9                     if(arr[j] > arr[j+1])10                     {11                         int temp = arr[j];12                         arr[j] = arr[j + 1];13                         arr[j + 1] = temp;14                     }15                 }16             }17         }18         static void Main(string[] args)19         {20             int[] list = new int[] { 2, 5, 1, 8, 45, 23, 20, 12 };21             Maopao(list);22             foreach (var item in list)23             {24                 Console.WriteLine(item);25             }26             Console.ReadKey();27         }28     }

 

2. Select sort

(My understanding) operation 1: Compare the maximum or minimum values of the first digit with the subsequent values, and then exchange the maximum or minimum values with the first digit.

Operation 2: Compare and exchange the second digit with the number behind him. Repeat operation 1.

And so on.

Selection sort is a simple and intuitive sorting algorithm. It works by selecting the smallest (or largest) element from the data elements to be sorted each time, and storing it at the starting position of the sequence until all the data elements to be sorted are arranged. Selecting sorting is an unstable sorting method (for example, switching the first [5] and [3] in sequence [5, 5, 3] for the first time, cause the first 5 to move to the second 5 ).]

 

1 class Program 2 {3 static void SelectSort (int [] arr) 4 {5 int length = arr. length; 6 for (int I = 0; I <length-1; I ++) 7 {8 int temp = arr [I]; // element value 9 int em = I; // index 10 for (int j = I + 1; j <length; j ++) 11 {12 if (arr [j] <temp) 13 {14 temp = arr [j]; 15 arr [j] = arr [em]; 16 arr [em] = temp; 17} 18} 19} 20} 21 static void Main (string [] args) 22 {23 int [] list = new int [] {2, 5, 1, 8, 45, 23, 20, 12}; 24 SelectSort (list ); 25 foreach (var item in list) 26 {27 Console. writeLine (item); 28} 29 Console. readKey (); 30} 31}

 

1 class Program 2 {3 static void SelectSort (int [] arr) 4 {5 int length = arr. length; 6 for (int I = 0; I <length-1; I ++) 7 {8 int min = arr [I]; // element value 9 int em = I; // index 10 for (int j = I + 1; j <length; j ++) 11 {12 if (arr [j] <min) 13 {14 min = arr [j]; 15 em = j; 16} 17} 18 if (em! = I) 19 {20 int temp = arr [I]; 21 arr [I] = arr [em]; 22 arr [em] = temp; 23} 24} 25} 26 static void Main (string [] args) 27 {28 int [] list = new int [] {2, 5, 1, 8, 45, 23, 20, 12}; 29 SelectSort (list); 30 foreach (var item in list) 31 {32 Console. writeLine (item); 33} 34 Console. readKey (); 35} 36}

 

3. Direct sorting

  Direct sortingStarting from the first element, the second number and the number behind it are compared with the number of the front edge for batch sorting.

1 class Program 2 {3 static void DirectSort (int [] arr) 4 {5 int length = arr. length; 6 bool f = false; 7 for (int I = 1; I <length; I ++) 8 {9 int temp = arr [I]; // retain the I position to avoid overwriting 10 f = false; 11 // obtain the I position element, and compare it with all the preceding elements, found that larger than I move 12 for backward (int j = I-1; j> = 0; j --) // forward 13 {14 if (arr [j]> temp) 15 {16 arr [j + 1] = arr [j]; // move backward 17} 18 else19 {20 arr [j + 1] = temp; 21 f = true; 22 break; 23} 24} 25 if (f = false) 26 {27 arr [0] = temp; 28} 29} 30} 31 static void Main (string [] args) 32 {33 int [] list = new int [] {2, 5, 1, 8, 45, 23, 20, 12}; 34 DirectSort (list ); 35 foreach (var item in list) 36 {37 Console. writeLine (item); 38} 39 Console. readKey (); 40} 41}

 

4. Quick sorting

Operation 1: for the number of columns, first consider the first digit as the baseline number A, and place the number smaller than A on the left of A, and the number greater than A on the right;

Operation 2: Repeat the series on the left and right.

And so on.

1 class Program 2 {3 /// <summary> 4 /// sort the number of indexes in array arrea from left to right. 5 /// </summary> 6/ // <param name = "arrea"> array to be sorted </param> 7 // <param name = "left"> Start index of the data to be sorted </param> 8 // <param name = "right"> end index of the data to be sorted </param> 9 static void QuickSort (int [] arrea, int left, int right) 10 {11 if (left <right) // sort the data from left to right by 12 {13 // obtain a reference number first, put something smaller than him or equal to him on the left side of it, and then put something bigger than him on the right side of it 14 int temp = arrea [left]; 15 int I = left; 16 int j = right; // The 17 while (true & I <j) sign used for loop. // when I = j, indicates that an intermediate position is found, which is the position where the reference number should be 18 {19. Compare from the back to the front, and put the following smaller than or equal to the baseline to the front side of 20 while (true & I <j) // j cannot be minimized. 21 {22 if (arrea [j] <= temp) 23 {24 arrea [I] = arrea [j]; 25 break; 26} 27 else28 {29 j --; 30} 31} 32 // find a number later than temp, put it in the position you just transferred. 33 while (true & I <j) 34 {35 if (arrea [I]> temp) 36 {37 arrea [j] = arrea [I]; 38 break; 39} 40 else41 {42 I ++; 43} 44} 45} 46 // jump out of the loop, now I = j, I is the intermediate position 47 arrea [I] = temp; 48 // sort the left and right sides of the sequence after the first large loop 49 QuickSort (arrea, left, I-1); // sort the left by 50 QuickSort (arrea, I + 1, right ); // sort the right side 51} 52} 53 static void Main (string [] args) 54 {55 int [] data = new int [] {42, 20, 17, 27, 13, 8, 17, 48}; 56 QuickSort (data, 0, data. length-1); 57 foreach (var item in data) 58 {59 Console. writeLine (item); 60} 61 Console. readKey (); 62} 63}

 

    

 

 

 

* ********************** Not to be resumed, continuous updates ****************************

 

 

 

--- Restore content end ---

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.