Select and sort by Sorting Algorithm
Select sorting definition: select the smallest (or largest) element from the data elements to be sorted, and place the elements in sequence at the end of the sorted series, until all data elements to be sorted are arranged. Selecting sorting is an unstable sorting method.
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); SelectSort (array); Console. writeLine ("sorted:"); Console. writeLine (string. join (",", array); Console. readKey ();} /// <summary> /// select sort /// </summary> /// <param name = "sources"> target array </param> private static void SelectSort (int [] sources) {for (int I = 0, len = sources. length-1; I <= len; I ++) {// assume that the minimum index int minIndex = I; // cyclically traverse the index for (int j = I + 1; j <= len; j ++) that finds the minimum value {// if the minimum value is greater than other elements, reset the minimum value index if (sources [minIndex]> sources [j]) {minIndex = j ;}// the position where the temporary variable exchanges the minimum value; int temp = sources [I]; sources [I] = sources [minIndex]; sources [minIndex] = temp ;}}}
C language selection Sorting Algorithm
Void bubble (int a [], int n) {int I, k, temp; for (k = 0; k <n-1; k ++) {for (I = k + 1; I <n; I ++) {if (a [k]> a [I]) {temp = a [I]; a [I] = a [k]; a [k] = temp ;}}for (I = 0; I <n; I ++) printf ("% d", a [I]); printf ("\ n ");}
What is the idea of selecting a sorting algorithm?
Select the maximum value of the current sequence for each time:
For example, 1, 3, 6, 9, and 5 are sorted in ascending order!
Step 1
Step 2, 1
Step 3, 3, 5
Step 4, 1, 5, 6
Step 5, 1, 5, 6, 9
// Progressively select the implementation of sorting!
Void SelectSort (double * head, int amount)
{
Double temp;
Int m, n;
For (m = 0; m <amount-1; m ++)
{
For (n = m + 1; n <amount; n ++)
{
If (head [n] {
Temp = head [n];
Head [n] = head [m];
Head [m] = temp;
}
}
}
}