Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: C++  |  size: 0.85 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # include <iostream>
  2.      
  3. using namespace std;              
  4. const int n=10000;
  5. int tab[n];                  
  6. int partition(int tab[], int l, int r)
  7. {
  8.         int q=tab[l];
  9.         int i=l-1;
  10.         int j=r+1;
  11. do
  12. {
  13.  
  14.         do
  15.         {
  16.                 i++;
  17.         }while(tab[i]<q);
  18.         do
  19.         {
  20.                 j--;
  21.         }while(tab[j]>q);
  22.  
  23.                 if(i<j)
  24.                 {
  25.                         int pom=tab[j];
  26.                         tab[j]=tab[i];
  27.                         tab[i]=pom;
  28.                 }
  29.                
  30. }while(i<j);
  31. return j;
  32. }
  33.  
  34. void quicksort(int tab[], int l, int r)
  35. {
  36.         if(l<r)
  37.         {
  38.         int x=partition(tab, l, r);
  39.         quicksort(tab, l, x);
  40.         quicksort(tab, x+1, r);
  41.         }
  42. }
  43. int main()
  44. {
  45.         int k=0;
  46.         int m=0;
  47.        
  48.         cout<<"przed"<<endl;
  49.         for (int i=0; i<n; i++)
  50.         {
  51.                 tab[i]=i;
  52.                 cout<<tab[i];
  53.         }
  54.         cout<<endl;
  55.         partition(tab, 0, n-1);
  56.         quicksort(tab, 0, n-1);
  57.         cout<<"po sorcie"<<endl;
  58.         for(int i=0; i<n; i++)
  59.         {
  60.                 cout<<tab[i]<<" ";
  61.         }
  62.            getchar();
  63.     getchar();
  64.  
  65.     return 0;
  66. }