1, Bubble algorithm.
The bubbling algorithm is the most basic sort algorithm, which is used to compare the first value with the adjacent value, and if the large number is swapped to the left in ascending order, the descending direction is exchanged to the right. Eventually, the large number is moved to one side, and a sequence is eventually formed:
Public classsorting{ Public voidBubblesorting () {int[] arr =New int[10]; Random rd =NewRandom (100); for(inti = 0; I < arr. Length; i++) {Arr[i] = Rd. Next (1, 100); } Console.WriteLine ("Init Array:"); Print (arr); Console.WriteLine ("Sorting:"); for(inti = arr. Length-1; I >= 1; i--) { for(intj = 0; J <= I-1; J + +) {if(Arr[j] > arr[j + 1]) {Swap (refARR[J],refArr[j + 1]); }} Print (arr); } Console.WriteLine ("Sort Result:"); Print (arr); }voidSwap (ref intAref intb) {var temp = A; A = b; b = temp; }voidPrint (int[] list) { for(inti = 0; I < list. Length; i++) {Console.Write ("" + list[i]); }
Console.WriteLine (); }}
Results:
2. Select sort
The choice of sorting requires a two-layer loop to implement, the outer loop control number, and the inner loop control to find the smallest value. The minimum value found in the inner loop is then exchanged with the outer loop of the corresponding element of the index until the complete array is traversed.
Public void Selectionsort () { int[] arr = Initarray (ten); int length = ten; Console.WriteLine ("Sorting:"); for (int i = 0; i < length; i++) { int min = i; for (int j = i + 1; j < length; J + +) { if (Arr[min] > arr[j]) min = j; } Swap (refref arr[min]); Print (arr); } Console.WriteLine ("Sort Result:"); Print (arr);}
Results:
3. Insert Sort
The insert sort has a two-layer loop, the outer loop iterates through the array elements, the inner loop compares the outer loop elements with the element's next element in the inner loop, and if the outer loop selects elements that are smaller than the inner loop-selected elements, the array elements move right to the inner loop element to leave the position.
Public void Insertionsort () { int[] arr = Initarray (ten); int length = ten; Console.WriteLine ("Sorting:"); for (int i = 1; i < length; i++) { int temp = arr[i]; int inner=i; while (Inner > 0 && arr[inner-1] >= temp) { Arr[inner] = arr[inner-1]; inner--; } Arr[inner] = temp; Print (arr); } Console.WriteLine ("Sort Result:"); Print (arr);}
Results:
4. The efficiency of three kinds of algorithms
To make a simple comparison, here the initialization data is in each of the algorithms, because each algorithm contains initialization and therefore does not affect the comparison.
Test code:
Static void testsorting (int size) { new sorting (); New Stopwatch (); Watch. Start (); Sort. bubbleascsorting (size); Watch. Stop (); Console.WriteLine ("bubbleascsorting time Milliseconds:" + watch. Elapsedmilliseconds); Watch. Restart (); Sort. Selectionsort (size); Watch. Stop (); Console.WriteLine ("selectionsort time Milliseconds:" + watch. Elapsedmilliseconds); Watch. Restart (); Sort. Insertionsort (size); Console.WriteLine ("insertionsort time Milliseconds:" + watch. Elapsedmilliseconds); Console.readkey ();}
1000 integer Results:
10,000 integer Results:
100,000 integer Results:
Get the following table from the test results:
|
Bubble |
Selection |
Insertion |
Bubble/selection |
Bubble/insertion |
Selection/insertion |
| 1000 |
15 |
4 |
3 |
3.75 |
5 |
1.333333333 |
| 10000 |
1342 |
412 |
283 |
3.257281553 |
4.74204947 |
1.455830389 |
| 100000 |
125212 |
40794 |
27570 |
3.069372947 |
4.541603192 |
1.479651795 |
|
|
|
|
|
|
|
| Avg |
|
|
|
3.358884833 |
4.761217554 |
1.422938506 |
Ignoring the test environment, since three algorithms are running in the same environment, the following conclusions can be drawn:
1. The bubbling algorithm is the least efficient.
2. The insertion algorithm is the most efficient.
3. The selection algorithm is 3.3 times times the bubble algorithm.
4. The insertion algorithm is 4.7 times times the bubble algorithm.
5. The insertion algorithm is the 1.4 Companion of the selection algorithm.
Three basic sorting algorithms for efficiency comparisons (bubbling, selecting, and inserting)