续写下列代码 #include <iostream> #include <stdio.h> #include <malloc.h> #define EQ(a,b) ((a)==(b)) #define LT(a,b) ((a)<(b)) #define LQ(a,b) ((a)<=(b)) typedef int Elemtype; typedef int KeyType; using namespace std; typedef struct { KeyType key; } SElemtype; typedef struct { SElemtype *elem; int length; } SStable; void creatSStable(SStable &T) { int i; // cout <<"请输入顺序表的长度:"; cin >>T.length; // cout<<"请输入顺序表的数据:" ; T.elem=(SElemtype*)malloc((T.length+1)*sizeof(KeyType)); for (i=1; i<=T.length; i++) cin >>T.elem[i].key; } /////////////////////////3)实现直接插入排序算法。 void InsertSort (SStable &L) {
时间: 2024-04-29 14:20:13 浏览: 85
#include <malloc.h>
4星 · 用户满意度95%
int i, j;
for (i = 2; i <= L.length; i++) {
if (LT(L.elem[i].key, L.elem[i - 1].key)) {
L.elem[0] = L.elem[i];
L.elem[i] = L.elem[i - 1];
for (j = i - 2; LT(L.elem[0].key, L.elem[j].key); j--) {
L.elem[j + 1] = L.elem[j];
}
L.elem[j + 1] = L.elem[0];
}
}
}
int main() {
SStable T;
creatSStable(T);
InsertSort(T);
cout << "排序后的结果为:" << endl;
for (int i = 1; i <= T.length; i++) {
cout << T.elem[i].key << " ";
}
cout << endl;
return 0;
}
阅读全文