#include <stdio.h> #include <stdlib.h> void selectionSort(int arr[], int n) { int i, j, min_idx; for (i = 0; i < n - 1; i++) { min_idx = i; for (j = i + 1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; int temp = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = temp; } } int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = (low - 1); for (int j = low; j <= high - 1; j++) { if (arr[j] < pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return (i + 1); } void quickSort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } void printArray(int arr[], int n) { for (int i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); } int main() { int arr[100]; for (int i = 0; i < 100; i++) arr[i] = rand() % 1000 + 1; printf("Original array:\n"); printArray(arr, 100); selectionSort(arr, 100); printf("Array sorted using selection sort:\n"); printArray(arr, 100); quickSort(arr, 0, 99); printf("Array sorted using quick sort:\n"); printArray(arr, 100); return 0; }无法运行如上程序,修改使其可以正确运行
时间: 2023-06-26 13:07:40 浏览: 114
问题在于程序中包含了中文字符“Original array”和“Array sorted using selection sort”等,需要将其改为英文字符或者使用Unicode编码表示。同时,头文件和函数之间应该有空格隔开,否则会出现编译错误。下面是修改后的代码:
```
#include <stdio.h>
#include <stdlib.h>
void selectionSort(int arr[], int n) {
int i, j, min_idx;
for (i = 0; i < n - 1; i++) {
min_idx = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[100];
for (int i = 0; i < 100; i++)
arr[i] = rand() % 1000 + 1;
printf("Original array:\n");
printArray(arr, 100);
selectionSort(arr, 100);
printf("Array sorted using selection sort:\n");
printArray(arr, 100);
quickSort(arr, 0, 99);
printf("Array sorted using quick sort:\n");
printArray(arr, 100);
return 0;
}
```
阅读全文