六个经典排序算法
最近在开始看算法书,从最经典的排序算法开始,用C++实现了六个经典的排序算法,记录下来,以便日后翻阅。
- 时间复杂度O(N*N)
- 冒泡排序
- 选择排序
- 插入排序
- 时间复杂度O(NlgN)
Show me the Code
#include <iostream>
#include <vector>
using namespace std;
class Sort{
public:
vector<int> A;
Sort(vector<int> A): A(A){}
void display(vector<int> &A){
for(auto c:A) cout<<c;
cout<<endl;
}
// Bubble Sort
void bubble_sort(vector<int> &A) {
// If the vector is empty
if(A.empty()) return ;
// If the vector is not empty
else{
for(int i=0; i<A.size()-1; ++i){
for(int j=0; j<A.size()-1-i; ++j){
if(A[j] > A[j+1]){
swap(A[j], A[j+1]);
}
}
}
}
}
// Selection Sort
void selection_sort(vector<int> &A) {
// If the vector is empty
if(A.empty()) return ;
// If the vector is not empty
else{
for(int i=0; i<A.size()-1; ++i){
int min_index = i;
for(int j=i+1; j<A.size(); ++j){
// Get the index of the min value
if(A[j] < A[min_index]){
min_index = j;
}
}
swap(A[i], A[min_index]);
}
}
}
// Insertion Sort
void insertion_sort(vector<int> &A) {
// If the vector is empty
if(A.empty()) return ;
// If the vector is not empty
else{
for(int j=1; j<A.size(); ++j){
int key = A[j]; // waiting for sort
// start from the previous one of the key
int i = j-1;
while(i>=0 && key<A[i]){
A[i+1] = A[i]; // move one by one
--i;
}
A[i+1] = key; // insert
}
}
}
// Quik sort
void quik_sort(vector<int> &A, int left, int right){
if(left>=right) return ;
int i = left, j = right;
int pivot = A[left];
while(i<j){
while(i<j && A[j]>=pivot) j--;
swap(A[i], A[j]);
while(i<j && A[i]<=pivot) i++;
swap(A[i], A[j]);
}
A[i] = pivot;
quik_sort(A, left, i-1);
quik_sort(A, i+1, right);
}
// Merge sort
void merge_sort(vector<int> &A, vector<int> &Reg, int left, int right){
if(left >= right) return ;
int len = right-left;
int mid = left + (len>>1);
int left1 = left, right1 = mid;
int left2 = mid+1, right2 = right;
merge_sort(A, Reg, left1, right1);
merge_sort(A, Reg, left2, right2);
int k = left;
while(left1<=right1 && left2<=right2){
Reg[k++] = (A[left1]<A[left2] ? A[left1++] : A[left2++]);
}
while(left1<=right1){
Reg[k++] = A[left1++];
}
while(left2<=right2){
Reg[k++] = A[left2++];
}
for(k = left; k<=right; k++){
A[k] = Reg[k];
}
}
// Heap Sort
void max_heapify(vector<int> &A, int start, int end){
int dad = start;
int son = dad * 2 + 1;
while(son <= end){
if(son+1 <= end && A[son]<A[son+1]) son++;
if(A[dad] >= A[son]) break;
else{
swap(A[dad], A[son]);
dad = son;
son = dad * 2 + 1;
}
}
}
void heap_sort(vector<int> &A){
for(int i = A.size()/2-1; i>=0; --i){
// start from the last dad
max_heapify(A, i, A.size()-1);
}
for(int i=A.size()-1; i>0; --i){
swap(A[0], A[i]);// swap the max and the back
max_heapify(A, 0, i-1);
}
}
};
int main() {
vector<int> A{3, 5, 1, 4, 2};
Sort sort_test(A);
cout<<"Origin: "<<endl;
sort_test.display(A);
cout<<"After bubble sort: "<<endl;
vector<int> bubble_temp(A);
sort_test.bubble_sort(bubble_temp);
sort_test.display(bubble_temp);
cout<<"After selection sort: "<<endl;
vector<int> selection_temp(A);
sort_test.selection_sort(selection_temp);
sort_test.display(selection_temp);
cout<<"After insertion sort: "<<endl;
vector<int> insertion_temp(A);
sort_test.insertion_sort(insertion_temp);
sort_test.display(insertion_temp);
cout<<"After quik sort: "<<endl;
vector<int> quik_temp(A);
sort_test.quik_sort(quik_temp, 0, quik_temp.size()-1);
sort_test.display(quik_temp);
cout<<"After merge sort: "<<endl;
vector<int> merge_temp(A), merge_reg(A.size());
sort_test.merge_sort(merge_temp, merge_reg, 0, merge_temp.size()-1);
sort_test.display(merge_temp);
cout<<"After heap sort: "<<endl;
vector<int> heap_temp(A);
sort_test.heap_sort(heap_temp);
sort_test.display(heap_temp);
return 0;
}