10月打工人 2021-06-15 20:20 采纳率: 0%
浏览 51

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

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

#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");
}   
  • 写回答

2条回答 默认 最新

  • CSDN专家-link 2021-06-15 20:21
    关注

    所有的都不正确吗?

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

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

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog