Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <stdlib.h>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. void Partition(int* masyv, int a) {
  8.  
  9.     if (a <= 1) {
  10.         return;
  11.     }
  12.  
  13.     int Pivot = masyv[rand() % a];
  14.     int low = 0;
  15.     int high = a - 1;
  16.  
  17.     while (low < high) {
  18.         while (masyv[low] < Pivot) {
  19.             ++low;
  20.         }
  21.         while (masyv[high] > Pivot) {
  22.             --high;
  23.         }
  24.  
  25.         int iTemp       = masyv[low];
  26.         masyv[low]     = masyv[high];
  27.         masyv[high]     = iTemp;
  28.     }
  29.  
  30.     Partition(masyv, low);
  31.     Partition(&(masyv[low + 1]), a - low - 1);
  32. }
  33.  
  34. void Quicksort(int* masyv, int a) {
  35.     srand((unsigned int)time(0));
  36.     Partition(masyv, a);
  37. }
  38.  
  39. int main() {
  40.  
  41.     int a;
  42.     ifstream failas;
  43.     failas.open("info.txt");
  44.     failas>>a;
  45.     int masyv[a];
  46.     cout<<"Nuskaityti skaiciai: \n";
  47.         for (int i=0;i<a;i++)
  48.         {
  49.             failas >>masyv[i];
  50.             cout<<masyv[i]<<" ";
  51.         }
  52.     cout<<"\nnuskaitymas baigtas...\n-------------------------------------\n";
  53.     Quicksort(masyv, a);
  54.  
  55.     cout<<"Surikiuoti skaiciai: \n";
  56.         for (int i = 0; i < a; i++){
  57.             cout << masyv[i] << " ";
  58.         }
  59.  
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement