数据结构问题,各种排序(直接插入、折半查找、冒泡排序、哈希查找等)没有错误,但是无法产生正确结果。

如题,各位大神知到错在那里吗?没有错误提示,但是没有正确结果

#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");
}   

 

所有的都不正确吗?

因该先对数组进行排序,然后再进行各种插入啊,否则插入位置怎么找?

排序后,插入函数中的if (R[i].key<R[i-1].key)干啥呢?应该是n<R[i].key,成立时将n插入R[i]的位置,同时i之后的数据全部后移一个位置

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps: 问答会员年卡【8折】购 ,限时加赠IT实体书,即可 享受50次 有问必答服务,了解详情>>>https://t.csdnimg.cn/RW5m