[排序] 快排 && 冒泡(自己寫)

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   ar   for   art   div   

#include <iostream>using namespace std;/*快速排序通過一趟排序,以軸點為界 分割為兩部分:左部分 <= 軸點 <= 右部分再分別對左右兩部分繼續遞迴排序最終整個序列有序*/void quickSort(int arr[], int low,int high){        //if (arr==NULL||length<=0||start<=0||end>=length)        //break;    int i,key,j;    if (low<high){                              key=arr[low];        i=low;        j=high;        while(i<j){                         //結束條件是i==j            while(i<j && arr[j]>=key)    //從高到低,直到找到第一個<key的元素a[j]                j--;            if(i<j)                         //這裡為什麼要再判斷一下呢??                arr[i++]=arr[j];         //繼續上邊,將arr[j]移到a[i]上            while(i<j && arr[i]<=key)                i++;            if(i<j)                arr[j--]=arr[i];        }        arr[i]=key;        quickSort(arr,low,i-1);        quickSort(arr,i+1,high);    }}void quickSort(int arr[], int low,int high){    //出錯預防    //if (arr==NULL||length<=0||start<=0||end>=length)    //break;    int pivot=partition(arr,low,high); //尋找樞軸    quickSort(arr,low,pivot-1);    quickSort(arr,pivot+1,high);}*/void main(){    int a[]={1,3,2,5,7,4};    quickSort(a,0,5);    for(int i=0;i<6;i++)    cout <<a[i];}

 

#include <iostream>using namespace std;/*冒泡排序*******************************************一次起泡,最大值通過交換達到最右邊二次起泡,第二大的值達到倒數第二位……len-1次起泡   ,基於面試寶典P216 *///改進版,若在某次起泡過程中沒有交換,則說明已經全部有序了,因此這趟排序後就可以終止了void bubble(int *a, int len){    //對數組傳引用,這裡形參怎麼寫更好??                                //答:*a 和 a[]都可以    int i,j,tmp,k;    bool tag;    for(k=1;k<len;k++){           tag=false;            //只執行這個,表示此趟排序沒有交換        for (i=0;i<len-k;i++){            j=i+1;            if (a[i]>a[j]){                tmp=a[i];                a[i]=a[j];                a[j]=tmp;                tag=true;    //表示此趟排序有交換            }        }        if (tag==false)            return;    }}/*//基本版void bubble(int *a, int len){//對數組傳引用,這裡形參怎麼寫更好??                            //答:*a 和 a[]都可以    int i,j,tmp,k;    for(k=1;k<len;k++){                 for (i=0;i<len-k;i++){            j=i+1;            if (a[i]>a[j]){                tmp=a[i];                a[i]=a[j];                a[j]=tmp;            }        }    }}*/void main(){    int a[8]={49,38,65,97,76,13,27,49};    /*        int a[10];            cout<<"請輸入10個整數:"<<endl;            for(int i=0;i<10;i++)                cin>>a[i];        bubble(a,10);    */        bubble(a,8);    for(int i=0;i<8;i++)        cout<<a[i]<<‘\t‘;    cout<<endl;}

 

[排序] 快排 && 冒泡(自己寫)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.