(1)编写各种排序算法对测试数据进行排序。基本排序算法共七种:直接插入排序、希尔排序、冒泡排序、快速排序、简单选择排序、堆排序和归并排序。要求至少实现4种排序算法。 (2)在测试数据不少于10000个的情况下比较不同排序算法的时间性能(实际运行时间)。c语言
时间: 2023-08-13 21:03:04 浏览: 80
以下是基本排序算法的C语言实现,包括直接插入排序、希尔排序、冒泡排序、快速排序、简单选择排序、堆排序和归并排序:
1. 直接插入排序
```c
void InsertSort(int arr[], int n) {
int i, j, temp;
for (i = 1; i < n; i++) {
temp = arr[i];
for (j = i; j > 0 && arr[j - 1] > temp; j--)
arr[j] = arr[j - 1];
arr[j] = temp;
}
}
```
2. 希尔排序
```c
void ShellSort(int arr[], int n) {
int i, j, gap, temp;
for (gap = n / 2; gap > 0; gap /= 2) {
for (i = gap; i < n; i++) {
temp = arr[i];
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];
arr[j] = temp;
}
}
}
```
3. 冒泡排序
```c
void BubbleSort(int arr[], int n) {
int i, j, temp;
for (i = n - 1; i > 0; i--) {
for (j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
```
4. 快速排序
```c
void QuickSort(int arr[], int left, int right) {
int i, j, pivot, temp;
if (left < right) {
i = left;
j = right;
pivot = arr[left];
while (i < j) {
while (i < j && arr[j] >= pivot)
j--;
if (i < j) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
while (i < j && arr[i] <= pivot)
i++;
if (i < j) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
arr[i] = pivot;
QuickSort(arr, left, i - 1);
QuickSort(arr, i + 1, right);
}
}
```
5. 简单选择排序
```c
void SelectSort(int arr[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i++) {
min = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min])
min = j;
if (min != i) {
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
}
```
6. 堆排序
```c
void HeapAdjust(int arr[], int i, int n) {
int child, temp;
for (temp = arr[i]; 2 * i + 1 < n; i = child) {
child = 2 * i + 1;
if (child != n - 1 && arr[child] < arr[child + 1])
child++;
if (temp < arr[child])
arr[i] = arr[child];
else
break;
}
arr[i] = temp;
}
void HeapSort(int arr[], int n) {
int i, temp;
for (i = n / 2 - 1; i >= 0; i--)
HeapAdjust(arr, i, n);
for (i = n - 1; i > 0; i--) {
temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
HeapAdjust(arr, 0, i);
}
}
```
7. 归并排序
```c
void Merge(int arr[], int left, int mid, int right, int* temp) {
int i = left, j = mid + 1, k = 0;
while (i <= mid && j <= right) {
if (arr[i] < arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
while (i <= mid)
temp[k++] = arr[i++];
while (j <= right)
temp[k++] = arr[j++];
k--;
while (k >= 0)
arr[left + k] = temp[k--];
}
void MergeSort(int arr[], int left, int right, int* temp) {
if (left < right) {
int mid = (left + right) / 2;
MergeSort(arr, left, mid, temp);
MergeSort(arr, mid + 1, right, temp);
Merge(arr, left, mid, right, temp);
}
}
void MergeSortWrapper(int arr[], int n) {
int* temp = (int*)malloc(sizeof(int) * n);
MergeSort(arr, 0, n - 1, temp);
free(temp);
}
```
对于测试数据不少于10000个的情况下,我们可以使用clock()函数计算排序算法的实际运行时间。完整的测试代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void InsertSort(int arr[], int n);
void ShellSort(int arr[], int n);
void BubbleSort(int arr[], int n);
void QuickSort(int arr[], int left, int right);
void SelectSort(int arr[], int n);
void HeapSort(int arr[], int n);
void MergeSortWrapper(int arr[], int n);
int main() {
int n = 10000;
int i, j, *arr = (int*)malloc(sizeof(int) * n);
clock_t start, end;
for (i = 0; i < n; i++)
arr[i] = rand();
start = clock();
InsertSort(arr, n);
end = clock();
printf("InsertSort: %lf seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
for (i = 0; i < n; i++)
arr[i] = rand();
start = clock();
ShellSort(arr, n);
end = clock();
printf("ShellSort: %lf seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
for (i = 0; i < n; i++)
arr[i] = rand();
start = clock();
BubbleSort(arr, n);
end = clock();
printf("BubbleSort: %lf seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
for (i = 0; i < n; i++)
arr[i] = rand();
start = clock();
QuickSort(arr, 0, n - 1);
end = clock();
printf("QuickSort: %lf seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
for (i = 0; i < n; i++)
arr[i] = rand();
start = clock();
SelectSort(arr, n);
end = clock();
printf("SelectSort: %lf seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
for (i = 0; i < n; i++)
arr[i] = rand();
start = clock();
HeapSort(arr, n);
end = clock();
printf("HeapSort: %lf seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
for (i = 0; i < n; i++)
arr[i] = rand();
start = clock();
MergeSortWrapper(arr, n);
end = clock();
printf("MergeSort: %lf seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
free(arr);
return 0;
}
```
运行结果可能会因为不同的数据集而有所不同,但是大致可以看出各种排序算法的相对性能。
阅读全文