#include <stdio.h>
#include "sortD.h"
#include "sortD.c"
#define N 12
int main() {
RedType d[N]={{0, ' '}, {3,'l'},{6,'e'},{5,'v'},{2,' '},\
{11, '!'}, {9,'o'},{1,'I'},{7,' '},{10, 'u'},\
{4, 'o'}, {8,'y'}};
SqList l1, l2;
for(int i=0; i < N; i++) {
l1.r[i] = d[i];
l2.r[i] = d[i];
}
l1.length = N - 1;
l2.length = N - 1;
printf("Before insert sort...\n");
display(&l1);
BInsertSort(&l1);
printf("After insert sort...\n");
display(&l1);
return 0;
}
#ifndef SORT_SORTD_H
#define SORT_SORTD_H
#define MAXSIZE 30
typedef int KeyType;
typedef char InfoType;
typedef struct{
KeyType key;
InfoType otherinfo;
}RedType;
typedef struct{
RedType r[MAXSIZE + 1];
int length;
}SqList;
int lessthan(RedType *r1, RedType *r2){
return r1->key < r2->key;
}
int lesseqthan(RedType *r1, RedType *r2){
return r1->key <= r2->key;
}
void display(SqList *l);
void InsertSort(SqList *l);
void BInsertSort(SqList *l);
#endif
#include "sortD.h"
#include <stdio.h>
void display(SqList *l){
for(int i=1; i<(l->length+1); i++)
printf("%c", l->r[i].otherinfo);
printf("\n");
}
void InsertSort(SqList *l){
for(int i=2; i <=l->length; i++){
if(lessthan(&(l->r[i]), &(l->r[i-1]))){
l->r[0] = l->r[i];
l->r[i] = l->r[i-1];
int j;
for(j=i - 2; lessthan(&(l->r[0]), &(l->r[j])); j--)
l->r[j + 1] = l->r[j];
l->r[j + 1] = l->r[0];
}
}
}
void BInsertSort(SqList *l){
for(int i=2; i <= l->length; i++){
l->r[0] = l->r[i];
int low = 1;
int high = i;
while(low <= high){
int m = (low + high) / 2;
if(lesseqthan(&(l->r[0]), &(l->r[m])))
high = m - 1;
else
low = m + 1;
}
for(int j=i - 1; j>=high + 1; j--)l->r[j+1] = l->r[j];
l->r[high + 1] = l->r[0];
}
}