讲讲排序(C++描述)
生活随笔
收集整理的這篇文章主要介紹了
讲讲排序(C++描述)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
排序和查找一直是算法的主流問題。
排序方法有很多種:
1.簡單排序(O(n^2))
#include <iostream> #include <algorithm> using namespace std;void SimpleSort(int *a,int n) {for (int i = 0; i < n - 1; ++i) {for (int j = i + 1; j < n; ++j) {if (a[i] > a[j]) {int temp = a[i];a[i] = a[j];a[j] = temp; } }} }int main(){int a[20],n;cin >> n;for (int i = 0; i < n && i < 20; ++i) {cin >> a[i];}SimpleSort(a,min(n,20));for (int i = 0; i < n && i < 20; ++i) {cout << a[i]<<" ";}cout << endl; }
簡單排序是實現就上面這些。(看起來很上的那一堆,主要是用了方便檢驗)至于調用了min,所以才會有那個include 那個algorithm,對了,稍微懂點的,或者還沒有忘掉的哥們,應該還是很清楚這個庫里面就包含了sort函數,這個就是用快排實現的。
2.冒泡排序 O(n^ 2)運氣好可以是O(n)
#include <iostream> #include <algorithm> using namespace std;void BubbleSort(int *a,int n){for (int j = 0; j < n - 1; ++j) {for (int i = 0; i < n - 1 - j; ++i) {if (a[i] > a[i + 1]) {int temp = a[i];a[i] = a[i + 1];a[i + 1] = temp;}}} }int main(){int a[20],n;cin >> n;for (int i = 0; i < n && i < 20; ++i) {cin >> a[i];}BubbleSort(a,min(n,20));for (int i = 0; i < n && i < 20; ++i) {cout << a[i]<<" ";}cout << endl; }
也不難理解,就是將每一個大小順序的點排到一個排的地方去,第一次是排最大的那個。依次類推。
由于前兩個比較可以使得在a[1]放的是a[0] a[1]中比較大的那個。
再來就是就是a[2]放的是a[1] a[2]中比較大的那個。由于a[1]又是a[0] a[1]中最大的那個,也就確保了a[2]是前三個中最大的那個。依次類推,排完所有點。
3.歸并排序(MergeSort)
#include <iostream> #define SIZE 100 //設置最大長度 using namespace std; void MergeSort(int *a ,int s,int e){if (s >= e || s < 0 || e < 0)return;int mid = (s + e) / 2;MergeSort(a,s,mid);MergeSort(a,mid + 1,e);int i,j;i = s;j = mid + 1;int b[SIZE],tot = 0; while (i <= mid&& j <= e) {if (a[i] > a[j]) {b[tot++] = a[j++]; } else {b[tot++] = a[i++];}}while (i <= mid) {b[tot++] = a[i++];} while (j <= e) {b[tot++] = a[j++];}for (int i = 0; i < tot; ++i) {a[s + i] = b[i];} }int main(){int a[20],n;cin >> n;for (int i = 0; i < n && i < 20; ++i) {cin >> a[i];}MergeSort(a,0,n - 1);for (int i = 0; i < n && i < 20; ++i) {cout << a[i]<<" ";}cout << endl; }
4.選擇排序
把最小的放最前面,或者是反過來也是可以的。
#include <iostream> using namespace std;void SelectSort(int *a,int n) {for (int i = 0; i < n - 1; ++i) {int min = i;for (int j = i + 1; j < n; ++j) if (a[j] < a[min]) {min = j;}if (min != i) {int temp = a[i];a[i] = a[min];a[min] = temp;}} }int main() {int a[20],n;cin >> n;for (int i = 0; i < n && i < 20; ++i) {cin >> a[i];}SelectSort(a,n);for (int i = 0; i < n && i < 20; ++i) {cout << a[i]<<" ";}cout << endl; }
5.快速排序
#include <iostream> using namespace std;void sort (int *a,int s,int e){if (s >= e)return;int i = s,j = e,k = a[s];while (i < j) {while ( i < j && a[j] >= k)--j;if ( i < j ) {a[i] = a[j];}while ( i < j && a[i] <= k)++i;if ( i < j ) {a[j] = a[i];}}a[i] = k;sort(a,s,i-1);sort(a,i+1,e); }int main(){int a[1000];int n;cin >> n;for (int i = 0; i < n; ++i) {cin >> a[i];}sort(a,0,n-1);for (int i = 0; i < n; ++i) {cout << a[i]<<" ";} cout << endl; }
6.插入排序
//非降序 void insertionSort(int* s, int n) {for (int i = 1; i < n; ++i) {//s[0]默認排好了int key = s[i];//記錄key值int j = i - 1; while (j >= 0 && s[j] > key) // 移動到最頂端或小于等于的key s[j + 1] = s[j--];s[j + 1] = key;} }
總體來說,我還是比較喜歡快排的。這是用遞歸實現的,如果能轉成用棧去模擬棧的話,應該會降低時間復雜度。
總結
以上是生活随笔為你收集整理的讲讲排序(C++描述)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: class多项式(链表实现)
- 下一篇: s3c2440移植MQTT