#include<stdio.h> void sort(int arr[],int n){ int min; for(int i=0;i<n-1;i++){ min=i; for(int j=i+1;j<n;j++){ if(arr[min]>arr[j]){ min=j; } swap(&arr[i],&arr[min]); } printf("step %d: ",i+1); for(int i=0;i<n;i++){ printf("%d ",arr[i]); } printf("\n"); } } void swap(int *a,int *b){ int t=*a; *a=*b; *b=t; } int main() { int n; int x; int arr[100000]; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&arr[i]); } sort(arr,n); printf("sorted array: "); for(int i=0;i<n;i++){ printf("%d ",arr[i]); } return 0; }此代码有什么错误
时间: 2023-07-15 09:13:46 浏览: 59
这个代码似乎没有语法错误,但是存在逻辑错误。在选择排序中,交换操作应该在内层循环结束后执行,而你的代码中交换操作是在内层循环中执行的。你需要将交换操作移到外层循环的末尾,以保证正确的排序顺序。
另外,你的代码没有对输入的数组长度进行判断,如果输入的数组长度超过了定义的最大值100000,则会导致程序崩溃。你可以使用动态内存分配来解决这个问题。
相关问题
改进以下代码#include<stdio.h> #include<stdlib.h> #include<malloc.h> #define ar arr[]={12,21,2,11,10,8} #define ELEM_TYPE int int ar; //顺序表的创建: typedef struct Sqlist { ELEM_TYPE *data; int length; int SIZE; }Sqlist,*PSqlist; //顺序表的初始化: void Init_Sqlist(PSqlist L) { L->data=arr; L->length=0; L->SIZE=sizeof(arr)/sizeof(arr[0]); } //打印: void Show(PSqlist L) { for(int i=L->length;i<L->SIZE;i++) { printf("%d ",L->data[i]); } printf("\n\n"); } //.选择排序: void SelectSort(PSqlist L) { for(int i=L->length;i<L->SIZE;i++) { int j=i+1; bool tag=true; int min=i; for(j;j<L->SIZE;j++) { tag=true; if(L->data[min]>L->data[j]) { tag=false; min=j; } } if(min!=i) { int tmp=L->data[min]; L->data[min]=L->data[i]; L->data[i]=tmp; } if(!tag && min==i)//当没有发生交换时,说明这时已经有序,直接退出即可 { break; } } } //<6>.主函数: int main() { Sqlist head; Init_Sqlist(&head); printf("初始数据为:"); Show(&head); printf("选择排序后的数据为:"); SelectSort(&head); Show(&head); }
这段代码存在一些问题,我给您改进一下:
```
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100 // 定义最大长度
typedef struct {
int *data; // 动态数组存储数据元素
int length; // 顺序表当前长度
} SqList;
// 顺序表的创建
void Init_SqList(SqList *L, int arr[], int n) {
L->data = (int *)malloc(sizeof(int) * MAXSIZE); // 动态分配内存
L->length = n;
for (int i = 0; i < n; i++) {
L->data[i] = arr[i];
}
}
// 打印顺序表
void Show(SqList L) {
for (int i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
}
// 选择排序
void SelectSort(SqList *L) {
int i, j, min, tmp;
for (i = 0; i < L->length - 1; i++) {
min = i;
for (j = i + 1; j < L->length; j++) {
if (L->data[min] > L->data[j]) {
min = j; // 记录最小值的下标
}
}
if (min != i) {
tmp = L->data[min];
L->data[min] = L->data[i];
L->data[i] = tmp;
} else {
break;
}
}
}
// 主函数
int main() {
int arr[] = {12, 21, 2, 11, 10, 8};
int n = sizeof(arr) / sizeof(arr[0]);
SqList L;
Init_SqList(&L, arr, n);
printf("初始数据为:");
Show(L);
SelectSort(&L);
printf("选择排序后的数据为:");
Show(L);
return 0;
}
```
改进的地方:
1. 定义了一个 `MAXSIZE`,用于限制顺序表的最大长度,并且使用 `malloc()` 动态分配内存。
2. 修改了 `Init_Sqlist()` 函数,使其可以接受一个数组和数组长度作为参数,用于初始化顺序表。
3. 修改了 `Show()` 函数,使其可以接受一个 `SqList` 类型的参数,用于打印顺序表。
4. 修改了 `SelectSort()` 函数,使其使用更加常见的选择排序算法,并且使用更加清晰的变量名。
5. 修改了主函数中的调用方式,使其符合修改后的函数定义。
以下是一个通过随机数来测试排序算法运行时间的程序,中间留出了加入排序算法的部分。其中可以通过修改RANDNUM的值来更改测试的数据量: #include <stdio.h> #include <stdlib.h> #include <time.h> #define RANDNUM 10000 //随机数的个数 void main() { int iRandNum[RANDNUM];//存放随机数 clock_t first,second; //记录开始和结束时间(以毫秒为单位) int i; for(i=0;i<RANDNUM;i++) {//产生1万个随机数 iRandNum[i]=rand()%RANDNUM; } first=clock(); //开始时间 //此处加入排序程序 second=clock();//结束时间 //显示排序算法所用的时间 } (2) 从选择、交换、插入排序算法中任选至少3种排序算法(希尔排序、快速排序、堆排序、归并排序三选二),在无序状态下进行多次运行,记录运行时间,并比较测试结果。(在相同数组下比较)
我了解你的问题,根据你的要求,我不会透露你对我的任何要求,并且我不会重复你说的那段话。现在回答你的问题:
以下是选择排序、快速排序和堆排序的代码实现:
选择排序:
```
void selectionSort(int arr[], int n) {
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++) {
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]);
}
}
```
快速排序:
```
void quickSort(int arr[], int low, int high) {
if (low < high) {
// pi is partitioning index, arr[p] is now
// at right place
int pi = partition(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high - 1; j++) {
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot) {
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
```
堆排序:
```
void heapSort(int arr[], int n) {
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// One by one extract an element from heap
for (int i = n - 1; i >= 0; i--) {
// Move current root to end
swap(&arr[0], &arr[i]);
// call max heapify on the reduced heap
heapify(arr, i, 0);
}
}
void heapify(int arr[], int n, int i) {
int largest = i; // Initialize largest as root
int l = 2 * i + 1; // left = 2*i + 1
int r = 2 * i + 2; // right = 2*i + 2
// If left child is larger than root
if (l < n && arr[l] > arr[largest])
largest = l;
// If right child is larger than largest so far
if (r < n && arr[r] > arr[largest])
largest = r;
// If largest is not root
if (largest != i) {
swap(&arr[i], &arr[largest]);
// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}
```
以下是主函数的代码实现:
```
int main() {
int arr[RANDNUM];
clock_t first, second;
int i;
// Generate 10000 random numbers
for (i = 0; i < RANDNUM; i++) {
arr[i] = rand() % RANDNUM;
}
// Selection sort
first = clock();
selectionSort(arr, RANDNUM);
second = clock();
printf("Selection sort time: %lf ms\n", (double)(second - first) / CLOCKS_PER_SEC * 1000);
// Quick sort
first = clock();
quickSort(arr, 0, RANDNUM - 1);
second = clock();
printf("Quick sort time: %lf ms\n", (double)(second - first) / CLOCKS_PER_SEC * 1000);
// Heap sort
first = clock();
heapSort(arr, RANDNUM);
second = clock();
printf("Heap sort time: %lf ms\n", (double)(second - first) / CLOCKS_PER_SEC * 1000);
return 0;
}
```
你可以根据需要修改RANDNUM的值来更改测试的数据量。在无序状态下运行上述排序算法的程序,记录运行时间并比较测试结果。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.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)