Advertisement
rootuss

quicksort Maciek

Feb 17th, 2017
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <conio.h>
  6.  
  7. using namespace std;
  8.  
  9. void wczyt(double T[], int n)
  10. {
  11.     for (int i=0; i<n; i++)
  12.     {
  13.         cout <<"podaj "<<i+1<<" liczbe"<<endl;
  14.         cin >>T[i];
  15.     }
  16. }
  17. void Sortowaniem( double T[], int left, int right )
  18. {
  19.  
  20.     int pom;
  21.     int i = left;
  22.     int j = right;
  23.     int x = T[( left + right ) / 2 ];
  24.     do
  25.     {
  26.         while( T[ i ] > x )
  27.              i++;
  28.  
  29.         while( T[ j ] < x )
  30.              j--;
  31.  
  32.         if( i <= j )
  33.         {
  34.             pom = T[i];
  35.             T[i]=T[j];
  36.             T[j]=pom;
  37.  
  38.  
  39.             i++;
  40.             j--;
  41.         }
  42.     } while( i <= j );
  43.  
  44.     if( left > j ) Sortowaniem( T, left, j );
  45.  
  46.     if( right < i ) Sortowaniem( T, i, right );
  47.  
  48. }
  49. void Sortowanie( double T[], int left, int right )
  50. {
  51.  
  52.     int pom;
  53.     int i = left;
  54.     int j = right;
  55.     int x = T[( left + right ) / 2 ];
  56.     do
  57.     {
  58.         while( T[ i ] < x )
  59.              i++;
  60.  
  61.         while( T[ j ] > x )
  62.              j--;
  63.  
  64.         if( i <= j )
  65.         {
  66.             pom = T[i];
  67.             T[i]=T[j];
  68.             T[j]=pom;
  69.  
  70.  
  71.             i++;
  72.             j--;
  73.         }
  74.     } while( i <= j );
  75.  
  76.     if( left < j ) Sortowanie( T, left, j );
  77.  
  78.     if( right > i ) Sortowanie( T, i, right );
  79.  
  80. }
  81. int main ()
  82. {int i,j;
  83. int n;
  84. for (;;)
  85. {
  86. int wybor;
  87.     cout <<" witaj w programie sortujacym"<<endl;
  88.     cout <<" ile elementów chcesz posortowac ?"<<endl;
  89.     cin>>n;
  90.      double T[n];
  91.      wczyt (T, n);
  92.     cout <<" %%%%%%%%%%%%%%   MENU   %%%%%%%%%%%"<<endl;
  93.     cout <<" 1. sortowanie rosnaco"<<endl;
  94.     cout <<" 2. sortowanie malejaco"<<endl;
  95.     cout <<" 3. zamkniecie programu"<<endl;
  96.     wybor =getch();
  97.     switch (wybor)
  98.     {
  99.     case'1':
  100.         Sortowanie (T,0,n-1);
  101.     cout <<" Po posortowaniu rosnaco: "<<endl;
  102.     for (int i=0;i<n;i++)
  103.     {
  104.  
  105.         cout <<"T["<<i<<"]="<<T[i]<<endl;
  106.     };
  107.     break;
  108.     case '2':
  109.         Sortowaniem(T,0,n-1);
  110.     cout <<" Po posortowaniu malejaco: "<<endl;
  111.     for (int i=0;i<n;i++)
  112.     {
  113.  
  114.         cout <<"T["<<i<<"]="<<T[i]<<endl;
  115.  
  116.     };
  117.     break;
  118.     case '3':
  119.         exit (0);
  120.         break;
  121.     default:
  122.         cout<<"nie matakiej opcji"<<endl;
  123.  
  124.     };
  125.     break;
  126.  
  127.  
  128.  
  129. }
  130. getchar (); getchar();
  131. system ("cls");
  132.  
  133.  
  134.    return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement