1. Insert sort (Insertion Sort Direct insert sort )
thinking :
The unordered sequence to be sorted is regarded as an ordered sequence containing only one element and an unordered sequence , Insert the elements in the unnecessary sequence into the corresponding positions of the ordered sequence .
Algorithm implementation :
// Direct insertion method I , The insertion position and the element after the position are moved back void InsertSortMove(int a[], int n) { int i, j,temp;
for(i=1; i<n; i++){ if(a[i] < a[i-1]){ // Determine whether it is an element to be inserted into an ordered sequence temp = a[i];
// Save the element to be inserted for(j=i-1; j>=0; j--){ if(a[j]>temp){ a[j+1] = a[j]; // Move the element in front of him back }
else break; } a[j+1] = temp; // Find the insertion location , Other elements have been moved back and saved. You can insert them directly } } }
// Direct insertion method II : Replacing the backward shift of elements in one by data exchange void InsertSortSwap(int a[], int n) { int temp; for(int
i=1; i<n; i++){ for(int j=i-1; j>=0; j--){ if(a[j]>a[j+1]){
// Judge whether this element and its previous element are in order , Disorder is exchanged temp=a[j]; // Until the element and its preceding elements are all in order a[j]=a[j+1];
a[j+1]=temp; } } } }
dynamic simulation :( Displacement insertion )
Code test :
1. Displacement insertion :
2. Swap insert :
2. Bubble sorting (Bubble Sort)
thinking :
In a set of numbers to sort , Compare and adjust the position of two unordered adjacent numbers in turn , Conditional floating . Each round of sorting will add a qualified element to the ordered sequence .
Algorithm implementation :
void BubbleSort(int a[],int n){ int temp; for (int i = 0; i < n-1; i++){ for
(int j = 0; j < n - i - 1; j++){ if (a[j] > a[j + 1]){ // The logic here is similar to the data exchange of direct insertion temp
= a[j + 1]; // The difference is that bubbling goes from front to back , Place ordered elements back a[j + 1] = a[j]; a[j] = temp; } } } }
dynamic simulation :
Code test :
3. Select sort (Select Sort)
thinking :
In the unordered sequence, select the element that meets the condition and the exchange position of the first element in the sequence , Then find the elements that meet the conditions from the other elements of the unordered sequence and insert them into the ordered sequence , Repeat operation , Until all elements are in order .
Algorithm implementation :
void SelSort(int a[],int n) { int temp,min; for (int i = 0; i < n - 1; i++) {
min = i; for (int j = i + 1; j < n ; j++) { // In the unordered sequence, select a number that meets the conditions to store the subscript if (a[j] <
a[min]) { // Finally, it is placed at the end of the ordered sequence min = j; } } if (min != i) { temp = a[i];
a[i]=a[min]; a[min] = temp; } } }
dynamic simulation :
Code test :
attach : Test code
#include<stdio.h> void print(int a[],int n){ for(int i=0;i<n;i++)
printf("%3d",a[i]); printf("\n"); } void printRes(int a[],int n,int i){
printf(" The first %2d The second cycle is completed :",i+1); for(int i=0;i<n;i++) printf("%3d",a[i]);
printf("\n"); } void InsertSortMove(int a[], int n) { int i, j,temp; for(i=1;
i<n; i++){ if(a[i] < a[i-1]){ temp = a[i]; for(j=i-1; j>=0; j--){
if(a[j]>temp){ a[j+1] = a[j]; } else break; } a[j+1] = temp; } printRes(a,n,i);
} } void InsertSortSwap(int a[], int n) { int temp; for(int i=1; i<n; i++){
for(int j=i-1; j>=0; j--){ if(a[j]>a[j+1]){ temp=a[j]; a[j]=a[j+1];
a[j+1]=temp; } } printRes(a,n,i); } } void BubbleSort(int a[],int n){ int temp;
for (int i = 0; i < n-1; i++){ for (int j = 0; j < n - i - 1; j++){ if (a[j] >
a[j + 1]){ temp = a[j + 1]; a[j + 1] = a[j]; a[j] = temp; } } printRes(a,n,i);
} } void SelSort(int a[],int n) { int temp,min; for (int i = 0; i < n - 1; i++)
{ min = i; for (int j = i + 1; j < n ; j++) { if (a[j] < a[min]) { min = j; } }
if (min != i) { temp = a[i]; a[i]=a[min]; a[min] = temp; } printRes(a,n,i); } }
int main(){ int a[15]={3,44,38,5,47,15,36,26,27,2,46,4,19,50,48};
printf(" Before sorting :"); print(a,15); SelSort(a,15); }
( Animation comes from the network )
Technology