首先,对于各个排序算法的原理本文不做讲解,可以参考以下两个博客:
https://blog.csdn.net/weixin_40205234/article/details/86699088
https://blog.csdn.net/Adusts/article/details/80882649
下面直接上代码:
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <set>
#include<algorithm>
#include <limits.h>
#include<iomanip>
#include <unordered_set>
#include <assert.h>
#include <math.h>
#include <unordered_map>
using namespace std;
/************二分查找算法*****************/
class Solution {
public:
int searchInsert(int A[], int n, int target) {
int l, r, mid;
l = 0;
r = n - 1;
while (l <= r) {
mid = l + (r - l) / 2;
if (A[mid] == target) {
return mid;
}
else if (A[mid] > target) {
r = mid - 1;
}
else {
l = mid + 1;
}
}
return l;
}
};
/**********************快速排序算法*****************************/
int quick_sort(int *a,int low,int high) // 时间复杂度(O(N*logN))
{
int left = low, right = high;
if (left >= right)
return 0;
int pivot = a[low];
int tmp = 0;
while (left != right){
while ((a[right] >= a[low]) && (left<right))
right--;
while ((a[left] <= a[low]) && (left<right))
left++;
if ((left<right)){
tmp = a[left];
a[left] = a[right];
a[right] = tmp;
}
}
tmp = a[low];
a[low] = a[right];
a[right] = tmp;
quick_sort(a, low, right - 1);
quick_sort(a, right + 1, high);
return 0;
}
int main()
{
int a[9] = {2,3,1,4,5,6,7,0,9};
int low = 0, high = 8;
quick_sort(a,low,high);
for (int i = 0; i < 9;i++)
cout << a[i] << endl;
}
/**********************归并排序算法*****************************/
/**
* 归并排序:C++ 时间复杂度(O(N*logN))
*
*
* 将一个数组中的两个相邻有序区间合并成一个
*
* 参数说明:
* a -- 包含两个有序区间的数组
* start --