sort
Bubble sort:
Bubble Sort is a relatively simple sorting algorithm in the field of computer science. It repeatedly visits the column of elements to be sorted, compares two adjacent elements in turn, and swaps them if their order (e.g. from large to small, and the initials from A to Z) is wrong. The work of visiting elements is repeated until no adjacent elements need to be exchanged, that is to say, the elements have been sorted. The algorithm's name comes from the fact that larger elements float slowly to the top of the sequence (ascending or descending) by exchange, just as carbon dioxide bubbles in carbonated drinks eventually float to the top, so it's called "bubble sorting".
Algorithmic idea of bubble sort code:
void BubbleSort(int arr[], int len) { int i = 0; int temp,j; for(i = 0; i < len - 1; i++)//Number of trips { for(j = 0; j<=i; j++)//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; } } } for (i = 0; i < len; i++) { printf("%d ", arr[i]); } }
If you finish a certain trip, you will find that the order has been arranged and there is no exchange. Reduce the number of unnecessary cycles (bubble optimization):
void BubbleSort1(int arr[], int len) { int i = 0; int temp, j; int flag; for (i = 0; i < len - 1; i++)//Number of trips { flag = 0; for (j = 0; j <= len - 1 - i; j++)//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; flag = 1; } } if (flag == 0) { break; } } }
Selection Sort:
Selection sort is a simple and intuitive sorting algorithm. It works by selecting the smallest (or largest) element from the data element to be sorted, and storing it at the beginning of the sequence until all the data elements to be sorted out. Selective sorting is an unstable sorting method.
Algorithmic thinking of code:
void Selectsort(int arr[], int len) { int i = 0; int j = 0; int temp; for (i = 0; i < len - 1; i++) { for (j = i+1; j < len ; j++) { if (arr[i]>arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } }
Direct insertion sort:
Direct insertion sort refers to taking the first element from the disordered table at a time and inserting it into the proper position of the ordered table so that the ordered table is still in order. The specific method is to compare the first two numbers in the first step, and then insert the second number into the ordered table by size; the third data and the first two numbers are scanned from front to back in the second step, and the third number is inserted into the ordered table by size; the whole sorting process is completed after (n-1) scanning.
It consists of two nested loops. The outer loop identifies and determines the values to be compared. The inner loop determines its final position for the values to be compared. Direct insertion sort compares the value to be compared with its previous value, so the outer loop starts with the second value. At present, when a value is larger than the value to be compared, the cycle is continued until the value smaller than the value to be compared is found and the value to be compared is placed in the next position to end the cycle.
Algorithmic thinking of code:
void InsertSort(int arr[], int len) { int i = 0; int j = 0; int temp; for (i = 1; i < len; i++) { temp = arr[i]; for (j = i - 1; j >= 0; j--) { if (arr[j]>temp) { arr[j + 1] = arr[j]; } else break; } arr[j + 1] = temp; } }
Features: The more orderly the original array, the faster the sorting.