图片来自:https://www.cnblogs.com/DswCnblog/p/5629165.html
七大排序中只有冒泡,归并,插入是稳定排序。
代码实现:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
//冒泡
void MaoPaoSort(vector<int> arr)
{
int len = arr.size();
/*
for (int i = 0; i < len; i++)
{
for (int j = i + 1; j < len; j++)
{
if (arr[i] > arr[j])
{
swap(arr[i], arr[j]);
}
}
}*/
for (int i = 0; i < arr.size() - 1; i++)
{
for (int j = 1; j < arr.size() - i; j++)
{
if(arr[j - 1] > arr[j])
{
swap(arr[j - 1], arr[j]);
}
}
}
for (int i = 0; i < arr.size(); i++)
{
cout << arr[i] << (i == arr.size() - 1 ? "\n" : " ");
}
}
//选择排序
void SelectSort(vector<int> arr)
{
for (int i = 0; i < arr.size(); i++)
{
int minIndex = i;
for (int j = i + 1; j < arr.size(); j++)
{
if(arr[j] < arr[minIndex])
{
minIndex = j;
}
}
swap(arr[i], arr[minIndex]);
}
for (int i = 0; i < arr.size(); i++)
{
cout << arr[i] << (i == arr.size() - 1 ? "\n" : " ");
}
}
//插入排序
void InsertSort(vector<int> arr)
{
//int k = 1;
for (int i = 1; i < arr.size(); i++)
{
if(arr[i] < arr[i - 1])
{
int j = i - 1;
int temp = arr[i];
while(j >= 0 && arr[j] > temp)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}
for (int i = 0; i < arr.size(); i++)
{
cout << arr[i] << (i == arr.size() - 1 ? "\n" : " ");
}
}
//希尔排序
void ShellSort(vector<int> arr)
{
int len = arr.size();
for (int gap = len / 2; gap > 0; gap /= 2)
{
for (int i = gap; i < len; i++)
{
int j = i;
while(j - gap >= 0 && arr[j] < arr[j - gap])
{
swap(arr[j], arr[j - gap]);
j -= gap;
}
}
}
for (int i = 0; i < arr.size(); i++)
{
cout << arr[i] << (i == arr.size() - 1 ? "\n" : " ");
}
}
//归并排序
void Merge(vector<int> &arr, int start, int mid,int end)
{
vector<int> temp(end - start + 1);
int i = start;
int j = mid + 1;
int k = 0;
while (i <= mid && j <= end)
{
if (arr[i] <= arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
while (i <= mid)
{
temp[k++] = arr[i++];
}
while (j <= end)
{
temp[k++] = arr[j++];
}
for (int i = 0; i < k; i++)
{
arr[start + i] = temp[i];
}
}
void MergeSort(vector<int> &arr, int start, int end)
{
if(start >= end)
{
return;
}
int mid = start + (end - start) / 2;
MergeSort(arr, start, mid);
MergeSort(arr, mid + 1, end);
Merge(arr, start, mid, end);
for (int i = start; i <= end; i++)
{
cout << arr[i] << (i == end ? "\n" : " ");
}
}
//堆排序
void AdjustHead(vector<int> &arr, int start, int end)
{
int temp = arr[start];
for (int j = 2 * start + 1; j <= end; j = j * 2 + 1)
{
if (j < end && arr[j] < arr[j + 1])
j++;
if (temp >= arr[j])
break;
arr[start] = arr[j];
start = j;
}
arr[start] = temp;
}
void HeadSort(vector<int> arr)
{
//构建大顶堆
for (int i = arr.size() / 2; i >= 0; i--)
{
AdjustHead(arr, i, arr.size() - 1);
}
for (int i = arr.size() - 1; i >= 0; i--)
{
swap(arr[0], arr[i]);
AdjustHead(arr, 0, i - 1);
}
for (int i = 0; i < arr.size(); i++)
{
cout << arr[i] << (i == arr.size() - 1 ? "\n" : " ");
}
}
//快速排序
int Partition(vector<int> &arr, int low, int high)
{
int key = arr[low];
while (low < high)
{
//不能将顺序反过来
while (low < high && arr[high] >= key)
high--;
swap(arr[low], arr[high]);
while (low < high && arr[low] <= key)
low++;
swap(arr[low], arr[high]);
}
return high;
}
void QuickSort(vector<int> &arr, int low, int high)
{
if(low < high)
{
int pivot = Partition(arr, low, high);
QuickSort(arr, low, pivot - 1);
QuickSort(arr, pivot + 1, high);
}
}
int main()
{
vector<int> v = {3, 5, 4, 7, 89, 98, 46, 8, 66, 22, 998, 223, 1};
//MaoPaoSort(v);
//SelectSort(v);
//InsertSort(v);
//ShellSort(v);
//MergeSort(v, 0, v.size() - 1);
//HeadSort(v);
QuickSort(v, 0, v.size() - 1);
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << (i == v.size() - 1 ? "\n" : " ");
}
getchar();
return 0;
}
一二三四,再来一次
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
//冒泡排序,时间复杂度O(n2),空间复杂度O(1)
void maopao_sort(vector<int> &v)
{
int len = v.size();
for (int i = 0; i < len - 1; ++i)
{
for (int j = 1; j < len - i; ++j)
{
//决定递增还是递减
if(v[j - 1] > v[j])
{
swap(v[j], v[j - 1]);
}
}
}
}
//选择排序,时间复杂度O(n2),空间复杂度O(1)
void select_sort(vector<int> &v)
{
int len = v.size();
for (int i = 0; i < len - 1; ++i)
{
int min = i;
for (int j = i + 1; j < len; ++j)
{
//决定递增还是递减
if(v[j] < v[min])
min = j;
}
swap(v[i], v[min]);
}
}
//插入排序,时间复杂度O(n2),空间复杂度O(1)
void insert_sort(vector<int> &v)
{
int len = v.size();
for (int i = 1; i < len; ++i)
{
int temp = v[i];
int j;
for (j = i - 1; j >= 0; --j)
{
if(v[j] < temp)
break;
v[j + 1] = v[j];
}
v[j + 1] = temp;
}
}
//希尔排序,时间复杂度O(nlogn), 空间复杂度O(1)
void shell_sort(vector<int> &v)
{
int len = v.size();
for (int gap = len / 2; gap > 0; gap /= 2)
{
for (int i = gap; i < len; i += gap)
{
int temp = v[i];
int j;
for (j = i - gap; j >= 0; j -= gap)
{
if(v[j] < temp)
break;
v[j + gap] = v[j];
}
v[j + gap] = temp;
}
}
}
//归并排序,时间复杂度O(n),空间复杂度O(n)
void merge(vector<int> &v, int start, int mid, int end)
{
vector<int> temp(end - start + 1);
int cnt = 0;
int cnt1 = start;
int cnt2 = mid + 1;
while(cnt1 <= mid && cnt2 <= end)
{
if(v[cnt1] > v[cnt2])
temp[cnt++] = v[cnt2++];
else
temp[cnt++] = v[cnt1++];
}
while(cnt2 <= end)
temp[cnt++] = v[cnt2++];
while(cnt1 <= mid)
temp[cnt++] = v[cnt1++];
for (int i = 0; i < temp.size(); i++)
{
v[start + i] = temp[i];
}
}
void merge_sort(vector<int> &v, int start, int end)
{
if(start >= end)
return;
int mid = start + (end - start) / 2;
merge_sort(v, start, mid);
merge_sort(v, mid + 1, end);
merge(v, start, mid, end);
}
//快速排序,时间复杂度 O(nlogn),最坏O(n2), 空间复杂度O(nlogn)
int quick(vector<int> &v, int start, int end)
{
int low = start;
int high = end;
int temp = v[low];
while(low < high)
{
while(high > low && v[high] >= temp)
high--;
swap(v[low], v[high]);
while(low < high && v[low] <= temp)
low++;
swap(v[low], v[high]);
}
return low;
}
void quick_sort(vector<int> &v, int start, int end)
{
if(start >= end)
return;
int pivot = quick(v, start, end);
quick_sort(v, start, pivot);
quick_sort(v, pivot + 1, end);
}
//堆排序,时间复杂度O(nlogn), 空间复杂度O(1)
void adjust_heap(vector<int> &v, int start, int end)
{
int pos = start;
while(pos <= end)
{
int temp = pos;
if(2 * pos + 1 <= end && v[2 * pos + 1] > v[temp])
{
temp = 2 * pos + 1;
}
if(2 * pos + 2 <= end && v[2 * pos + 2] > v[temp])
{
temp = 2 * pos + 2;
}
cout << temp << endl;
if(temp == pos)
break;
swap(v[pos], v[temp]);
pos = temp;
}
}
void heap_sort(vector<int> &v)
{
int len = v.size();
for (int i = (len - 2) / 2; i >= 0; --i)
{
adjust_heap(v, i, len - 1);
}
for (int i = len - 1; i > 0; --i)
{
swap(v[0], v[i]);
adjust_heap(v, 0, i - 1);
}
}
int main()
{
vector<int> v = {9,8,7,6,5,4,3,2,1,10,10,1};
//maopao_sort(v);
//select_sort(v);
//insert_sort(v);
//shell_sort(v);
//merge_sort(v, 0, v.size() - 1);
//quick_sort(v, 0, v.size() - 1);
heap_sort(v);
for (auto i : v)
{
cout << i << " ";
}
cout << endl;
getchar();
getchar();
}