欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

專注電子技術學習與研究
當前位置:單片機教程網 >> MCU設計實例 >> 瀏覽文章

用VC++類實現快速排序(并輸出過程)

作者:佚名   來源:本站原創   點擊數:  更新時間:2013年12月01日   【字體:



&&&&&&&&&&&&&&&&&&&&&&&&&&&&主函數&&&&&&&&&&&&&&&&&&&&&&
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "WangQi.h"
using namespace std;
#define MAX 100
void main(){
SeqList L;
int num;
cout<<"請輸入要排序的元素個數:"<<endl;
cin>>num;
cout<<"請輸入要排序的元素:"<<endl;
for(int i=1;i<=num;i++)
cin>>L.r[i];
L.length=num;
//輸出排序前的順序表
L.output(&L,1,L.length,-1);
L.quicksort(&L,1,L.length);
L.output(&L,1,L.length,-2);
}
&&&&&&&&&&&&&&&&&&&含有類定義的頭文件&&&&&&&&&&&&&&&&&&&&&&&&&
#include <iostream>
using namespace std;
#define MAX 100
class SeqList{
public:
int r[MAX+1];
int length;

void output(SeqList *L,int low, int high,int pivotloc){
int i;

if(pivotloc==-1||pivotloc==-2){
  if(pivotloc==-1)
       cout<<"初始狀態:{"<<'\t';
  else cout<<"排序結果:{"<<'\t';
     for(i=low;i<=high;i++)
      cout<<L->r[i]<<'\t';
      cout<<"}";
      }else {
     cout<<"劃分結果:{"<<'\t';
   for(i=low;i<pivotloc;i++)
      cout<<L->r[i]<<'\t';
      cout<<"}"<<L->r[pivotloc]<<"{";
    for(i=pivotloc+1;i<=high;i++)
     cout<<L->r[i]<<'\t';
    cout<<"}";
 }
  cout<<'\n'<<endl;
}


int partition(SeqList *L,int low,int high){
  int pivotkey;
  int temp1=low,temp2=high;
  L->r[0]=L->r[low];
  pivotkey=L->r[low];
  while (low<high){
     while (low<high && L->r[high]>=pivotkey)
     --high;
     L->r[low]=L->r[high];
    while(low<high && L->r[low]<=pivotkey)
    ++low;
    L->r[high]=L->r[low];
   }
    L->r[low]=L->r[0];
    output(L,temp1,temp2,low);
    return low;
    }


void quicksort(SeqList *L,int low,int high){
int pivotloc;
   if(low<high)
     pivotloc=partition(L,low,high);
    if(low<pivotloc-1)
     quicksort(L,low,pivotloc-1);
    if(high>pivotloc+1)
     quicksort(L,pivotloc+1,high);
}
};


 

關閉窗口

相關文章