<排序算法> 插入排序InsertSort
作者:互联网
代码实现:
1 #include<iostream> 2 using namespace std; 3 4 void PrintArr(int arr[],int len); 5 void InsertSort(int arr[],int len) 6 { 7 if(arr == NULL || len <= 0) return ; 8 9 int yes = 0; //有序下标 10 int no = 1; //无序下标 11 int i,j; 12 13 for(i=yes;i<len-1;i++) 14 { 15 if(arr[no] < arr[i]) 16 { 17 int move = arr[no]; 18 for(j=yes;j>=0;j--) 19 { 20 if(arr[j] > move) 21 arr[j+1] = arr[j]; 22 else 23 break; 24 } 25 arr[j+1] = move; 26 } 27 yes ++; 28 no ++; 29 } 30 31 return ; 32 } 33 34 void PrintArr(int arr[],int len) 35 { 36 for(int i=0;i<len;i++) 37 cout << arr[i] << " "; 38 cout << endl; 39 40 return ; 41 } 42 43 int main() 44 { 45 //int arr[10] = {9,8,7,6,5,4,3,2,1,0}; 46 int arr[10] = {4,8,6,3,7,2,9,5,0,1}; 47 InsertSort(arr,sizeof(arr)/sizeof(arr[0])); 48 PrintArr(arr,sizeof(arr)/sizeof(arr[0])); 49 50 system("pause"); 51 return 0; 52 }
标签:arr,int,插入排序,move,len,InsertSort,void,PrintArr 来源: https://www.cnblogs.com/Aaaaaalei0612/p/11218780.html