一.快速排序
#include <bits/stdc++.h>
using namespace std;
// 分区函数
int partition(std::vector<int>& arr, int low, int high) {
int i = low;
int j = high;
int target = arr[i];
while(i<j){
while(i<j&&arr[j]>=target){
j--;
}
if(i<j){
swap(arr[i],arr[j]);
i++;
}
while(i<j&&arr[i]<target){
i++;
}
if(i<j){
swap(arr[i],arr[j]);
j--;
}
}
return i;
}
// 快速排序函数
void quickSort(std::vector<int>& arr, int low, int high) {
if(low<high){
int part = partition(arr,low,high);
quickSort(arr,low,part-1);
quickSort(arr,part+1,high);
}
}
int main() {
std::vector<int> arr = {10, 7, 8, 9, 1, 5};
int n = arr.size();
quickSort(arr, 0, n - 1);
std::cout << "Sorted array: ";
for (int i = 0; i < n; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}
二.归并排序
#include <iostream>
#include <vector>
// 合并两个子数组
void merge(std::vector<int>& arr, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
// 创建临时数组
std::vector<int> leftArr(n1), rightArr(n2);
// 拷贝数据到临时数组
for (int i = 0; i < n1; ++i)
leftArr[i] = arr[left + i];
for (int j = 0; j < n2; ++j)
rightArr[j] = arr[mid + 1 + j];
// 合并临时数组回到原数组
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArr[i] <= rightArr[j]) {
arr[k] = leftArr[i];
++i;
} else {
arr[k] = rightArr[j];
++j;
}
++k;
}
// 拷贝剩余元素(如果有)
while (i < n1) {
arr[k] = leftArr[i];
++i;
++k;
}
while (j < n2) {
arr[k] = rightArr[j];
++j;
++k;
}
}
// 归并排序函数
void mergeSort(std::vector<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);
}
}
int main() {
std::vector<int> arr = {12, 11, 13, 5, 6, 7};
int arr_size = arr.size();
std::cout << "Given array is \n";
for (int i = 0; i < arr_size; i++)
std::cout << arr[i] << " ";
std::cout << std::endl;
mergeSort(arr, 0, arr_size - 1);
std::cout << "Sorted array is \n";
for (int i = 0; i < arr_size; i++)
std::cout << arr[i] << " ";
std::cout << std::endl;
return 0;
}
三.桶排序(基数排序)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
void jishu(vector<int>& nums) {
int n = nums.size();
//如果数组元素个数小于 2,则返回 0
if(n < 2)
{
return;
}
vector<int> buf(n); //buf为临时顺组,用于存储每次排完序的数组
int maxVal = *max_element(nums.begin(), nums.end());
int time = maxBit(maxVal); //计算出需要最高位数,即需要排多少次
int dev = 1;
//开始从低位到高位基数排序
for(int i = 0; i < time; i++){
vector<vector<int>> count(10); //桶
//统计每个桶中有多少个数
for(int j = 0; j < n; j++){
int digit = (nums[j] / dev) % 10; //digit 为nums[j]的第i位数
count[digit].emplace_back(nums[j]);
}
int index = n-1;
for(int number = 9;number>=0;number--){
while(count[number].size()>0){
nums[index] = count[number].back();
count[number].pop_back();
index--;
}
}
dev *= 10;
}
}
int maxBit(int maxVal){
int p = 10;
int d = 1;
while(maxVal >= p){
p *= 10;
d++;
}
return d;
}
};
int main() {
std::vector<int> arr = {12, 11, 13, 5, 6, 7};
int arr_size = arr.size();
std::cout << "Given array is \n";
for (int i = 0; i < arr_size; i++)
std::cout << arr[i] << " ";
std::cout << std::endl;
Solution a;
cout<<"jishu"<<endl;
a.jishu(arr);
std::cout << "Sorted array is \n";
for (int i = 0; i < arr_size; i++)
std::cout << arr[i] << " ";
std::cout << std::endl;
return 0;
}