vladkomarr

funcObjectSort

Oct 22nd, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. //main.cpp
  2. #include <iostream>
  3. #include "sort.h"
  4.  
  5. int main()
  6. {
  7.     int *array;
  8.     int size, type;
  9.  
  10.     std::cout << "Enter the size of array: ";
  11.     std::cin >> size;
  12.     array = new int[size];
  13.  
  14.     std::cout << "Enter the array:\n";
  15.     for(int i = 0; i < size; i++)
  16.         std::cin >> array[i];
  17.  
  18.     std::cout << "Choose the sort method:\n";
  19.     std::cout << "1. Quick sort\n"
  20.               << "2. Bubble sort\n"
  21.               << "3. Insert sort\n";
  22.  
  23.     std::cin >> type;
  24.     TSort sort(type);
  25.  
  26.     sort(array, 0, size);
  27.  
  28.     std::cout << "Sorting the elements...\n";
  29.     for(int i = 0; i < size; i++)
  30.         std::cout << array[i] << " ";
  31.  
  32.     return 0;
  33. }
  34.  
  35. //sort.h
  36. #ifndef SORT_H
  37. #define SORT_H
  38. #include <iostream>
  39.  
  40. enum METHOD { QUICKSORT=1, BUBBLESORT, INSERTSORT };
  41.  
  42. class TSort
  43. {
  44. void (*method)(int *array, int first, int last);
  45. public:
  46.     TSort(int type);
  47.     void operator()(int *array, int first, int last);
  48. };
  49.  
  50. #endif // SORT_H
  51.  
  52. //sort.cpp
  53.  
  54. #include "sort.h"
  55.  
  56. void quickSort(int *array, int first, int last) {
  57.     last = last - 1;
  58.     int i = first;
  59.     int j = last;
  60.     int m = array[(first + last) / 2];
  61.  
  62.     do {
  63.         while(array[i] < m) i++;
  64.         while(array[j] > m) j--;
  65.  
  66.         if(i <= j) {
  67.             if(array[i] > array[j]) {
  68.                 int temp = array[j];
  69.                 array[j] = array[i];
  70.                 array[i] = temp;
  71.             }
  72.             i++;
  73.             j++;
  74.         }
  75.     } while(i <= j);
  76.  
  77.     if(i < last)
  78.         quickSort(array, i, last);
  79.     if(j > first)
  80.         quickSort(array, first, j);
  81. }
  82.  
  83. void bubbleSort(int *array, int first, int last) {
  84.     last = last - 1;
  85.     for(int i = 0; i < last; i++)
  86.         for(int j = 0; j < last - i; j++)
  87.             if(array[j] > array[j+1]) {
  88.                 int temp = array[j];
  89.                 array[j] = array[j+1];
  90.                 array[j+1] = temp;
  91.             }
  92. }
  93.  
  94. void insertSort(int *array, int first, int last) {
  95.     int temp;
  96.     for(int i = 0, j; i < last; ++i) {
  97.         temp = array[i];
  98.         for(j = i -1; j >= 0 && array[j] > temp; --j)
  99.             array[j+1] = array[j];
  100.         array[j+1] = temp;
  101.     }
  102. }
  103.  
  104.  
  105. TSort::TSort(int type) {
  106.     switch(type) {
  107.     case QUICKSORT:
  108.         method = quickSort;
  109.         break;
  110.     case BUBBLESORT:
  111.         method = bubbleSort;
  112.         break;
  113.     case INSERTSORT:
  114.         method = insertSort;
  115.     default:
  116.         method = quickSort;
  117.     }
  118. }
  119.  
  120. void TSort::operator ()(int *array, int first, int last) {
  121.     method(array, first, last);
  122. }
Advertisement
Add Comment
Please, Sign In to add comment