<> one . Bubble sort

Bubble sort ( English :Bubble
Sort) Is a simple sort algorithm . It repeatedly visits the sequence to be sorted , Compare two elements at a time , If they're in the same order ( Such as from big to small , Initials from A reach Z) I'll trade them for mistakes .
#include <stdio.h> void bubble_sort(int arr[], int len) { int i, j, temp; for (
i= 0; i < len - 1; i++) for (j = 0; j < len - 1 - i; j++) if (arr[j] > arr[j + 1
]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } int main() { int
arr[] = { 22, 34, 3, 32, 82}; int len = (int) sizeof(arr) / sizeof(*arr);
bubble_sort(arr, len); for (int i = 0; i < len; i++) printf("%d ", arr[i]);
return 0; }
<> two . Select sort

Select sort (Selection
sort) It is a simple and intuitive sorting algorithm . It works as follows . First, find the minimum value in the unordered sequence ( large ) element , To the beginning of the sort sequence , then , Then we continue to search for the smallest element from the remaining unordered elements ( large ) element , Then put it at the end of the sorted sequence . and so on , Until all elements are sorted .
#include <stdio.h> void swap(int *a,int *b) // Exchange two numbers { int temp = *a; *a = *b; *
b= temp; } void selection_sort(int arr[], int len) { int i,j; for (i = 0 ; i <
len- 1 ; i++) { int min = i; for (j = i + 1; j < len; j++) // Visit unordered elements if (arr[j]
< arr[min]) // Find the current minimum min = j; // Record minimum swap(&arr[min], &arr[i]); // In exchange } } int
main() { int arr[] = { 22, 34, 3, 32, 82}; int len = (int) sizeof(arr) / sizeof(
*arr); selection_sort(arr, len); for (int i = 0; i < len; i++) printf("%d ", arr
[i]); return 0; }
<> three . Insert sort

Insert sort ( English :Insertion
Sort) It is a simple and intuitive sorting algorithm . It works by building ordered sequences , For unsorted data , Scan from back to front in sorted sequence , Find the location and insert .
#include <stdio.h> void insertion_sort(int arr[], int len){ int i,j,temp; for (
i=1;i<len;i++){ temp = arr[i]; for (j=i;j>0 && arr[j-1]>temp;j--) arr[j] = arr[j
-1]; arr[j] = temp; } } int main() { int arr[] = { 22, 34, 3, 32, 82}; int len =
(int) sizeof(arr) / sizeof(*arr); insertion_sort(arr, len); for (int i = 0; i <
len; i++) printf("%d ", arr[i]); return 0; }
<> four . Shell Sort

Shell Sort , Also known as descending incremental sorting algorithm , It is a more efficient and improved version of insert sort . Hill sort is an unstable sort algorithm . Hill sort is an improved method based on the following two properties of insertion sort :

* Insert sort is used to sort almost ordered data , efficient , That is, it can achieve the efficiency of linear sorting
* But insert sort is generally inefficient , Because insert sort can only move data one bit at a time #include <stdio.h> void shell_sort(int arr[]
, int len) { int gap, i, j; int temp; for (gap = len >> 1; gap > 0; gap = gap >>
1) for (i = gap; i < len; i++) { temp = arr[i]; for (j = i - gap; j >= 0 && arr[
j] > temp; j -= gap) arr[j + gap] = arr[j]; arr[j + gap] = temp; } } int main()
{ int arr[] = { 22, 34, 3, 32, 82}; int len = (int) sizeof(arr) / sizeof(*arr);
shell_sort(arr, len); for (int i = 0; i < len; i++) printf("%d ", arr[i]);
return 0; }
<> five . Merge sort

Divide the data into two sections , Select the smallest element from the two segments one by one and move it to the end of the new data segment . It can be done from top to bottom or from bottom to top .

Iterative method :
#include <stdio.h> #include <stdlib.h> int min(int x, int y) { return x < y ? x
: y; } void merge_sort(int arr[], int len) { int* a = arr; int* b = (int*)
malloc(len * sizeof(int)); int seg, start; for (seg = 1; seg < len; seg += seg)
{ for (start = 0; start < len; start += seg + seg) { int low = start, mid = min(
start+ seg, len), high = min(start + seg + seg, len); int k = low; int start1 =
low, end1 = mid; int start2 = mid, end2 = high; while (start1 < end1 && start2 <
end2) b[k++] = a[start1] < a[start2] ? a[start1++] : a[start2++]; while (start1
< end1) b[k++] = a[start1++]; while (start2 < end2) b[k++] = a[start2++]; } int*
temp= a; a = b; b = temp; } if (a != arr) { int i; for (i = 0; i < len; i++) b[
i] = a[i]; b = a; } free(b); } int main() { int arr[] = { 22, 34, 3, 32, 82};
int len = (int) sizeof(arr) / sizeof(*arr); merge_sort(arr, len); for (int i = 0
; i < len; i++) printf("%d ", arr[i]); return 0; }
Recursive method :
#include <stdio.h> void merge_sort_recursive(int arr[], int reg[], int start,
int end) { if (start >= end) return; int len = end - start, mid = (len >> 1) +
start; int start1 = start, end1 = mid; int start2 = mid + 1, end2 = end;
merge_sort_recursive(arr, reg, start1, end1); merge_sort_recursive(arr, reg,
start2, end2); int k = start; while (start1 <= end1 && start2 <= end2) reg[k++]
= arr[start1] < arr[start2] ? arr[start1++] : arr[start2++]; while (start1 <=
end1) reg[k++] = arr[start1++]; while (start2 <= end2) reg[k++] = arr[start2++];
for (k = start; k <= end; k++) arr[k] = reg[k]; } void merge_sort(int arr[],
const int len) { int reg[len]; merge_sort_recursive(arr, reg, 0, len - 1); } int
main() { int arr[] = { 22, 34, 3, 32, 82}; int len = (int) sizeof(arr) / sizeof(
*arr); merge_sort(arr, len); for (int i = 0; i < len; i++) printf("%d ", arr[i])
; return 0; }
<> six . Quick sort

Randomly select an element in the interval as the benchmark , Place elements smaller than the datum before the datum , Elements larger than the datum are placed after the datum , Then sort the decimal area and the large number area respectively .

Iterative method :
#include <stdio.h> typedef struct _Range { int start, end; } Range; Range
new_Range(int s, int e) { Range r; r.start = s; r.end = e; return r; } void swap
(int *x, int *y) { int t = *x; *x = *y; *y = t; } void quick_sort(int arr[],
const int len) { if (len <= 0) return; // avoid len A segment error is raised when the value is equal to negative (Segment Fault) //
r[] Simulation list ,p For quantity ,r[p++] For push,r[--p] For pop And get the element Range r[len]; int p = 0; r[p++] =
new_Range(0, len - 1); while (p) { Range range = r[--p]; if (range.start >=
range.end) continue; int mid = arr[(range.start + range.end) / 2]; // Select the middle point as the datum point
int left = range.start, right = range.end; do { while (arr[left] < mid) ++left;
// Check whether the left side of the reference point meets the requirements while (arr[right] > mid) --right; // Check whether the right side of the reference point meets the requirements if (left <=
right) { swap(&arr[left],&arr[right]); left++;right--; // Move the pointer to continue } } while (
left<= right); if (range.start < right) r[p++] = new_Range(range.start, right);
if (range.end > left) r[p++] = new_Range(left, range.end); } } int main() { int
arr[] = { 22, 34, 3, 32, 82}; int len = (int) sizeof(arr) / sizeof(*arr);
quick_sort(arr, len); for (int i = 0; i < len; i++) printf("%d ", arr[i]);
return 0; }
Recursive method :
#include <stdio.h> void swap(int *x, int *y) { int t = *x; *x = *y; *y = t; }
void quick_sort_recursive(int arr[], int start, int end) { if (start >= end)
return; int mid = arr[end]; int left = start, right = end - 1; while (left <
right) { while (arr[left] < mid && left < right) left++; while (arr[right] >=
mid&& left < right) right--; swap(&arr[left], &arr[right]); } if (arr[left] >=
arr[end]) swap(&arr[left], &arr[end]); else left++; if (left)
quick_sort_recursive(arr, start, left - 1); quick_sort_recursive(arr, left + 1,
end); } void quick_sort(int arr[], int len) { quick_sort_recursive(arr, 0, len -
1); } int main() { int arr[] = { 22, 34, 3, 32, 82}; int len = (int) sizeof(arr)
/ sizeof(*arr); quick_sort(arr, len); for (int i = 0; i < len; i++) printf("%d "
, arr[i]); return 0; }

Technology