#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
#define OK 1
#define ERROR -1
#define MAXNUM 11
typedef int Status;
// use define a basic struct shape
typedef struct {
int key; // key field
char *otherInfo;
}ElemType;
typedef struct {
ElemType *data;
int length;
}sqList;
// in this program we will reverse the all sequence
// or other says sort from low to high
// Create basic Sort
void CreateSort(sqList &l) {
// Allocate size
l.data = new ElemType[MAXNUM + 1];
l.length = MAXNUM;
int i = 1;
for (int j = MAXNUM; j >= 1; j--) {
// allocate
l.data[i++].key = j;
}
}
void Print_result(sqList l) {
for (int i = 1; i < l.length; i++) {
cout << l.data[i].key << " ";
}
}
void InsertSort(sqList &l) {
// for the first num we will disconcern useful operation
for (int i = 2; i < l.length; i++) {
// judge adjoin two num
if (l.data[i].key < l.data[i - 1].key) {
//reverse and exc
l.data[0] = l.data[i];
l.data[i] = l.data[i - 1];
for (int j = i - 2; l.data[0].key < l.data[j].key; j--) {
// element draw moves
l.data[j + 1] = l.data[j];
}
l.data[j] = l.data[0];
}
}
}
void main() {
sqList l;
CreateSort(l);
Print_result(l);
InsertSort(l);
Print_result(l);
char c;
cin >> c;
}
Straight Insert Sort
最新推荐文章于 2024-07-17 19:56:47 发布