用c语言实现随机产生300个整数构成的序列,,分别以直接插入,冒泡,希尔,快速,归并等排序算法排序,并统计各自的比较次数
时间: 2024-05-06 10:16:33 浏览: 53
编程实现希尔、快速、堆、归并排序算法,随机产生10000个随机数存入磁盘文件,读取,并将不同排序方法的结果存入文件中(含报告)
5星 · 资源好评率100%
好的,下面是我为您提供的代码实现。代码中使用了不同的排序算法,并统计了每个算法的比较次数。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void generateRandomArray(int *arr, int n);
void printArray(int *arr, int n);
int insertionSort(int *arr, int n);
int bubbleSort(int *arr, int n);
int shellSort(int *arr, int n);
int quickSort(int *arr, int low, int high);
int mergeSort(int *arr, int low, int high);
int merge(int *arr, int low, int mid, int high);
int main()
{
int arr[300];
int n = sizeof(arr) / sizeof(arr[0]);
int comparisons;
// generate random array
generateRandomArray(arr, n);
// sort using insertion sort
int *insertionArr = malloc(n * sizeof(int));
for (int i = 0; i < n; i++)
{
insertionArr[i] = arr[i];
}
comparisons = insertionSort(insertionArr, n);
printf("Insertion Sort:\n");
printf("Comparisons: %d\n", comparisons);
printArray(insertionArr, n);
free(insertionArr);
// sort using bubble sort
int *bubbleArr = malloc(n * sizeof(int));
for (int i = 0; i < n; i++)
{
bubbleArr[i] = arr[i];
}
comparisons = bubbleSort(bubbleArr, n);
printf("Bubble Sort:\n");
printf("Comparisons: %d\n", comparisons);
printArray(bubbleArr, n);
free(bubbleArr);
// sort using shell sort
int *shellArr = malloc(n * sizeof(int));
for (int i = 0; i < n; i++)
{
shellArr[i] = arr[i];
}
comparisons = shellSort(shellArr, n);
printf("Shell Sort:\n");
printf("Comparisons: %d\n", comparisons);
printArray(shellArr, n);
free(shellArr);
// sort using quick sort
int *quickArr = malloc(n * sizeof(int));
for (int i = 0; i < n; i++)
{
quickArr[i] = arr[i];
}
comparisons = quickSort(quickArr, 0, n - 1);
printf("Quick Sort:\n");
printf("Comparisons: %d\n", comparisons);
printArray(quickArr, n);
free(quickArr);
// sort using merge sort
int *mergeArr = malloc(n * sizeof(int));
for (int i = 0; i < n; i++)
{
mergeArr[i] = arr[i];
}
comparisons = mergeSort(mergeArr, 0, n - 1);
printf("Merge Sort:\n");
printf("Comparisons: %d\n", comparisons);
printArray(mergeArr, n);
free(mergeArr);
return 0;
}
void generateRandomArray(int *arr, int n)
{
srand(time(NULL));
for (int i = 0; i < n; i++)
{
arr[i] = rand() % 1000;
}
}
void printArray(int *arr, int n)
{
printf("Sorted Array: ");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int insertionSort(int *arr, int n)
{
int i, j, key, comparisons = 0;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
comparisons++;
}
arr[j + 1] = key;
}
return comparisons;
}
int bubbleSort(int *arr, int n)
{
int i, j, temp, comparisons = 0;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
comparisons++;
}
}
}
return comparisons;
}
int shellSort(int *arr, int n)
{
int i, j, temp, gap, comparisons = 0;
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];
comparisons++;
}
arr[j] = temp;
}
}
return comparisons;
}
int quickSort(int *arr, int low, int high)
{
int comparisons = 0;
if (low < 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;
comparisons++;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
comparisons++;
int pi = i + 1;
comparisons += quickSort(arr, low, pi - 1);
comparisons += quickSort(arr, pi + 1, high);
}
return comparisons;
}
int mergeSort(int *arr, int low, int high)
{
int comparisons = 0;
if (low < high)
{
int mid = low + (high - low) / 2;
comparisons += mergeSort(arr, low, mid);
comparisons += mergeSort(arr, mid + 1, high);
comparisons += merge(arr, low, mid, high);
}
return comparisons;
}
int merge(int *arr, int low, int mid, int high)
{
int i, j, k, comparisons = 0;
int n1 = mid - low + 1;
int n2 = high - mid;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
{
L[i] = arr[low + i];
}
for (j = 0; j < n2; j++)
{
R[j] = arr[mid + 1 + j];
}
i = 0;
j = 0;
k = low;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
comparisons++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
return comparisons;
}
```
代码中使用了 `generateRandomArray()` 函数来生成一个包含300个随机整数的数组。然后,使用 `insertionSort()`,`bubbleSort()`,`shellSort()`,`quickSort()` 和 `mergeSort()` 函数对该数组进行排序,并统计比较次数。最后,使用 `printArray()` 函数打印排序后的数组。
注意,为了防止指针错误,我们使用 `malloc()` 函数动态分配内存来存储排序后的数组,并在使用完后使用 `free()` 函数释放内存。
阅读全文