说明
1、稳定排序:如果 a 原本在 b 的前面,且 a == b,排序之后 a 仍然在 b 的前面,则为稳定排序。
2、非稳定排序:如果 a 原本在 b 的前面,且 a == b,排序之后 a 可能不在 b 的前面,则为非稳定排序。
3、原地排序:原地排序指在排序过程中不申请多余的存储空间,只利用原来存储待排数据的存储空间进行比较和交换的数据排序。
4、非原地排序:需要利用额外的数组来辅助排序。
5、时间复杂度:一个算法执行所消耗的时间。
6、空间复杂度:运行完一个算法所需的内存大小。
性能分析
直接贴代码片:
#include<iostream>
#include<vector>
#include<stdlib.h>
#include<stack>
#include <algorithm>
using namespace std;
void BubbleSort(int* a,int n)
{
int end = n;
int exchange = 0;
while(end)
{
for(int i=1;i<end;++i)
{
if(a[i-1] > a[i])
{
swap(a[i-1],a[i]);
exchange = 1;
}
}
if(0 == exchange) break;
--end;
}
}
void quickSort1(int *a,int left,int right)
{
int i = left,j = right;
if(left>right) return;
while(i<j){
while(i<j && a[j]>=a[left])
j--;
while(i<j && a[i]<=a[left])
i++;
if(i<j){
swap(a[i],a[j]);
}
}
swap(a[i],a[left]);
quickSort1(a,left,i-1);
quickSort1(a,i+1,right);
}
void quickSort2(int *nums,int begin,int end)
{
if(begin >= end) return;
int first = begin,last = end;
int key = nums[first];
while(first<last){
while(first<last && nums[last]>=key)
last--;
if(first < last)
nums[first++] = nums[last];
while(first<last && nums[first]<=key)
first++;
if(first < last)
nums[last--] = nums[first];
}
nums[first] = key;
quickSort2(nums,begin,first-1);
quickSort2(nums,first+1,end);
}
int Partition(int *a,int low,int high)
{
if(low>=high) return -1;
int pivot = a[low];
while(low<high){
while(low<high && a[high]>=pivot)
high--;
if(low<high)
a[low++] = a[high];
while(low<high && a[low]<=pivot)
low++;
if(low<high)
a[high--] = a[low];
}
a[low] = pivot;
return low;
}
void quickSort3(int *a,int left,int right)
{
if(left >= right) return;
stack<int> s;
int mid = Partition(a,left,right);
if(mid-1 > left){
s.push(left);
s.push(mid-1);
}
if(mid+1 < right){
s.push(mid+1);
s.push(right);
}
while(!s.empty()){
int r = s.top();
s.pop();
int l = s.top();
s.pop();
mid = Partition(a,l,r);
if(mid-1 > l){
s.push(l);
s.push(mid-1);
}
if(mid+1 < r){
s.push(mid+1);
s.push(r);
}
}
}
vector<int> insertSort(vector<int>& nums)
{
int n = nums.size();
for(int i=1;i<n;i++){
int flag = nums[i];
int j = i - 1;
while(j>=0 && nums[j]>flag){
nums[j+1] = nums[j];
j--;
}
nums[j+1] = flag;
}
return nums;
}
vector<int> shellSort(vector<int>& nums)
{
int n = nums.size();
int h = 1;
while(h<n/3)
h = 3*h+1;
while(h>0)
{
for(int i=h;i<n;i++)
{
int j = i-h;
int flag = nums[i];
while(j>=0 && flag<nums[j])
{
nums[j+h] = nums[j];
j = j-h;
}
nums[j+h] = flag;
}
h = h/3;
}
return nums;
}
void selectSort(int *a,int n)
{
int begin = 0;
int end = n-1;
while (begin < end)
{
int min = begin,max = begin;
for(int i=begin;i<=end;++i)
{
if(a[min] > a[i])
min = i;
if(a[max] < a[i])
max = i;
}
swap(a[min],a[begin]);
if(max == begin)
max = min;
swap(a[max],a[end]);
begin++;
end--;
}
}
void AdjustDown(vector<int>& a, int root, int n)
{
int parent = root;
int child = parent*2+1;
while( child < n ){
if( (child+1) < n && a[child+1] > a[child] ){
++child;
}
if( a[child] > a[parent] ){
swap(a[child],a[parent]);
parent = child;
child = parent*2+1;
}
else
break;
}
}
void heapSort(vector<int>& a, int n)
{
for( int i = n/2-1; i >=0 ; i-- )
AdjustDown(a,i,n);
int end = n - 1;
while( end > 0 ){
swap(a[0],a[end]);
AdjustDown(a,0,end);
end--;
}
}
void Merge(int* a, int left, int mid, int right)
{
int *temp = new int[right - left + 1];
int st1 = left,st2 = mid + 1;
int t = 0;
while (st1 <= mid && st2 <= right)
temp[t++] = a[st1] < a[st2] ? a[st1++] : a[st2++];
while (st1 <= mid)
temp[t++] = a[st1++];
while (st2 <= right)
temp[t++] = a[st2++];
for (int j = 0; j < t; ++j)
a[left + j] = temp[j];
delete[] temp;
}
void mergeSort(int* a, int start, int end){
if (a==NULL || start>=end)
return;
int mid = (start + end) >> 1;
mergeSort(a, start, mid);
mergeSort(a, mid + 1, end);
Merge(a, start, mid, end);
}
void bucketSort(vector<int>& shit){
int max = 0,min = 0,n = shit.size();
for(int n:shit){
if(max < n) max = n;
if(min > n) min = n;
}
int gap = (max-min) / n + 1;
int count = (max-min) / gap +1;
vector<int> buckets[count];
for(int n:shit){
int index = n/gap;
buckets[index].push_back(n);
}
int index = 0;
for(auto bucket:buckets){
sort(bucket.begin(),bucket.end());
for(int m:bucket){
shit[index++] = m;
}
}
}
int main()
{
int a[12] = {1,2,4,5,11,33,6,77,8,1000,5,12};
int left = 0, right = 11;
cout << "before sort:" << endl;
vector<int> shit(a,a+12);
for(int v:shit)
cout << v << " ";
cout << endl;
cout << "after sort:" << endl;
for(int v:shit)
cout << v << " ";
cout << endl;
return 0;
}
|