1. Fast sorting
For more information, see the quick sorting algorithm in the previous blog.
2. Heap sorting
For details, see the heap sorting implementation of non-recursive methods in the previous blog post.
3. Simple sorting (Bubble sorting, selection sorting, and insertion sorting)
The Code is as follows:
# Include <stdio. h> # include <stdlib. h> # include <time. h> # define N 20 static void show (int * arr, int Len) {int index; For (Index = 0; index <Len; index ++) {printf ("% d", arr [Index]);} printf ("\ n");} static void swap (int * left, int * right) {int TMP = * left; * Left = * right; * Right = TMP;}/*** select the sorting method: in order to put the minimum number of unordered array in the position of the POs, POS from 0 to N-1 * --> the number of front N-1 position, the last number is ignored */static void select_sort (int * arr, int Len) {Int POs, index; int min_index; For (Pos = 0; POS <len-1; POS ++) {min_index = Pos; For (Index = POS + 1; index <Len; index ++) {If (ARR [Index] <arr [min_index]) min_index = index;} If (min_index! = POS) {swap (ARR + POs, arr + min_index) ;}} static void insert_sort (int * arr, int Len) {int POs, index; int key; for (Pos = 1; POS <Len; POS ++) {key = arr [POS]; for (Index = pos-1; index> = 0; index --) {If (Key <arr [Index]) {arr [index + 1] = arr [Index];} else {break ;}} arr [index + 1] = Key ;}} static void bubble_sort (int * arr, int Len) {int POs, index; For (Pos = 0; POS <len-1; pos ++) {for (Index = 0; index <len-1-Pos; index ++) {If (ARR [Index]> arr [index + 1]) {swap (ARR + index, arr + index + 1) ;}}} static void bubble_sort2 (int * arr, int Len) {int left, right, index; left = 0; Right = len-1; while (left <right) {for (Index = left; index <right; index ++) {If (ARR [Index]> arr [index + 1]) {swap (ARR + index, arr + index + 1) ;}} right --; if (Left = right) {break;} For (Index = right; index> left; index --) {If (ARR [Index] <arr [index-1]) {swap (ARR + index, arr + index-1) ;}}left ++ ;}int main (INT argc, char * argv []) {int arr [N], index; srand (Time (null); For (Index = 0; index <n; index ++) {arr [Index] = rand () % 100 + 1;} Show (ARR, n); bubble_sort2 (ARR, n); show (ARR, n); System ("pause"); Return 0 ;}4. Merge Sorting
To be continued.
Sorting Algorithm Summary