如题,各位大神知到错在那里吗?没有错误提示,但是没有正确结果
#include<stdio.h>
typedef int KeyType;
typedef char InfoType[10];
typedef struct
{ KeyType key;
InfoType data;
}RecType;
void InsertSort(RecType R[], int n) {//直接插入法
int i, j;RecType tmp;
for ( i = 1; i < n; i++) {
if (R[i].key<R[i-1].key)
{tmp=R[i];
j=i-1;
do
{R[j+1]=R[j];
j--;
}while(j>=0 && R[j].key>tmp.key);
R[j+1]=tmp;
}
}
}
void BinInsertSort(RecType R[], int n) {//折半插入法
int i, j, low = 0, high, mid;
RecType tmp;
for (i = 1; i < n; i++) {
if (R[i].key < R[i - 1].key) {
tmp = R[i];low=0;
high = i - 1;
while (low <= high) {
mid = (low + high) / 2;
if (tmp.key < R[mid].key) {
high = mid - 1;
}
else {
low = mid + 1;
}
}
for (j = i - 1; j >= high + 1; j--) {
R[j + 1] = R[j];
}
R[high + 1] = tmp;
}
}
}
void ShellSort(RecType R[], int n) {//希尔排序法
int i, j, d;
RecType tmp;
d = n / 2;
while (d>0) {
for (i = d; i < n; i++) {
tmp = R[i];
j = i - d;
while (j>=0 &&tmp.key<R[j].key) {
R[j + d] = R[j];
j = j - d;
}
R[j + d] = tmp;
}
d = d / 2;
}
}
void Swap(RecType& x, RecType& y) {//交换
RecType tmp = x;
x = y; y = tmp;
}
void BubbleSort(RecType r[], int n) {//冒泡排序
int i, j;
bool exchange;
for ( i = 0; i < n-1; i++) {
exchange = false;
for ( j = n-1; j > i; j--) {
if (r[j].key < r[j - 1].key) {
Swap(r[j], r[j - 1]);
exchange = true;
}
}
if (!exchange) {
return;
}
}
}
int partition(RecType R[], int s, int t) {//快速排序
int i = s, j = t;
RecType tmp = R[i];
while (i < j) {
while (j > i && R[j].key >= tmp.key) {
j--;
}
R[i] = R[j];
while (i < j && R[i].key <= tmp.key) {
i++;
}
R[j] = R[i];
}
R[i] = tmp;
return i;
}
void QuickSortt(RecType R[], int s, int t) {
int i;
if (s < t) {
i = partition(R, s, t);
QuickSortt(R, s, i - 1);
QuickSortt(R, i + 1, t);
}
}
void Select(RecType R[], int t) {//选择排序
for (int s = 0; s < t; s++) {
int min = R[s].key, m = 0;
for (int i = s; i < t; i++) {
if (R[i].key <= min) {
min = R[i].key;
m = i;
}
}
Swap(R[s], R[m]);
}
}
int main()
{
RecType array[12]={3,6,2,10,1,20,88,8,5,7,4,9};
printf("直接插入法:");
InsertSort(array,12);
for (int i = 0; i <12; i++)
{
printf("%d,",array[i]);
}
printf("\n");
printf("折半插入发:");
BinInsertSort(array,12);
for (int i = 0; i < 12; i++)
{
printf("%d,",array[i]);
}
printf("\n");
printf("希尔排序:");
ShellSort(array,12);
for (int i = 0; i < 12; i++)
{
printf("%d,",array[i]);
}
printf("\n");
printf("冒泡排序:");
BubbleSort(array,12);
for (int i = 0; i < 12; i++)
{
printf("%d,",array[i]);
}
printf("\n");
printf("快速排序:");
partition(array,0,12);
for (int i = 0; i < 12; i++)
{
printf("%d,",array[i]);
}
printf("\n");
printf("选择排序:");
Select(array,12);
for (int i = 0; i < 12; i++)
{
printf("%d,",array[i]);
}
printf("\n");
}