例99:C語言實現直接插入排序 。 解題思路:直接插入排序是一種最簡單的排序方法,其基本操做是將一條記錄插入到已排好的有序表中,從而獲得一個新的、記錄數量增1的有序表。C語言源代碼演示:#include<stdio.h>//頭文件 int main()//主函數 { void insort(int post[],int n);//函數聲明 int array[11],i;//定義整型變量和數組 printf("請輸入10個數據:\n");//提示語句 for(i=1;i<=10;i++) { scanf("%d",&array[i]);//循環輸入10個數 } printf("原始順序:\n");//提示語句 for(i=1;i<11;i++) { printf("%5d",array[i]);//輸出原來的 } insort(array,10);//調用排序函數 printf("\n插入數據排序後排序:\n");//提示語句 for(i=1;i<11;i++) { printf("%5d",array[i]);//輸出排序後的結果 } printf("\n"); //換行 return 0;//函數返回值爲0 } void insort(int post[],int n) { int i,j; for(i=2;i<=n;i++) //數組下標從2開始,是[0]作監視哨,s[1]一個數據無可比性 { post[0]=post[i]; //給監視哨賦值 j=i-1; //肯定要比較元素的最右邊位置 while(post[0]<post[j]) { post[j+1]=post[j]; //數據右移 j--; //移向左邊一個未比較的數 } post[j+1]=post[0]; //在肯定的位置插入s[i] } }編譯運行結果:請輸入10個數據: 9 8 7 1 4 5 0 3 2 99 原始順序: 9 8 7 1 4 5 0 3 2 99 插入數據排序後排序: 0 1 2 3 4 5 7 8 9 99 -------------------------------- Process exited after 14.48 seconds with return value 0 請按任意鍵繼續. . .