Guest User

Untitled

a guest
Jul 20th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. void PrintArray(int* array, int n);
  8. void QuickSort(int* array, int startIndex, int endIndex);
  9. int SplitArray(int* array, int pivotValue, int indexPivot, int startIndex, int endIndex);
  10. void swap(int &a, int &b);
  11.  
  12. int comparisons = 0;
  13.  
  14. int main(void)
  15. {
  16.     int array[10000];
  17.     int i;
  18.     FILE *file;
  19.     file = fopen("file.txt", "r");
  20.    
  21.     for( i = 0; i < 10000; i++)        
  22.     {
  23.          fscanf(file, "%d", &array[i]);
  24.     }
  25.    
  26.     QuickSort(array, 0, 10000 - 1);        
  27.     PrintArray(array, 10000);
  28.     printf("%d ", comparisons);
  29.    
  30.     getch();
  31.     return 0;
  32. }
  33.  
  34. void swap(int &a, int &b)
  35. {
  36.     int temp;
  37.     temp = a;
  38.     a = b;
  39.     b = temp;
  40. }
  41.  
  42.  
  43. void PrintArray(int* array, int n)
  44. {
  45.     int i;
  46.     FILE *out;
  47.     out=fopen("out.txt", "w");
  48.      
  49.     for( i = 0; i < n; i++)
  50.         fprintf(out, "\n %d ", array[i]);
  51. }
  52.  
  53. int choosePivot(int *array, int startIndex, int endIndex)
  54. {
  55.     int middle = 0;
  56.     middle = array[(endIndex - startIndex - 1)/2 + startIndex];
  57.     startIndex = array[startIndex];
  58.     endIndex = array[endIndex];
  59.     if((middle < startIndex && startIndex < endIndex) || (endIndex < startIndex && startIndex < middle))
  60.         return startIndex;
  61.     else
  62.         if((startIndex < endIndex && endIndex < middle) || (middle < endIndex && endIndex < startIndex))
  63.             return endIndex;
  64.         else
  65.             return middle;
  66. }
  67.  
  68. int SplitArray(int* array, int pivot, int indexPivot, int startIndex, int endIndex)
  69. {
  70.     int leftBoundary = startIndex;
  71.     int rightBoundary = endIndex;
  72.    
  73.     int i = leftBoundary + 1;
  74.    
  75.     for (int j = leftBoundary + 1; j <= rightBoundary; j++)
  76.     {
  77.         if (array[j] < pivot)
  78.         {
  79.             swap (array[j], array[i]);
  80.             i++;
  81.         }
  82.         comparisons++;
  83.     }
  84.  
  85.     swap (array[leftBoundary], array[i-1]);
  86.     indexPivot = i-1;
  87.     return indexPivot;
  88.  
  89. }
  90.  
  91. void QuickSort(int* array, int startIndex, int endIndex)
  92. {
  93.     int pivot = choosePivot(array, startIndex, endIndex);
  94.     int splitPoint = 0, middle = 0;
  95.  
  96.     middle = (endIndex - startIndex - 1) / 2 + startIndex;
  97.    
  98.     if(endIndex > startIndex)  
  99.     {
  100.         if(pivot == array[startIndex])
  101.         {
  102.             int indexPivot = startIndex;
  103.             splitPoint = SplitArray(array, pivot, indexPivot, startIndex, endIndex);
  104.         }
  105.         if(pivot == array[endIndex])
  106.         {
  107.             int indexPivot = endIndex;
  108.             swap(array[startIndex], array[endIndex]);
  109.             splitPoint = SplitArray(array, pivot, indexPivot, startIndex, endIndex);
  110.         }
  111.         if(pivot == array[middle])
  112.         {
  113.             int indexPivot = (endIndex - startIndex - 1) / 2 + startIndex;
  114.             swap(array[startIndex], array[middle]);
  115.             splitPoint = SplitArray(array, pivot, indexPivot, startIndex, endIndex);
  116.         }
  117.         QuickSort(array, startIndex, splitPoint-1);
  118.         QuickSort(array, splitPoint+1, endIndex);
  119.     }
  120. }
Add Comment
Please, Sign In to add comment