typedef struct box { int a[max]; int n; int begin; int end; }box; void fast(struct box *a) { int i=a->begin; int j=a->end; int temp; int t; temp=a->a[a->begin]; if(i>j) { return ; } else { while(i!=j) { while(a->a[j]>=temp&&i<j) { j--; } while(a->a[i]<=temp&&i<j) { i++; } if(j>i) { t=a->a[j]; a->a[j]=a->a[i]; a->a[i]=t; } } a->a[a->begin]=a->a[i]; a->a[i]=temp; a->end=i-1; fast(a); a->end=a->n-1; a->begin=i+1; fast(a); } } int halfsearch(int a[1000],int y,int p) { int mid; int end=0; while(end<=y) { mid=(end+y)/2; if(a[mid]>a[p]) { y=mid-1; } else if(a[mid]<a[p]) { end=mid+1; } else { return 1; } } return 0; } int main() { box *a; int m,flag; a->begin=0; printf("输入排序元素的个数\n"); scanf("%d",&a->n); printf("输入相关需排序的元素\n"); for(int i=0;i<a->n;i++) { scanf("%d",&a->a[i]); } a->end=a->n-1; fast(a); printf("排序结果为:\n"); for(int i=0;i<a->n;i++) { printf("%d ",a->a[i]); } return 0; }有什么问题
时间: 2024-02-14 19:08:39 浏览: 20
在这段修正后的代码中,仍然存在一些问题:
1. 在 `main` 函数中,没有为指针变量 `a` 分配内存空间。应该使用 `malloc` 函数为其分配空间,例如 `box *a = (box *)malloc(sizeof(box));`。
2. 在 `halfsearch` 函数中,应该判断 `end` 是否大于 `y`,如果是则说明查找失败,应该返回 0。
3. 在排序结束后,应该将 `begin` 和 `end` 恢复到初始状态,即 `begin=0`,`end=n-1`。
4. 在使用 `a` 指针时,应该使用 `->` 运算符,例如 `a->begin`。
5. 在 `main` 函数结束前,应该使用 `free` 函数释放 `a` 指针所指向的内存空间,以免出现内存泄漏。
建议对代码进行修正,以免出现错误。
相关问题
typedef struct{int n}PER 中的PER是什么
PER并不是一个变量或者类型,而是一个结构体标记(tag),用于标识这个结构体的名称。
在这个例子中,typedef struct { int n } PER; 定义了一个结构体,其中包含了一个int类型的成员变量n,并且给这个结构体起了一个别名PER。这样以后就可以通过PER来代替struct { int n }这个结构体的名称。
例如,可以这样声明一个PER类型的变量:
```c
PER myper;
```
这个声明语句等价于:
```c
struct { int n } myper;
```
但是使用PER类型可以更加简洁明了。
typedef struct { int edges[max][max]; int n; }MatGraph;
这段代码定义了一个邻接矩阵表示的图的数据结构,其中:
- `max` 是一个常量,表示图的最大顶点数;
- `edges` 是一个二维数组,用于存储边的信息,`edges[i][j]` 表示从顶点 i 到顶点 j 是否有边,如果有则为 1,否则为 0;
- `n` 是一个整数,表示图的当前顶点数。
可以通过操作 `edges` 数组来实现图的遍历、最短路径、最小生成树等算法。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)
![](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)