排序演算法--快速排序

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   

描述:

快速排序可以理解為:分+遞迴,主要思想是分治。先找一個中間數,將數組劃分成左右兩個部分,左邊的均小於或中間數,右邊的均大於中間數;然後遞迴對左右部分進行遞迴;遞迴結束是區間只含一個數或者零個數。

參考:挖坑+填坑http://blog.csdn.net/morewindows/article/details/6684558#reply

 1 #include <iostream> 2 #include <string> 3 #include <memory.h> 4 #include <vector> 5 using namespace std; 6  7 //遞迴版本 8 void quick_sort1(vector<int> &arr,int s,int e) 9 {10     if(s < e)11     {12         int pos = part_tion(arr,s,e);13         quick_sort1(arr, s, pos - 1);14         quick_sort1(arr, pos + 1, e);15     }16 }17 18 int part_tion(vector<int> &arr,int s,int e)19 {20     int tmp = arr[s];21     int i = s;22     int j = e;23     while(i < j)24     {25         while(i < j && arr[j] > tmp)26             --j;27         if(i < j)28         {29             swap(arr[i],arr[j]);30             ++i;31         }32 33         while(i < j && arr[i] <= tmp)34             ++i;35         if(i < j)36         {37             swap(arr[i],arr[j]);38             j--;39         }40     }41     arr[i] = tmp;42     return i;43 }44 45 int main()46 {47 48     return 0;49 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.