zoukankan      html  css  js  c++  java
  • 快速排序法的实现

    /*
      快速排序的实现
      coder:QPZ
      time:2014-12-04
    */
    #include <stdlib.h>
    #include <time.h>
    #include <iostream>
    using namespace std;
    #define N 10
    void Swap(int &a,int &b){
    	int t;
    	t=a;
    	a=b;
    	b=a;
    }
    class QuickSort{
      private:
      	int *a;
      	int  n;
      public:
      QuickSort(int n);
      void Quick(int left,int right);
      void PrinArr();
    };
    int main(void)
    {
    	class QuickSort *p=new QuickSort(N);
        p->PrinArr();
    	p->Quick(0,N-1);
    	p->PrinArr();
    	return 0;
    }
      QuickSort::QuickSort(int n){
       this->n=n;
       this->a=(int *)malloc(n*(sizeof(int)));
       srand((unsigned)time(NULL));
       for(int i=0;i <n; i++ ){
       this->a[i]=rand()%10;	
       }/*for*/
         	
      }	
      void  QuickSort::Quick(int left,int right)
      {
      	int Pivot=a[left];
      	int Left=left;
      	int Right=right;
      	   if(left<right){
      	  	 while(Left<Right){
      	   	  while(Left<Right&&a[Right]>=Pivot) Right--;
      	   	      a[Left]=a[Right];
      	   	  while(Left<Right&&a[Left]<=Pivot) Left++;
      	   	      a[Right]=a[Left];
      	   }//while(Left<Right)
      	   a[Left]=Pivot;
      	   Quick(left,Left-1);
      	   Quick(Left+1,right);
       }//if 
    	} 
      void QuickSort::PrinArr()
      {
          for(int i=0; i < n; i++ ){
          	cout<<this->a[i]<<" ";
          }
          cout<<endl;
      }
    


  • 相关阅读:
    hdu 3367 Pseudoforest
    hdu 2489 Minimal Ratio Tree
    hdu 4009 Transfer water
    poj 3164 Command Network
    hdu 3926 Hand in Hand
    hdu 3938 Portal
    5-26日(面经总结)
    5-25日
    5-21日|5-22日
    5-13日记录|5-14日
  • 原文地址:https://www.cnblogs.com/pzqu/p/9457669.html
Copyright © 2011-2022 走看看