创建输出排序表函数,取得系统时间函数,直接插入排序函数,冒泡排序函数,快速排序函数,直接选择排序函数,筛选函数,对排序函数,归并排序函数。c++
时间: 2024-02-06 17:10:15 浏览: 66
以下是 C++ 代码实现:
```
#include <iostream>
#include <ctime>
using namespace std;
// 创建输出排序表函数
void output(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
// 取得系统时间函数
void getTime() {
time_t now = time(0);
char* dt = ctime(&now);
cout << "当前时间为: " << dt << endl;
}
// 直接插入排序函数
void insertSort(int arr[], int n) {
int i, j, temp;
for (i = 1; i < n; i++) {
temp = arr[i];
for (j = i - 1; j >= 0 && arr[j] > temp; j--) {
arr[j + 1] = arr[j];
}
arr[j + 1] = temp;
}
}
// 冒泡排序函数
void bubbleSort(int arr[], int n) {
int i, j, temp;
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;
}
}
}
}
// 快速排序函数
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
int temp;
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
// 直接选择排序函数
void selectSort(int arr[], int n) {
int i, j, minIndex, temp;
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
// 筛选函数
void sift(int arr[], int low, int high) {
int i = low;
int j = 2 * i + 1;
int temp = arr[i];
while (j <= high) {
if (j < high && arr[j] < arr[j + 1]) {
j++;
}
if (temp < arr[j]) {
arr[i] = arr[j];
i = j;
j = 2 * i + 1;
} else {
break;
}
}
arr[i] = temp;
}
void heapSort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--) {
sift(arr, i, n - 1);
}
for (int i = n - 1; i >= 1; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
sift(arr, 0, i - 1);
}
}
// 归并排序函数
void merge(int arr[], int low, int mid, int high) {
int len = high - low + 1;
int* temp = new int[len];
int i = low;
int j = mid + 1;
int k = 0;
while (i <= mid && j <= high) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= high) {
temp[k++] = arr[j++];
}
for (int i = 0; i < len; i++) {
arr[low + i] = temp[i];
}
}
void mergeSort(int arr[], int low, int high) {
if (low < high) {
int mid = (low + high) / 2;
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);
merge(arr, low, mid, high);
}
}
int main() {
int arr[] = { 3, 2, 5, 1, 4 };
int n = sizeof(arr) / sizeof(int);
cout << "排序前:";
output(arr, n);
// 直接插入排序
insertSort(arr, n);
cout << "直接插入排序后:";
output(arr, n);
// 冒泡排序
bubbleSort(arr, n);
cout << "冒泡排序后:";
output(arr, n);
// 快速排序
quickSort(arr, 0, n - 1);
cout << "快速排序后:";
output(arr, n);
// 直接选择排序
selectSort(arr, n);
cout << "直接选择排序后:";
output(arr, n);
// 筛选函数
heapSort(arr, n);
cout << "堆排序后:";
output(arr, n);
// 归并排序
mergeSort(arr, 0, n - 1);
cout << "归并排序后:";
output(arr, n);
// 取得系统时间
getTime();
return 0;
}
```
阅读全文