用dev-c++实现排序综合采用插入排序、希尔排序、起泡排序、快速排序、选择排序、堆排序、归并排序)方法实现
时间: 2023-08-01 21:07:09 浏览: 107
常用c++排序算法(包括插入排序、冒泡排序、选择排序、快速排序、归并排序、希尔排序等)
好的,我理解了你的要求。以下是使用 Dev-C++ 实现七种排序算法的示例代码。
```c++
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_SIZE 1000000
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void insertSort(int arr[], int n)
{
for (int i = 1; i < n; i++)
{
int j = i - 1;
int temp = arr[i];
while (j >= 0 && temp < arr[j])
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}
void shellSort(int arr[], int n)
{
int gap = n / 2;
while (gap > 0)
{
for (int i = gap; i < n; i++)
{
int j = i - gap;
int temp = arr[i];
while (j >= 0 && temp < arr[j])
{
arr[j + gap] = arr[j];
j -= gap;
}
arr[j + gap] = temp;
}
gap /= 2;
}
}
void bubbleSort(int arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
swap(&arr[j], &arr[j + 1]);
}
}
}
}
void quickSort(int arr[], int left, int right)
{
if (left >= right)
{
return;
}
int i = left, j = right, pivot = arr[left];
while (i < j)
{
while (i < j && arr[j] >= pivot)
{
j--;
}
if (i < j)
{
arr[i++] = arr[j];
}
while (i < j && arr[i] < pivot)
{
i++;
}
if (i < j)
{
arr[j--] = arr[i];
}
}
arr[i] = pivot;
quickSort(arr, left, i - 1);
quickSort(arr, i + 1, right);
}
void selectSort(int arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
int minIndex = i;
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[minIndex])
{
minIndex = j;
}
}
if (minIndex != i)
{
swap(&arr[i], &arr[minIndex]);
}
}
}
void adjustHeap(int arr[], int i, int n)
{
int temp = arr[i];
for (int j = i * 2 + 1; j < n; j = j * 2 + 1)
{
if (j + 1 < n && arr[j + 1] > arr[j])
{
j++;
}
if (arr[j] > temp)
{
arr[i] = arr[j];
i = j;
}
else
{
break;
}
}
arr[i] = temp;
}
void heapSort(int arr[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
{
adjustHeap(arr, i, n);
}
for (int i = n - 1; i > 0; i--)
{
swap(&arr[0], &arr[i]);
adjustHeap(arr, 0, i);
}
}
void merge(int arr[], int left, int mid, int right)
{
int *temp = (int *)malloc((right - left + 1) * sizeof(int));
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 (int p = 0; p < k; p++)
{
arr[left + p] = temp[p];
}
free(temp);
}
void mergeSort(int arr[], int left, int right)
{
if (left >= right)
{
return;
}
int mid = (left + right) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
int main()
{
int arr[MAX_SIZE];
int n;
printf("请输入数组大小:");
scanf("%d", &n);
printf("请输入数组元素:");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
clock_t start, end;
double duration;
start = clock();
insertSort(arr, n);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("插入排序结果:");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n插入排序用时:%f秒\n", duration);
start = clock();
shellSort(arr, n);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("希尔排序结果:");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n希尔排序用时:%f秒\n", duration);
start = clock();
bubbleSort(arr, n);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("冒泡排序结果:");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n冒泡排序用时:%f秒\n", duration);
start = clock();
quickSort(arr, 0, n - 1);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("快速排序结果:");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n快速排序用时:%f秒\n", duration);
start = clock();
selectSort(arr, n);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("选择排序结果:");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n选择排序用时:%f秒\n", duration);
start = clock();
heapSort(arr, n);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("堆排序结果:");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n堆排序用时:%f秒\n", duration);
start = clock();
mergeSort(arr, 0, n - 1);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("归并排序结果:");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n归并排序用时:%f秒\n", duration);
return 0;
}
```
你可以将以上代码复制到 Dev-C++ 编辑器中,然后运行该程序即可。程序会提示输入数组大小和数组元素,并输出每种排序算法的结果和用时。
阅读全文