三种排序方法:快速排序、归并排序、选择排序

//快速排序
#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值