//快速排序
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <ctime>
using namespace std;
int partition(int arr[], int low, int high) {
int pivot = arr[low];
while (low < high)
{
while(high > low && arr[high] > pivot)
high--;//如果high比基准大,往前移动
arr[low] = arr[high];//如果high比基准小,则换到low的位置
while (high > low && arr[low] < pivot)
low++;
arr[high] = arr[low];//如果low比基准大,则换到high的位置
}
arr[low] = pivot;
return low;
}
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);
}
}
int main() {
srand(time(0)); // 初始化随机数生成器
int arr[10001];
int n;
cout << "(快速排序)请输入要排序的数据的个数: " << endl;
cin >> n;
// 检查输入并生成不同长度的随机数组
if (n == 5 || n == 10 || n == 100) {
for (int i = 0; i < n; i++) {
arr[i] = rand() % 1000000; // 生成随机数(0-999999)
}
} else {
cout << "不支持此数组长度。请使用 5, 10, 或 100。" << endl;
return 1;
}
cout << "生成的随机数组为: " << endl;
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
int high = n - 1;
QuickSort(arr, 0, high);
cout << "排序后的序列为: " << endl;
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
//选择排序
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <ctime>
using namespace std;
void SelectSort(int arr[],int begin,int end){
for(int i=0;i<end;i++){
for(int j=i+1;j<=end;j++){
if(arr[j]<arr[i]){
int temp = arr[i];
arr[i] = arr[j];
arr[j]=temp;
}
}
}
}
int main() {
srand(time(0)); // 初始化随机数生成器
int arr[100001];
int n;
cout << "(选择排序)请输入要排序的数据的个数: " << endl;
cin >> n;
// 检查输入并生成不同长度的随机数组
if (n == 5 || n == 10 || n == 100) {
for (int i = 0; i < n; i++) {
arr[i] = rand() % 1000000; // 生成随机数(0-999999)
}
} else {
cout << "不支持此数组长度。请使用 5, 10, 或 100。" << endl;
return 1;
}
cout << "生成的随机数组为: " << endl;
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
int end = n - 1;
SelectSort(arr, 0, end);
cout << "排序后的序列为: " << endl;
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
//归并排序
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void merge(int arr[],int temp[],int low,int mid,int high){
int i = low;//左分治数组开始的下标
int j =mid+1;//右分治数组开始的下标
int k = low;//temp数组开始下标
while(i<mid+1 && j<high+1){//到达最右边,即后方无元素时
if(arr[i]< arr[j])
temp[k++]=arr[i++];//存放数组的同时移动角标
if(arr[i]>arr[j])
temp[k++]=arr[j++];
}
while(i<mid+1)
temp[k++]=arr[i++];
while(j<high+1)
temp[k++]=arr[j++];//把剩下的一个挪到temp中
for (int i=low;i<=high;i++)
arr[i]=temp[i];//将排序好的数组放回上层
}
void MergeSort(int arr[],int temp[],int low,int high){
int mid = (low+high)/2;
if(low<high){
MergeSort(arr,temp,low,mid);//递归将左半边数组分成一个一个
MergeSort(arr,temp,mid+1,high);//递归将右半边数组分成一个一个
merge(arr,temp,low,mid,high);//排序
}
}
int main() {
srand(time(0)); // 初始化随机数生成器
int arr[100001];
int temp[100001];
int n;
cout << "(归并排序)请输入要排序的数据的个数: " << endl;
cin >> n;
// 检查输入并生成不同长度的随机数组
if (n == 5 || n == 10 || n == 100) {
for (int i = 0; i < n; i++) {
arr[i] = rand() % 1000000; // 生成随机数(0-999999)
}
} else {
cout << "不支持此数组长度。请使用 5, 10, 或 100。" << endl;
return 1;
}
cout << "生成的随机数组为: " << endl;
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
int high = n - 1;
MergeSort(arr, temp, 0, high);
cout << "排序后的序列为: " << endl;
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}