#include<bits/stdc++.h>
using namespace std;voidquick_sort(vector<int>&q,int l,int r){if(l >= r)return;int i = l -1, j = r +1, x = q[l + r >>1];while(i < j){do i ++;while(q[i]< x);do j --;while(q[j]> x);if(i < j){swap(q[i], q[j]);}else{quick_sort(q, l, j),quick_sort(q, j +1, r);}}}intmain(){int n, t;
cin >> n;
vector<int>q;for(int i =0; i < n; i ++){
cin >> t;
q.push_back(t);}quick_sort(q,0, q.size()-1);for(int x : q) cout << x <<' ';
cout << endl;return0;}
冒泡
#include<bits/stdc++.h>
using namespace std;voidbubble_sort(vector<int>&q){for(int i = q.size()-1; i >0; i --){
bool isSwap = false;for(int j =1; j <= i; j ++){if(q[j -1]> q[j]){
isSwap = true;swap(q[j -1], q[j]);}if(!isSwap)break;}}}intmain(){int n, t;
cin >> n;
vector<int>q;for(int i =0; i < n; i ++){
cin >> t;
q.push_back(t);}bubble_sort(q);for(int x : q) cout << x <<' ';
cout << endl;return0;}
选择
#include<bits/stdc++.h>
using namespace std;voidselect_sort(vector<int>&q){for(int i =0; i < q.size(); i ++){for(int j = i +1; j < q.size(); j ++){if(q[i]> q[j]){swap(q[i], q[j]);}}}}intmain(){int n, t;
cin >> n;
vector<int>q;for(int i =0; i < n; i ++){
cin >> t;
q.push_back(t);}select_sort(q);for(int x : q) cout << x <<' ';
cout << endl;return0;}
插入
#include<bits/stdc++.h>
using namespace std;voidinsert_sort(vector<int>&q){for(int i =1; i < q.size(); i ++){int t = q[i], j;for(j = i -1; j >=0; j --){if(q[j]> t){
q[j +1]= q[j];}else{break;}}
q[j +1]= t;}}intmain(){int n, t;
cin >> n;
vector<int>q;for(int i =0; i < n; i ++){
cin >> t;
q.push_back(t);}insert_sort(q);for(int x : q) cout << x <<' ';
cout << endl;return0;}
归并
#include<bits/stdc++.h>
using namespace std;voidmerge_sort(vector<int>&q,int l,int r){if(l >= r)return;int mid = l + r >>1;merge_sort(q, l, mid);merge_sort(q, mid +1, r);static vector<int>w;
w.clear();int i = l, j = mid +1;while(i <= mid && j <= r){if(q[i]< q[j]){
w.push_back(q[i ++]);}else{
w.push_back(q[j ++]);}}while(i <= mid) w.push_back(q[i ++]);while(j <= r) w.push_back(q[j ++]);for(int i =0, j = l; i < w.size(); i ++) q[j ++]= w[i];}intmain(){int n, t;
cin >> n;
vector<int>q;for(int i =0; i < n; i ++){
cin >> t;
q.push_back(t);}merge_sort(q,0, q.size()-1);for(int x : q) cout << x <<' ';
cout << endl;return0;}
快排#include <bits/stdc++.h>using namespace std;void quick_sort(vector<int>&q, int l, int r) { if(l >= r) return; int i = l - 1, j = r + 1, x = q[l + r >> 1]; while(i < j) { do i ++; while(q[i] < x