#include <stdio.h> #define CNT 6 //define sort number//bubble sort optimization void bubble_sort (int a[],int N) {for (int P = N-1; P >= 0; p--) {int flag = 0; Defines whether the sequence after the flag is ordered for (int i = 0;i < p;i++)/ /A trip bubbling {if (A[i] > a[i+1]) //bubbling {int temp = a[i+1]; A[I+1] = A[i]; A[i] = temp;} flag = 1;} if (flag = = 1) //description followed by sequence and ordered not in row break;}} Insert sort void insertion_sort (int a[],int N) {for (int P = 1; P < N; p++)///Initial number take P "0" as the basic number and then insert n card for loop execution order first execute 1 and then determine if the condition {int Tmp = a[p] is satisfied; Store the number I need to insert int i; Record the move position for (i = p;i>0&&a[i-1]>tmp;i--) //Move the number that needs to be inserted {a[i] = a[i-1];} A[i] = Tmp;}} int main () {int a[cnt] = {5,7,4,1,9,6};insertion_sort (a,cnt); for (int i=0;i< cnt;i++) {printf ("%d", A[i]);} GetChar ();}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Bubble sort optimization and insert sort (this note is less suitable for a certain basis of viewing)