快速排序(C++)

    科技2022-09-16  119

    数据结构–快速排序(C++)

    快速排序是属于交换排序的一种方便的排序方法,使用较为快捷,其平均复杂度为O(nlogn)(以2为底)

    C++代码

    #include <iostream> #include <bits/stdc++.h> #define MAXLEN 6 using namespace std; int Position(int a[],int low,int high){ int pos = a[low]; while(low < high){ while(low < high && a[high]>=pos) --high; a[low] = a[high]; while(low < high && a[low]<=pos) ++low; a[high] = a[low]; } a[low] = pos; return low; } void QuickSort(int a[],int low,int high){ if(low < high){ int pos = Position(a,low,high); QuickSort(a,low,pos-1); QuickSort(a,pos+1,high); } } int main(){ int arr[MAXLEN] = {10,20,14,21,9,30}; QuickSort(arr,0,MAXLEN-1); cout << "The Result: " << endl; for(int i = 0; i < MAXLEN; i++){ cout << arr[i] << " "; } }
    Processed: 0.015, SQL: 9