插入排序

来源:互联网 发布:mac转换铃声 编辑:程序博客网 时间:2024/06/07 09:41

这个应该很基础......

head.h

#include<iostream>using namespace std;#define MAXSIZE 6#define LT(a,b) a<btypedef int KeyType;typedef int InfoType;typedef struct{KeyType key;InfoType otherinfo;}RedType;typedef struct{RedType r[MAXSIZE+1];int length;}SqList;void InsertSort(SqList &L);void ShowList(SqList &L);

function.cpp

#include"head.h"#include<iostream>using namespace std;void InsertSort(SqList &L){for(int i=2;i<=L.length;i++){if(LT(L.r[i].key,L.r[i-1].key)){L.r[0].key=L.r[i].key;L.r[i].key=L.r[i-1].key;for(int j=i-2;LT(L.r[0].key,L.r[j].key);j--){L.r[j+1].key=L.r[j].key;}L.r[j+1].key=L.r[0].key;}}}void ShowList(SqList &L){for(int i=1;i<=L.length;i++)cout<<L.r[i].key<<"  ";}

main.cpp

#include"head.h"#include<iostream>using namespace std;void main(){SqList L;int n;cout<<"请输入一串数的个数:"<<endl;cin>>n;cout<<"输入这"<<n<<"个数:"<<endl;for(int i=1;i<=n;i++){cin>>L.r[i].key;}L.length=n;InsertSort(L);ShowList(L);}


0 0
原创粉丝点击