fork download
  1. void Utilities::QuickSort(std::vector<int>::iterator begin, std::vector<int>::iterator end, std::vector<int>& v)
  2. {
  3. // Initially find a random pivot
  4. if (begin >= end - 1)
  5. return;
  6. int pivotIndex = std::rand() % (end - begin);
  7. int pivot = v.at(pivotIndex);
  8.  
  9. // Pointer to begining of array and one to the end
  10.  
  11. // While begin != end
  12. while(begin != end)
  13. {
  14. // Find the lowest bound number to swap
  15. while(*begin < pivot && begin < end)
  16. begin++;
  17. while(*end >= pivot && begin < end)
  18. end--;
  19.  
  20. // Do the swap
  21. Utilities::Swap(begin, end);
  22. }
  23.  
  24. // Here begin and end are equal and equal to point from where left side is lower and right side is greater or equal to pivot
  25.  
  26. // Partition left
  27. Utilities::QuickSort(v.begin(), begin, v);
  28. // Partition right
  29. Utilities::QuickSort(begin, --v.end(), v);
  30. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty