c++ - How to simplify two functions that similar with each other -


i'm student studying computer engineering. today learning quick sort using c++. awesome algorithm , recognized quick sort needs 2 function ascending order , opposite. following codes!

#include <iostream> #define ascending 0  #define descending 1 #define max_size 50001  using namespace std;   int numbercnt; int sortmanner; int list[max_size];  void getinput(); void quicksort(int* list, int left, int right, int(*partition)(int*, int, int)); int partitionascending(int* list, int left, int right); int partitiondescending(int* list, int left, int right); void swap(int &a, int &b);  int main(){     getinput();     quicksort(list, 0, numbercnt - 1, sortmanner == ascending ? partitionascending : partitiondescending);     (int = 0; < numbercnt; i++){         cout << list[i] << endl;     }      return 0; }  void quicksort(int* list, int left, int right, int (*partition)(int*,int,int)){     if (left < right){         int pivot = partition(list, left, right);         quicksort(list, left, pivot - 1, partition);         quicksort(list, pivot + 1, right, partition);     } } int partitionascending(int* list, int left, int right){     int pivotval = list[left];     int pivotidx = left;     int low = left;     int high = right + 1;      do{         do{             low++;         } while (list[low] < pivotval);         do{             high--;         } while (list[high] > pivotval);         if (low < high)             swap(list[low], list[high]);     } while (low < high);      swap(list[pivotidx], list[high]);      return high; }  int partitiondescending(int* list, int left, int right){     int pivotval = list[left];     int pivotidx = left;     int low = left;     int high = right + 1;      do{         do{             low++;         } while (list[low] > pivotval);         do{             high--;         } while (list[high] < pivotval);         if (low < high)             swap(list[low], list[high]);     } while (low < high);      swap(list[pivotidx], list[high]);      return high; }  void swap(int &a, int &b){     int temp = a;     = b;     b = temp; }  void getinput(){     cin >> numbercnt >> sortmanner;     (int = 0; < numbercnt; i++)         cin >> list[i]; } 

you know functions similar each other! seems wasteful me!

how simplify functions?

if don't understand pool english plz, don't hesitate let me know :)

your partition can take comparison functor, like:

template <typename comp> int partition(int* list, int left, int right, comp comp){     int pivotval = list[left];     int pivotidx = left;     int low = left;     int high = right + 1;      do{         do{             low++;         } while (comp(list[low], pivotval));         do{             high--;         } while (!comp(list[high], pivotval));         if (low < high)             swap(list[low], list[high]);     } while (low < high);      swap(list[pivotidx], list[high]);      return high; }  int partitionascending(int* list, int left, int right){     return partition(list, left, right, [](int l, int r){ return l < r; });     // or return partition(list, left, right, std::less<int>()); }  int partitiondescending(int* list, int left, int right){     return partition(list, left, right, [](int l, int r){ return l > r; });     // or return partition(list, left, right, std::greater<int>()); } 

Comments