Bubble sort, selection sort, insertion sort, quick sort

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Data;
  6 
  7 namespace TestSample
  8 {
  9 
 10     #region explaination
 17 
 18 
 19     //一、冒泡排序 
 20 
 21     //Given a set of out of order data a [1], a [2 ], ...... a [n], which need to be in ascending order. First compare a [1] and a [2] value, when a [1] is greater than a [2] values of both the exchange is otherwise unchanged. Then compare a [2] and a [3] value, if a [2] is greater than a [3] values of both the exchange is otherwise unchanged. Then compare a [3] and a [4], and so on, and finally comparing a [n-1] and the value a [n] is. After such a treatment, a [n] is the value of this data set must be the largest. Again a [1] ~ a [n -1] a process in the same manner, the value a [n-1] must be a [1] ~ a [n -1] is the largest. Again a [1] ~ a [n -2] a process in the same manner, and so on. After a total of n-1 treated wheel a [1], a [2 ], ...... a [n] to the ascending order. 
22 is      
23 is      @ advantages: stable, known number of comparisons; its time complexity is O (^ n-2)
 24  
25      @ drawbacks: slow, can only be moved adjacent two data, the number of times of data movement. 
26 is  
27  
28      // Second, selection sort 
 29  
30      //Given a set of out of order data a [1], a [2 ], ...... a [n], which need to be in ascending order. First compare a [1] and a [2] value, when a [1] is greater than a [2] values of both the exchange is otherwise unchanged. Then compare a [1] and a [3] value, when a [1] is greater than a [3] values of both the exchange is otherwise unchanged. Then compare a [1] and a [4], and so on, and finally comparing a [1] and the value a [n] is. After such a treatment, a value of [1] must be this minimum set of data. Then a [2] and a [3] ~ a [n ] in the same manner as a comparison, is a [2] value must be a [2] ~ a [n ] is the smallest. Then a [3] and a [4] ~ a [n ] in the same manner as a comparison, and so on. After a total of n-1 treated wheel a [1], a [2 ], ...... a [n] to the ascending order. 
31      // always assume a minimum [a1]
 32      // each pass selected minimum (or maximum) of an element from data element to be sorted in the order in rows the number of columns has been sorted in the last, until all be sorting data elements drained. Selection Sort sorting method is unstable
 33 is  
34 is      @ advantages: stable, bubble sort as the number of comparisons, the number of data movement is less than the bubble sort; its time complexity is O (^ n-2)
 35  
36      // disadvantages: relative or slow below. 
37 [  
38 is      // Third, insertion sort 
 39  
40      @This sequence is assumed that the array is properly arranged, and then back from the beginning, if several current value is larger than the outer element, this position will be moved back number, until the value of the current element is greater than or equal to the outer position in front of its . Until this algorithm after having drained before the number of k, can guarantee a [1 ... k] is a partially ordered, to ensure the accuracy of insertion.
 41 is  
42 is      @ advantages: stable, fast; O (n ^ 2)
 43  
44      // drawback: the number of comparisons are not necessarily, the fewer the number of comparisons, the more data after moving the insertion point, especially when a large amount of data time, but as a linked list can solve this problem. 
45  
46      // Fourth, reduce the incremental sort 
 47  
48      // 1959 proposed by Hill, also known as the Hill sorting. 
49  
50      // known an unordered set of data a [1], a [2 ], ...... a [n], which need to be in ascending order. Found that when n is not that good insertion sort of effect. First, take an increment d (d <n), to a [1], a [1 + d], a [1 + 2d] ...... as the first group, a [2], a [ 2 + d] , a [2 + 2d] ...... as the second group ......, a [d], a [2d], a [3d] ...... and so on as the last group, each group was inserted in the sorting, then take d '<d, above operation is repeated until the d = 1. 
51 is  
52 is      @ advantages: faster, less data movement; 
 53 is  
54 is      //Disadvantages: unstable, the value of d is how much, how many different values should be taken, we can not know for sure, only by experience to take. 
55  
56      // V. quicksort 
 57  
58      // Quick Sort Bubble Sort is an improved version, it is currently the fastest known sorting method. 
59  
60      // set is to sort the array A [0] ...... A [N -1], select a first arbitrary data (typically a first selection data) data as a key, and then all of it is smaller than the number of all the discharge to the front of it, all larger than its number are put behind it, a process known as a trip to quick sort. It is noteworthy that, quick sort is not a stable sorting algorithm, that is, the same relative positions of the plurality of values may fluctuate at the end of the algorithm.
61 is  
62 is      @ advantages: fast, less data movement; O (nlog n) a desired time, O (n ^ 2) worst case 
 63 is  
64      @ disadvantages: unstable. 
65  
66      #endregion 
67  
68      class Program
 69      {
 70          ///  <Summary> 
71 is          /// bubble sort
 72         /// </summary>
 73         /// <param name="array"></param>
 74         static void Bsort(int[] array)
 75         {
 76             //int[] bsort = { 12, 35, 11, 67, 34, 89, 99, 23 };
 77             for (int i = 0; i < array.Length; i++)
 78             {
 79                 for (int j = i + 1; j < array.Length; j++)
 80                 {
 81                     if (array[j] < array[i])
 82                     {
 83                         int temp;
 84                         temp = array[j];
 85                         array[j] = array[i];
 86                         array[i] = temp;
 87 
 88                     }
 89                 }
 90             }
 91             Console.WriteLine("after the bubble sort, the array as below:");
 92             for (int i = 0; i < array.Length; i++)
 93             {
 94                 Console.Write(array[i] + " ");
 95             }
 96  
 97         }
 98 
 99         /// <summary>
100         /// select sort
101         /// </summary>
102         /// <param name="array"></param>
103         static void SelectSort(int[] array)
104         {
105             for (int i = 0; i < array.Length; i++)
106             {
107                 int temp;
108                 int min = array[i];
109                 for (int j = i + 1; j < array.Length; j++)
110                 {
111                     if (min > array[j])
112                     {
113                         min = array[j];
114                         temp = array[i];
115                         array[i] = array[j];
116                         array[j] = temp;
117                     }
118                 }
119             }
120             Console.WriteLine("after the select sort, the array as below:");
121             for (int i = 0; i < array.Length; i++)
122             {
123                 Console.Write(array[i] + " ");
124             }
125         }
126 
127         /// <summary>
128         /// insert sort
129         /// </summary>
130         /// <param name="array"></param>
131 
132         static void Isort(int[] array)
133         {
134             int temp;
135             for (int i = 1; i < array.Length; i++)
136             {
137                 temp = array[i];
138                 int j = i;
139                 while (j > 0 && array[j - 1] > temp)
140                 {
141                     array[j] = array[j - 1];
142                     j--;
143                 }
144                 array[j] = temp;
145 
146             }
147             Console.WriteLine("after the Insert sort,the array as below:");
148 
149             for (int i = 0; i < array.Length; i++)
150             {
151                 Console.Write(array[i] + " ");
152             }
153         }
154 
155          static  void the QuickSort ( int [] Array, int left, int right)
 156          {
 157              // left index is less than the right, the sort is not yet completed 
158              IF (left < right)
 159              {
 160.                  // taking the middle element as the comparison reference , less than his to the left shift, greater than his to the right shifter 
161                  int Middle = Array [(left + right) / 2 ];
 162                  int I = left - . 1 ;
 163                  int J = right + . 1 ;
 164 is                  the while ( to true )
 165                 {
166                     while (array[++i] < middle && i < right) ;
167                     while (array[--j] > middle && j > 0) ;
168                     if (i >= j)
169                         break;
170                     //Swap(numbers, i, j);
171                     int temp = array[i];
172                     array[i] = array[j];
173                     array[j] = temp;
174                 }
175                 QuickSort(array, left, i - 1);
176                 QuickSort(array, j + 1, right);
177             }
178 
179 
180 
181         }
182 
183         static void Main(string[] args)
184         {
185             int[] array = new int[10];
186             List<int> list=new List<int>();
187             Random rd=new Random();
188 
189             for(int i=0;i<10;i++)
190             {
191                 int next=rd.Next(1,100);
192                 if (!list.Contains(next))
193                 {
194                     list.Add(next);
195                 }
196             }
197 
198             array = list.ToArray();
199 
200             Bsort(array);
201             Console.WriteLine();
202             SelectSort(array);
203             Console.WriteLine();
204             Isort(array);
205             Console.WriteLine();
206             QuickSort(array,0,array.Length-1);
207             Console.WriteLine("after the quick sort,the array as below:");
208 
209             for (int m = 0; m < array.Length; m++)
210             {
211                 Console.Write(array[m] + " ");
212             }    
213 
214         }
215     }    
216 }

 

Reproduced in: https: //www.cnblogs.com/Jenny90/archive/2013/04/13/3018401.html

Guess you like

Origin blog.csdn.net/weixin_34406061/article/details/93566816