标签:
//
// sxh.c
// TIAOSHI
//
// Created by 就不告诉你我是谁 on 15-7-10.
// Copyright (c) 2015年 刘勋. All rights reserved.
//
#include <stdio.h>
typedef int KeyType;
typedef struct {
KeyType key;
// othertype other_data
}RecordType;
void output(RecordType a[],int b,int len){//依次输出从a数组以下标b开始的len个数据
for (int i=b; i<b+len; i++) {
printf("%4d",a[i].key);
}
printf("\n");
}
void InSort(RecordType r[],int length){ //注意:此r数组的使用范围是从1到length
//对记录数组r做直接插入排序,length为数组中待排序记录的数目
int j;
for (int i=2; i<=length; i++) {
r[0]=r[i]; j=i-1; //将待插入记录放到监视哨r[0]中
while (r[0].key<r[j].key) { //寻找插入位置
r[j+1]=r[j]; j=j-1; }
r[j+1]=r[0];//将待插入的记录插入到已排序的记录序列中
}
}
void InSort1(RecordType r[],int length){ //注意:此r数组的使用范围从0到length-1
//对记录数组r做直接插入排序,length为数组中待排序记录的数目
int j; RecordType t;
for (int i=1; i<length; i++) { //将待插入记录放到中间变量t中
t=r[i]; j=i-1;
while (t.key<r[j].key&&j>=0) {
r[j+1]=r[j];j=j-1;
}
r[j+1]=t;
}
}
//
// sxh.h
// TIAOSHI
//
// Created by 就不告诉你我是谁 on 15-7-10.
// Copyright (c) 2015年 刘勋. All rights reserved.
//
#ifndef TIAOSHI_sxh_h
#define TIAOSHI_sxh_h
typedef int KeyType;
typedef struct {
KeyType key;
// othertype other_data
}RecordType;
void output(RecordType a[],int b,int len);
void InSort(RecordType r[],int length);
void InSort1(RecordType r[],int length);
#endif
最后在main.c的主函数中包含头文件 代码如下://
// main.c
// TIAOSHI
//
// Created by 就不告诉你我是谁 on 15-7-10.
// Copyright (c) 2015年 刘勋. All rights reserved.
//
#include <stdio.h>
#include "order1.h"
int main(int argc, const char * argv[])
{
RecordType a[9]={0,48,62,35,77,55,14,35,98};
RecordType b[8]={48,62,35,77,55,14,35,98};
output(a, 1, 8);
output(b, 0, 8);
InSort(a, 8);
output(a, 1, 8);
InSort1(b, 8);
output(b, 0, 8);
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/u013087513/article/details/46841717