Select sort
How it works: each time the largest or smallest element is selected from the data element to be sorted, it is stored at the beginning of the sequence until all the data elements to be sorted are exhausted.
Void selectsort (int* a, size_t size) { assert (a); for (int i = 0; i < size; i++) { int min = i; for (int j = i + 1; j < size; j++) { //Select the smallest element if (A[j] < a[i]) { min = j; } } //in the first place if (A[i] != a[min]) { int tmp = a[i]; a[i] = a[ min]; a[min] = tmp; } }}
Heap Sort
only need
Ascending Void adjustdown (int* a, size_t size, int root) { ASSERT (a); int child = root * 2 + 1; while (child < size) { if (Child + 1 < size && a[child + 1] > a[child]) { child++; } if (A[child] > a[root]) { swap (A[child], a[root]); root = child; child = root * 2 + 1; } else { break; } }}void adjustup (int* a, size_t size, int root) { assert (a); int child = root * 2 + 1; while (child < size) { if (child + 1 < size && a[child + 1] < a[child]) { child++; } if (A[child] < a[root]) { swap (A[child], a[root]); root = child; child = root * 2 + 1; } else { break; } }}void heap_sort (int* a, size_t size) { assert (a); //Building for (int i = ( SIZE&NBSP;-&NBSP;2) &NBSP;/&Nbsp;2; i >= 0; i--) { adjustdown (a, size, i); } for (int j = size - 1; j > 0; j--) { swap (A[0], a[j]); adjustdown (a, j, 0); }}void _heap_sort (int* a, size_t size) { assert (a); for (int i = (SIZE&NBSP;-&NBSP;2) / 2; i >= 0; i--) { adjustup (a, size, i); } for ( int j = size - 1; j > 0; j--) { &nbSp; swap (A[0], a[j]); adjustup (a, j, 0); }}
This article from "Write Quality or Low" blog, declined reprint!
Sort (Insert sort, hill sort, select Sort, heap sort)