第七章快速排序之“區間模糊排序FUZZY-SORT”(待改進。。。)

來源:互聯網
上載者:User

快速排序可以看成區間大小為1的模糊排序。

#include <string.h>#include <time.h>#define BUFFER_SIZE 10typedef struct{int start;int end;}Node; int FuzzyPartition(Node *a,int p,int r){Node tmp;int i=0;int j=0;int k=0;Node x;i=p-1;x.start=a[r].start;x.end=a[r].end;for(j=p;j<r;j++){if(a[j].end<x.start){//在所選主元區間左邊 i++;tmp.start=a[i].start;tmp.end=a[i].end;a[i].start=a[j].start;a[i].end=a[j].end;a[j].start=tmp.start;a[j].end=tmp.end;}else if(a[j].start>x.end){//在所選主元區間右邊 ;}else{//與所選主元區間有重疊,則將主元區間更新為重疊部分 x.start=(a[j].start>=x.start)?a[j].start:x.start;x.end=(a[j].end<=x.end)?a[j].end:x.end;}}i++;tmp.start=a[i].start;tmp.end=a[i].end;a[i].start=a[j].start;a[i].end=a[j].end;a[j].start=tmp.start;a[j].end=tmp.end;return i;}void FuzzySort(Node *a,int p,int r){int q=0;if(p<r){q=FuzzyPartition(a,p,r);FuzzySort(a,p,q-1);FuzzySort(a,q+1,r);}}int main(){int i=0;int j=0;Node a[BUFFER_SIZE]; //隨機產生數組 srand((unsigned)time(NULL));for(j=0;j<BUFFER_SIZE;j++){a[j].start=rand()%100;a[j].end=rand()%100+100;} printf("隨機產生的區間:\n");for(i=0;i<BUFFER_SIZE;i++){printf("%d %d\n",a[i].start,a[i].end);} printf("\n");FuzzySort(a,0,BUFFER_SIZE-1);printf("對區間進行模糊排序:\n"); for(i=0;i<BUFFER_SIZE;i++){printf("%d %d\n",a[i].start,a[i].end);}system("pause");return 0;} 

聯繫我們

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