用C++,先输入产生随机数的个数,进行随机数产生的输出,分别用简单选择排序、直接插人排序、冒泡排序、快速排序、两路合并排序以及堆排序进行排序,进行排序结果的输出,若待排序关键字个数为500、10 000、50 000、100 000时,输出每种方法完成排序所需要的时间单位:毫秒)。
时间: 2024-03-22 21:38:52 浏览: 53
C/C++排序算法实现,冒泡排序、简单选择排序、直接插入排序、希尔排序、堆排序、归并排序、快速排序
好的,这是一个计算机科学方面的问题,我会尽力帮你解答。
首先,我们可以用以下的代码来产生指定个数的随机数并输出:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
int n;
cout << "请输入随机数的个数:";
cin >> n;
srand(time(NULL));
for (int i = 0; i < n; i++) {
cout << rand() << " ";
}
cout << endl;
return 0;
}
```
接着,我们可以分别用以下代码来实现简单选择排序、直接插入排序、冒泡排序、快速排序、两路合并排序以及堆排序:
- 简单选择排序:
```c++
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
if (minIdx != i) {
swap(arr[i], arr[minIdx]);
}
}
}
```
- 直接插入排序:
```c++
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int temp = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > temp) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}
```
- 冒泡排序:
```c++
void bubbleSort(int arr[], int n) {
bool flag = true;
for (int i = 0; i < n - 1 && flag; i++) {
flag = false;
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
flag = true;
}
}
}
}
```
- 快速排序:
```c++
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return i + 1;
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivotPos = partition(arr, low, high);
quickSort(arr, low, pivotPos - 1);
quickSort(arr, pivotPos + 1, high);
}
}
```
- 两路合并排序:
```c++
void merge(int arr[], int left, int mid, int right) {
int len1 = mid - left + 1;
int len2 = right - mid;
int *temp1 = new int[len1];
int *temp2 = new int[len2];
for (int i = 0; i < len1; i++) {
temp1[i] = arr[left + i];
}
for (int j = 0; j < len2; j++) {
temp2[j] = arr[mid + 1 + j];
}
int i = 0, j = 0, k = left;
while (i < len1 && j < len2) {
if (temp1[i] <= temp2[j]) {
arr[k++] = temp1[i++];
} else {
arr[k++] = temp2[j++];
}
}
while (i < len1) {
arr[k++] = temp1[i++];
}
while (j < len2) {
arr[k++] = temp2[j++];
}
delete[] temp1;
delete[] temp2;
}
void mergeSort(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
```
- 堆排序:
```c++
void heapify(int arr[], int n, int i) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest]) {
largest = l;
}
if (r < n && arr[r] > arr[largest]) {
largest = r;
}
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for (int i = n - 1; i >= 0; i--) {
swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}
```
最后,我们可以用以下代码来测试每种排序方法对于不同规模的数据所需的时间:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void selectionSort(int arr[], int n) {
// 简单选择排序
// ...
}
void insertionSort(int arr[], int n) {
// 直接插入排序
// ...
}
void bubbleSort(int arr[], int n) {
// 冒泡排序
// ...
}
int partition(int arr[], int low, int high) {
// 快速排序
// ...
}
void quickSort(int arr[], int low, int high) {
// 快速排序
// ...
}
void merge(int arr[], int left, int mid, int right) {
// 两路合并排序
// ...
}
void mergeSort(int arr[], int left, int right) {
// 两路合并排序
// ...
}
void heapify(int arr[], int n, int i) {
// 堆排序
// ...
}
void heapSort(int arr[], int n) {
// 堆排序
// ...
}
int main() {
int n[] = {500, 10000, 50000, 100000};
for (int i = 0; i < 4; i++) {
int *arr = new int[n[i]];
srand(time(NULL));
for (int j = 0; j < n[i]; j++) {
arr[j] = rand();
}
clock_t start, end;
start = clock();
selectionSort(arr, n[i]);
end = clock();
cout << "简单选择排序: " << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms" << endl;
start = clock();
insertionSort(arr, n[i]);
end = clock();
cout << "直接插入排序: " << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms" << endl;
start = clock();
bubbleSort(arr, n[i]);
end = clock();
cout << "冒泡排序: " << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms" << endl;
start = clock();
quickSort(arr, 0, n[i] - 1);
end = clock();
cout << "快速排序: " << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms" << endl;
start = clock();
mergeSort(arr, 0, n[i] - 1);
end = clock();
cout << "两路合并排序: " << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms" << endl;
start = clock();
heapSort(arr, n[i]);
end = clock();
cout << "堆排序: " << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms" << endl;
delete[] arr;
}
return 0;
}
```
希望这能够对你有所帮助!
阅读全文