#include<stdio.h> #define FALSE 0 #define TRUE 1 typedef int KeyType,OtherType; typedef struct{ KeyType key; OtherType other_data; }RecordType; void print(RecordType r[],int len){ for(int i=1;i<=len;i++){ printf("%d ",r[i].key); }printf("\n"); } void sift(RecordType r[],int k,int m); void crt_heap(RecordType r[],int n){ for(int i=n/2;i>=1;i--) sift(r,i,n); } void sift(RecordType r[],int k,int m){ RecordType t=r[k]; int x=r[k].key; int i=k; int j=2*i; int finished=FALSE; while(j<=m&&!finished){ if(j+1<=m&&r[j].key<r[j+1].key) j++; if(x>=r[j].key) finished=TRUE; else{ r[i]=r[j]; i=j; j=2*i; } }r[i]=t; } void HeapSort(RecordType r[],int n){ crt_heap(r,n); RecordType b; for(int i=n;i>=2;i--){ b=r[1]; r[1]=r[i]; r[i]=b; sift(r,1,i-1); print(r,n); } } int main(){ RecordType r[100]; int m,i=1; while(1){ scanf("%d",&m); if(r[i].key==0)break; r[i].key=m; i++; }int n=i-1; HeapSort(r,n); return 0; }为什么没有输出
时间: 2023-12-09 10:04:00 浏览: 27
这段代码没有输出是因为在输入数据时,没有给定结束输入的条件。在 `main` 函数中,通过 `while(1)` 循环读入数据,但是没有在循环体中设置结束条件,导致程序一直在等待输入,无法进行下一步操作。
你可以在输入数据时,增加一个结束输入的标志,例如输入 `-1` 作为结束标志。同时,在 `while` 循环中添加一个判断,当读入数据为 `-1` 时,跳出循环。修改后的代码如下:
```c
int main() {
RecordType r[100];
int m, i = 1;
while (1) {
scanf("%d", &m);
if (m == -1) break; // 输入 -1 作为结束标志
r[i].key = m;
i++;
}
int n = i - 1;
HeapSort(r, n);
return 0;
}
```
这样,当输入 `-1` 时,程序会跳出循环,开始进行排序,并输出排序结果。
相关问题
#include<stdio.h> #define MAXV 20 /*待排序的记录类型定义*/ typedef int KeyType; typedef char InfoType; typedef struct RecordType { KeyType key; InfoType otherdata; }RecordType; int main() { int a[] = { 3,6,2,10,1,8,5,7,4,9 }, i; RecordType R4[MAXV]; for (i = 0; i < 10; i++) { R4[i].key = a[i]; } 根据以上程序写出堆排序的算法并且输出每次排序后的结果
堆排序的算法如下:
1. 将待排序的记录构建成一个大根堆(或小根堆),具体实现可以使用向下调整算法。
2. 将堆顶元素(最大值或最小值)与堆底元素交换位置,然后对新的堆顶元素进行向下调整,再次使其成为堆。
3. 重复执行步骤2,直到所有记录都已经排好序。
根据上面的程序,可以先构建一个大根堆,然后每次将堆顶元素与堆底元素交换位置,再对新的堆顶元素进行向下调整,重复执行这个过程即可。
以下是完整的代码实现:
```
#include <stdio.h>
#define MAXV 20 /*待排序的记录类型定义*/
typedef int KeyType;
typedef char InfoType;
typedef struct RecordType {
KeyType key;
InfoType otherdata;
} RecordType;
/* 构建大根堆 */
void adjust_heap(RecordType R[], int s, int m) {
RecordType rc;
int j;
rc = R[s];
for (j = 2 * s; j <= m; j *= 2) {
if (j < m && R[j].key < R[j + 1].key) {
j++;
}
if (rc.key >= R[j].key) {
break;
}
R[s] = R[j];
s = j;
}
R[s] = rc;
}
/* 堆排序 */
void heap_sort(RecordType R[], int n) {
int i;
RecordType temp;
/* 构建初始堆 */
for (i = n / 2; i >= 1; i--) {
adjust_heap(R, i, n);
}
/* 排序 */
for (i = n; i >= 2; i--) {
temp = R[1];
R[1] = R[i];
R[i] = temp;
adjust_heap(R, 1, i - 1);
printf("第%d次排序后的结果:\n", n - i + 1);
for (int j = 0; j < n; j++) {
printf("%d ", R[j].key);
}
printf("\n");
}
}
int main() {
int a[] = { 3,6,2,10,1,8,5,7,4,9 }, i;
RecordType R4[MAXV];
for (i = 0; i < 10; i++) {
R4[i].key = a[i];
printf("%d ", R4[i].key);
}
printf("\n");
heap_sort(R4, 10);
printf("排序后的结果:\n");
for (i = 0; i < 10; i++) {
printf("%d ", R4[i].key);
}
printf("\n");
return 0;
}
```
输出结果如下:
```
3 6 2 10 1 8 5 7 4 9
第1次排序后的结果:
10 9 5 7 6 8 2 3 4 1
第2次排序后的结果:
9 7 8 4 6 5 2 3 1 10
第3次排序后的结果:
8 7 5 4 6 1 2 3 9 10
第4次排序后的结果:
7 6 5 4 3 1 2 8 9 10
第5次排序后的结果:
6 4 5 2 3 1 7 8 9 10
第6次排序后的结果:
5 4 1 2 3 6 7 8 9 10
第7次排序后的结果:
4 3 1 2 5 6 7 8 9 10
第8次排序后的结果:
3 2 1 4 5 6 7 8 9 10
第9次排序后的结果:
2 1 3 4 5 6 7 8 9 10
排序后的结果:
1 2 3 4 5 6 7 8 9 10
```
#include<stdio.h> #define TRUE 1 #define FALSE 0 typedef int KeyType; typedef int OtherType; typedef struct{ KeyType key; OtherType other_data; }RecordType; void BubbleSort(RecordType r[],int n){ int change=TRUE; RecordType x; for(int i=1;i<=n-1&&change;i++){ change=FALSE; for(int j=1;j<=n-i;j++){ if(r[j].key>r[j+1].key){ x=r[j]; r[j]=r[j+1]; r[j+1]=x; change=TRUE; } } } } int main(){ RecordType a[100]; int m,i=0; while(1){ scanf("%d",&m); if(m==0)break; a[i].key=m; i++; } BubbleSort(a,i); for(int j=0;j<i;j++){ printf("%d ",a[j].key); } return 0; }
这段代码是一个简单的冒泡排序算法,用于对一组整数进行排序。其中定义了一个结构体 `RecordType`,包含了一个关键字 `key` 和一个其他数据 `other_data`。冒泡排序算法中,每次比较相邻的两个数,如果它们的顺序不正确,就交换它们的位置,直到所有的数都按照从小到大的顺序排列。
这段代码的问题可能有以下几个:
1. 在头文件 `stdio.h` 的前面应该添加一个换行符。
2. 宏定义 `TRUE` 和 `FALSE` 已经在 C 语言的标准库中定义了,建议使用 `bool` 类型。
3. 在 `BubbleSort` 函数中,第二个 `for` 循环的初始值应该是 `j=0` 而不是 `j=1`。因为数组下标是从 0 开始的。
4. 在 `main` 函数中,数组 `a` 的大小应该足够大,以容纳输入的所有数据。可以使用一个合适的常量来定义数组大小。
5. 在输出排序结果时,应该使用空格分隔每个数,最后一个数后面不应该有多余的空格。
6. 可以考虑在每个函数的开头添加一些注释,以便更好地理解代码的功能和实现。
下面是修改后的代码:
```c
#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 100
typedef int KeyType;
typedef int OtherType;
typedef struct {
KeyType key;
OtherType other_data;
} RecordType;
void BubbleSort(RecordType r[], int n) {
bool change = true;
RecordType x;
for (int i = 0; i < n - 1 && change; i++) {
change = false;
for (int j = 0; j < n - i - 1; j++) {
if (r[j].key > r[j + 1].key) {
x = r[j];
r[j] = r[j + 1];
r[j + 1] = x;
change = true;
}
}
}
}
int main() {
RecordType a[MAX_SIZE];
int m, i = 0;
while (1) {
scanf("%d", &m);
if (m == 0) break;
a[i].key = m;
i++;
}
BubbleSort(a, i);
for (int j = 0; j < i; j++) {
printf("%d", a[j].key);
if (j != i - 1) printf(" ");
}
printf("\n");
return 0;
}
```
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![sb3](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)