#include <stdio.h>
#include <stdlib.h>
Include “PriorityQueue.c”
typedef int KeyType;
typedef int DataType;
typedef struct
{ KeyType key; /* 排序码字段 */
DataType info; /* 记录的其它字段 */
}RecordNode;
typedef struct
{ int n; /* n 为文件中的记录个数*/
RecordNode *record;
}SortObject;
/*直接插入排序*/
void insertSort(SortObject * pvector) { /* 按递增序进行直接插入排序
*/
int i, j;
RecordNode temp;
for( i = 1; i < pvector->n; i++ ) { /* 依次插入记录 R
1
, R
2
…R
n-1
*/
temp = pvector->record[i]; j = i-1;
while ((temp.key < pvector->record[j].key)&&(j>=0) ){ /* 由后向前找插入位置 */
pvector->record[j+1] = pvector->record[j]; j--; }/* 将排序码大于 k
i
的记录后移 */
if( j!=(i-1) ) pvector->record[j+1] = temp;
}
}
/*二分法插入排序*/
void binSort(SortObject * pvector) { /* 按递增序进行二分法插入排序 */
int i, j, left, mid, right;
RecordNode temp;
for( i = 1; i < pvector->n; i++ ) {
temp = pvector->record[i]; left = 0; right = i – 1;/* 置已排序区间的下、上界初值 */
while (left <= right) {
mid=(left+right)/2; /* mid 指向已排序区间的中间位置 */
if (temp.key<pvector->record[mid].key)right=mid-1; /* 插入元素应在左子区间 */
else left=mid+1; /* 插入元素应在右子区间 */
}
for(j=i-1; j>=left; j--) pvector->record[j+1] = pvector->record[j];
/* 将排序码大的记录后移 */
if(left!=i) pvector->record[left] = temp;