Insert Sort
程式碼:
結果:
#include<stdio.h>
#include<stdlib.h>
void Insert_Sort(int [],int);
int main(void){
int arr[] = {37,41,19,81,41,25,56,61,49};
Insert_Sort(arr,9);
system("pause");
return 0;
}
void Insert_Sort(int arr[],int n){
int i,j,k,up;
for(i=0;i<n;i++){
up = arr[i];//把第一個元素存入up
j = i;
while(j>0 && arr[i-1]>up){ //前者>UP則成立
arr[j] = arr[j-1];//把前者的值存入後者 ,就是右移
j--;//j-1在比較但j-1為空資料
}
arr[j] = up;;
for(k=0;k<n;k++){
printf("%d ",arr[k]);
}
printf("\n");
}
}
結果:


留言
張貼留言