Direct insertion of sorting algorithms and direct insertion of sorting algorithms
Insert the sorting definition directly:Each time the first element is extracted from the unordered table, it is inserted to the proper position of the ordered table, so that the ordered table is still ordered. Directly inserted sorting is a stable sorting. The worst time complexity is O (n ^ 2), and the space complexity is O (1 ).
Class Program {static void Main (string [] args) {int [] array = new [] {234,632, 23,643, 2, 6,-2,423,234, 43}; Console. writeLine ("Before sorting:"); Console. writeLine (string. join (",", array); InsertSort (array); Console. writeLine ("sorted:"); Console. writeLine (string. join (",", array); Console. readKey ();} /// <summary> /// insert sort directly /// </summary> /// <param name = "sources"> target array </param> private static void InsertSort (int [] sources) {// starting from Index 1, suppose that sources [0] has been ordered for (int I = 1, len = sources. length-1; I <= len; I ++) {// prepare the data to be inserted int insertValue = sources [I], // assume that the index to be inserted is insertIndex = I-1; // retrieve the inserted index position while (insertIndex> = 0 & insertValue <sources [insertIndex]) {// One sources [insertIndex + 1] = sources [insertIndex]; insertIndex --;} // if the preceding conditions are not met, the location is found, insert data sources [insertIndex + 1] = insertValue ;}} /// <summary> /// insert sort directly for implementation /// </summary> /// <param name = "sources"> target array </param> private static void InsertSort1 (int [] sources) {for (int I = 1, len = sources. length-1; I <= len; I ++) {for (int j = I; j> 0; j --) {if (sources [j]> sources [j-1]) //> descending, <ascending {int temp = sources [j]; sources [j] = sources [j-1]; sources [j-1] = temp ;}}}}}
Apply the direct insertion Sorting Algorithm to sort the key-value SEQUENCES {,} from small to large, and try to write the results of each sort?
6, 18, 28, 93, 46, 55
6, 18, 28, 93, 46, 55
6, 18, 28, 93, 46, 55
6, 18, 28, 46, 93, 55
6, 18, 28, 46, 55, 93
1. Sorting (2 persons): (1) Various Sorting Algorithm operations can be performed. Sorting includes direct insertion sorting, Bubble sorting, fast sorting, and heap sorting.
# Include <iostream>
Using namespace std;
Void qsort (int array [], int size)
{
Int z = 0, y = size-1;
Int k = array [z];
If (z <y)
{
Do
{
While (z <y) & (array [y]> = k) y --;
If (z <y)
{
Array [z] = array [y];
Z ++;
}
While (z <y) & (array [z] <= k) z ++;
If (z <y)
{
Array [y] = array [z];
Y --;
}
} While (z! = Y );
Array [z] = k;
Qsort (array, z );
Qsort (array + z + 1, size-z-1 );
}
}
Int main ()
{
Int n, I;
Cin> n;
Int * a = new int [n + 1];
For (I = 0; I <n; I ++) cin> a [I];
Qsort (a, n );
For (I = 0; I <n; I ++) cout <a [I] <'';
Cout <endl;
System ("pause ");
Return 0;
}