随机产生300个整数构成的序列,分别以直接插入,冒泡,希尔,快速,归并等排序算法排序,并统计各自的比较次数,并用c语言实现
时间: 2023-09-22 19:07:36 浏览: 95
编程实现希尔、快速、堆、归并排序算法,随机产生10000个随机数存入磁盘文件,读取,并将不同排序方法的结果存入文件中(含报告)
5星 · 资源好评率100%
以下是C语言实现的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 直接插入排序
int InsertionSort(int arr[], int n) {
int cmpCount = 0;
for (int i = 1; i < n; i++) {
int temp = arr[i], j = i - 1;
while (j >= 0 && arr[j] > temp) {
arr[j + 1] = arr[j];
j--;
cmpCount++;
}
arr[j + 1] = temp;
}
return cmpCount;
}
// 冒泡排序
int BubbleSort(int arr[], int n) {
int cmpCount = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
cmpCount++;
}
}
}
return cmpCount;
}
// 希尔排序
int ShellSort(int arr[], int n) {
int cmpCount = 0;
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i++) {
int temp = arr[i], j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
cmpCount++;
}
arr[j] = temp;
}
}
return cmpCount;
}
// 快速排序
int Partition(int arr[], int left, int right, int* cmpCount) {
int pivot = arr[right], i = left - 1;
for (int j = left; j < right; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
(*cmpCount)++;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[right];
arr[right] = temp;
(*cmpCount)++;
return i + 1;
}
void QuickSort(int arr[], int left, int right, int* cmpCount) {
if (left < right) {
int pivotIndex = Partition(arr, left, right, cmpCount);
QuickSort(arr, left, pivotIndex - 1, cmpCount);
QuickSort(arr, pivotIndex + 1, right, cmpCount);
}
}
// 归并排序
void Merge(int arr[], int left, int mid, int right, int* cmpCount) {
int n1 = mid - left + 1, n2 = right - mid;
int* L = (int*)malloc(n1 * sizeof(int));
int* R = (int*)malloc(n2 * sizeof(int));
for (int i = 0; i < n1; i++) {
L[i] = arr[left + i];
}
for (int j = 0; j < n2; j++) {
R[j] = arr[mid + 1 + j];
}
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
}
(*cmpCount)++;
}
while (i < n1) {
arr[k++] = L[i++];
(*cmpCount)++;
}
while (j < n2) {
arr[k++] = R[j++];
(*cmpCount)++;
}
free(L);
free(R);
}
void MergeSort(int arr[], int left, int right, int* cmpCount) {
if (left < right) {
int mid = (left + right) / 2;
MergeSort(arr, left, mid, cmpCount);
MergeSort(arr, mid + 1, right, cmpCount);
Merge(arr, left, mid, right, cmpCount);
}
}
int main() {
srand(time(NULL));
int n = 300;
int arr[n];
for (int i = 0; i < n; i++) {
arr[i] = rand();
}
int cmpCount;
// 直接插入排序
int insertionArr[n];
for (int i = 0; i < n; i++) {
insertionArr[i] = arr[i];
}
cmpCount = InsertionSort(insertionArr, n);
printf("直接插入排序的比较次数:%d\n", cmpCount);
// 冒泡排序
int bubbleArr[n];
for (int i = 0; i < n; i++) {
bubbleArr[i] = arr[i];
}
cmpCount = BubbleSort(bubbleArr, n);
printf("冒泡排序的比较次数:%d\n", cmpCount);
// 希尔排序
int shellArr[n];
for (int i = 0; i < n; i++) {
shellArr[i] = arr[i];
}
cmpCount = ShellSort(shellArr, n);
printf("希尔排序的比较次数:%d\n", cmpCount);
// 快速排序
int quickArr[n];
for (int i = 0; i < n; i++) {
quickArr[i] = arr[i];
}
cmpCount = 0;
QuickSort(quickArr, 0, n - 1, &cmpCount);
printf("快速排序的比较次数:%d\n", cmpCount);
// 归并排序
int mergeArr[n];
for (int i = 0; i < n; i++) {
mergeArr[i] = arr[i];
}
cmpCount = 0;
MergeSort(mergeArr, 0, n - 1, &cmpCount);
printf("归并排序的比较次数:%d\n", cmpCount);
return 0;
}
```
阅读全文