首页 > 其他分享 >直接插入排序

直接插入排序

时间:2022-12-23 21:23:28浏览次数:34  
标签:typedef int 插入排序 elem InsertSort SqList 直接 KeyType

本题要求实现直接插入排序函数,待排序列的长度1<=n<=1000。

函数接口定义:

1 void  InsertSort(SqList L);

其中L是待排序表,使排序后的数据从小到大排列。
###类型定义:

1 typedef  int  KeyType;
2 typedef  struct {                      
3   KeyType *elem; /*elem[0]一般作哨兵或缓冲区*/                       
4   int Length;      
5 }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;
}
/*你的代码将被嵌在这里 */

代码如下:

void  InsertSort(SqList L){
    for(int i=2;i<=L.Length;i++){
        int p=i;
        L.elem[0]=L.elem[p--];
        while(p>0&&L.elem[p]>L.elem[0]){
            L.elem[p+1]=L.elem[p];
            p--;
        }
        L.elem[p+1]=L.elem[0];
    }
}

 

标签:typedef,int,插入排序,elem,InsertSort,SqList,直接,KeyType
From: https://www.cnblogs.com/psh888/p/17001653.html

相关文章