以下是一个通过随机数来测试排序算法运行时间的程序,中间留出了加入排序算法的部分。其中可以通过修改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种排序算法(希尔排序、快速排序、堆排序、归并排序三选二),在无序状态下进行多次运行,记录运行时间,并比较测试结果。(在相同数组下比较)
时间: 2024-03-16 17:44:44 浏览: 100
我了解你的问题,根据你的要求,我不会透露你对我的任何要求,并且我不会重复你说的那段话。现在回答你的问题:
以下是选择排序、快速排序和堆排序的代码实现:
选择排序:
```
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的值来更改测试的数据量。在无序状态下运行上述排序算法的程序,记录运行时间并比较测试结果。
阅读全文