Sorting and sorting algorithms
1. Insert sort directly (straight insertion sort)
Thought: first, compare the first two numbers, and then insert the second number into the ordered table by size;
The second round scans the first two numbers from the back to the front, inserts the third number into the ordered table by size, and performs (n-1) scanning in sequence.
The entire sorting process will be completed later.
Stable sorting. The worst time complexity is O (n ^ 2) and the space complexity is O (1)
# Include <stdio. h> void move (int start, int end, int * s, int e) {for (int t = end; t> = start; t --) s [t + 1] = s [t];} void InsertSort (int * s, int n) {int t; for (int I = 1; I <n; I ++) {t = s [I]; for (int j = I-1; j> = 0; j --) if (t> = s [j]) {move (j + 1, I-1, s, t); s [j + 1] = t; break;} if (j <0) // The number of current comparisons should be inserted to the front {move (0, I-1, s, t); s [0] = t ;}} int main () {int s [] = {6, 3,}; InsertSort (s, 8); for (int I = 0; I <8; I ++) printf ("% d", s [I]); return 0 ;}
2 binary Merge Sorting
Stable sorting, time complexity O (n log n) space complexity O (n)
The merge sort algorithm is implemented recursively.
First, the interval [s, t] to be sorted is divided by the midpoint,
Next, sort the left subintervals and then the right subintervals,
Finally, merge the left and right intervals into an ordered interval [s, t] with one merge operation.
The merge operation works as follows:
Step 1: Apply for a space so that the size is the sum of the two sorted sequences. This space is used to store the merged sequences.
Step 2: set two pointers. The initial positions are the starting positions of the two sorted sequences respectively.
Step 3: Compare the elements pointed to by the two pointers, select a relatively small element into the merging space, and move the pointer to the next position.
Repeat Step 3 until a pointer exceeds the end Of the sequence
Copy all the remaining elements of another sequence directly to the end of the merged sequence.
# Include <stdio. h> # include <stdlib. h> void Merge (int * s, int start, int mid, int end) // two sequence tables may have different lengths. {int * s1 = (int *) malloc (sizeof (int) * (end-start + 1); int t = 0, I, j; I = start, j = mid + 1; // two pointers point to the starting position of two sorted sequences while (I <= mid & j <= end) // Both pointers do not exceed the end of the sequence {if (s [I]> s [j]) {s1 [t ++] = s [j]; j ++ ;} else {s1 [t ++] = s [I]; I ++ ;}} if (I> mid) // the first sequence table pointer exceeds the end Of the sequence, copy all the remaining elements of the second sequence table to the end of the merging sequence, and vice versa {for (int k = j; k <= end; k ++) s1 [t ++] = s [k];} if (j> end) {for (int k = I; k <= mid; k ++) s1 [t ++] = s [k] ;}for (int k = 0; k <t; k ++) // copy the merged sequence table back {// s [start ++] = s1 [k]; Array Overflow s [start] = s1 [k]; start ++;} free (s1);} void MergeSort (int * s, int start, int end) {if (end-start = 0) return; else if (end-start = 1) {if (s [end] <s [start]) // The prerequisite for switching s [end] = s [start] + s [end]-(s [start] = s [end]); // swap return ;} else {MergeSort (s, start, (end-start)/2 + start); MergeSort (s, (end-start)/2 + start + 1, end ); merge (s, start, (end-start)/2 + start, end); // 2-way Merge} int main () {int s [] = {10, 4, 6, 3,}; MergeSort (s,); for (int I = 0; I <= 7; I ++) printf ("% d ", s [I]); return 0 ;}
Test example:
// 10, 4, 6, 3, 11, 8, 2, 5, 7
// 10, 4, 6, 3, 8, 2, 5, 7
// 2, 1, 8, 3, 9, 10, 5, 3, 7