随机十个数,分别使用选择排序、希尔排序、快速排序、归并排序和基数排序使数据集有序,写出完整c语言代码
时间: 2023-12-15 16:03:13 浏览: 89
好的,下面是完整的C语言代码,包括生成随机数和各种排序算法的实现:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
void generateRandomArray(int arr[], int size);
void printArray(int arr[], int size);
void selectionSort(int arr[], int size);
void shellSort(int arr[], int size);
void quickSort(int arr[], int left, int right);
void mergeSort(int arr[], int left, int right);
void merge(int arr[], int left, int mid, int right);
void radixSort(int arr[], int size);
int main() {
int arr[MAX];
generateRandomArray(arr, MAX);
printf("Original array:\n");
printArray(arr, MAX);
selectionSort(arr, MAX);
printf("\nAfter selection sort:\n");
printArray(arr, MAX);
generateRandomArray(arr, MAX);
shellSort(arr, MAX);
printf("\nAfter shell sort:\n");
printArray(arr, MAX);
generateRandomArray(arr, MAX);
quickSort(arr, 0, MAX - 1);
printf("\nAfter quick sort:\n");
printArray(arr, MAX);
generateRandomArray(arr, MAX);
mergeSort(arr, 0, MAX - 1);
printf("\nAfter merge sort:\n");
printArray(arr, MAX);
generateRandomArray(arr, MAX);
radixSort(arr, MAX);
printf("\nAfter radix sort:\n");
printArray(arr, MAX);
return 0;
}
void generateRandomArray(int arr[], int size) {
int i;
srand(time(NULL));
for (i = 0; i < size; i++) {
arr[i] = rand() % 100;
}
}
void printArray(int arr[], int size) {
int i;
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}
void selectionSort(int arr[], int size) {
int i, j, temp;
for (i = 0; i < size - 1; i++) {
int minIndex = i;
for (j = i + 1; j < size; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
void shellSort(int arr[], int size) {
int i, j, gap, temp;
for (gap = size / 2; gap > 0; gap /= 2) {
for (i = gap; i < size; i++) {
temp = arr[i];
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = temp;
}
}
}
void quickSort(int arr[], int left, int right) {
int i, j, pivot, temp;
if (left < right) {
pivot = arr[left];
i = left + 1;
j = right;
while (i <= j) {
while (i <= j && arr[i] <= pivot) {
i++;
}
while (i <= j && arr[j] > pivot) {
j--;
}
if (i < j) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[left];
arr[left] = arr[j];
arr[j] = temp;
quickSort(arr, left, j - 1);
quickSort(arr, j + 1, right);
}
}
void mergeSort(int arr[], int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
void merge(int arr[], int left, int mid, int right) {
int temp[MAX];
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++];
}
for (i = left, k = 0; i <= right; i++, k++) {
arr[i] = temp[k];
}
}
void radixSort(int arr[], int size) {
int i, j, k, m, n, p, q, radix = 10, count = 0, bucket[radix], max = arr[0];
for (i = 1; i < size; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
while (max > 0) {
count++;
max /= radix;
}
int temp[size];
for (i = 0, m = 1; i < count; i++, m *= radix) {
for (j = 0; j < radix; j++) {
bucket[j] = 0;
}
for (j = 0; j < size; j++) {
n = (arr[j] / m) % radix;
bucket[n]++;
}
for (j = 1; j < radix; j++) {
bucket[j] += bucket[j - 1];
}
for (j = size - 1; j >= 0; j--) {
n = (arr[j] / m) % radix;
temp[bucket[n] - 1] = arr[j];
bucket[n]--;
}
for (j = 0; j < size; j++) {
arr[j] = temp[j];
}
}
}
```
注意,为了方便展示,我将所有排序算法都放在了同一个文件中。实际使用时,应该将它们分别放在不同的文件中,并在需要使用时进行调用。另外,由于C语言中并没有内置的数组排序函数,所以这些排序算法都是手写的。
阅读全文