Guest User

Untitled

a guest
Jul 20th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <stdlib.h>
  5.  
  6. using namespace std;
  7.  
  8. void swap (int &x, int &y) {int z=x;x=y;y=z;}
  9.  
  10. void qSort( long int* A, int n, int& compres) {
  11.  
  12.      int i=1;
  13.       int j=1;
  14.       int pivot=n-1;//0,median
  15.      
  16.       if (n>1) {
  17.      
  18.      int x=A[pivot];
  19.      compres+=n-1;
  20.    
  21.       for (;j<n;j++){
  22.       if(i<j && x>A[j]){
  23.         swap (A[j],A[i]);
  24.         i++;
  25.         }
  26.       if(i==j && x> A[j]) i++;
  27.         }
  28.       swap (A[pivot],A[i-1]);
  29.        
  30.       long int *b=A+i;
  31.      
  32.       qSort (A, i-1, compres);
  33.       qSort (b, n-i, compres);
  34.        
  35.    }
  36.      
  37.  
  38.   }
  39.  
  40.  
  41. int main(int argc, char **argv)
  42. {
  43.     fstream myReadFile;
  44.  myReadFile.open("array1.txt");
  45.  long int *numbers;
  46.  int size=23;
  47.  numbers = new long int[size];
  48.  int count=0;
  49.  int compres=0;
  50.  string read;
  51.  
  52.  if (myReadFile.is_open()) {
  53.  while (!myReadFile.eof()) {
  54.    myReadFile >> read;
  55.    numbers[count]=atoi(read.c_str());
  56.     count++;
  57. }
  58. }
  59.  
  60.    
  61.     qSort (numbers,size,compres);
  62.     for (int i=0;i<size;i++)
  63.     cout<<i<<" "<<numbers[i]<<endl;
  64.    
  65.     cout<<endl<<endl<<compres;
  66.            
  67.     return 0;
  68. }
Add Comment
Please, Sign In to add comment