6-1 直接插入排序 (10 分)
阿新 • • 發佈:2018-12-17
本題要求實現直接插入排序函式,待排序列的長度1<=n<=1000。
函式介面定義:
void InsertSort(SqList L);
其中L是待排序表,使排序後的資料從小到大排列。
型別定義:
typedef int KeyType;
typedef struct {
KeyType *elem; /elem[0]一般作哨兵或緩衝區/
int Length;
}SqList;
裁判測試程式樣例:
#include<stdio.h>
#include<stdlib.h>
typedef int KeyType;
typedef struct {
KeyType *elem; /elem[0]一般作哨兵或緩衝區/
int Length;
}SqList;
void CreatSqList(SqList *L);/待排序列建立,由裁判實現,細節不表/
void InsertSort(SqList L);
int main()
{
SqList L;
int i;
CreatSqList(&L);
InsertSort(L);
for(i=1;i<=L.Length;i++)
{
printf("%d ",L.elem[i]);
}
return 0;
}
/*你的程式碼將被嵌在這裡 */
輸入樣例:
第一行整數表示參與排序的關鍵字個數。第二行是關鍵字值 例如:
10
5 2 4 1 8 9 10 12 3 6
輸出樣例:
輸出由小到大的有序序列,每一個關鍵字之間由空格隔開,最後一個關鍵字後有一個空格。
1 2 3 4 5 6 8 9 10 12
void InsertSort(SqList L){
int i,j;
for(i=1;i<=L.Length;i++){
L.elem[0]=L.elem[i];
for(j=i;L.elem[j-1]>L.elem[0]&&j>0;j--)
L.elem[j]=L.elem[j-1];
L.elem[j]=L.elem[0];
}
}