Skip to content

Instantly share code, notes, and snippets.

Created June 28, 2012 16:18
Show Gist options
  • Save anonymous/3012254 to your computer and use it in GitHub Desktop.
Save anonymous/3012254 to your computer and use it in GitHub Desktop.
QuickSort
template<typename T>
void quickSort(std::vector<T>& nums)
{
_qSort(nums, 0, nums.size());
}
template<typename T>
void _qSort(std::vector<T>& nums, int begin, int end)
{
if(end - begin < 2)
return;
int pivot = choosePivot(nums, begin, end);
swap(nums[begin], nums[pivot]);
pivot = begin;
int i = begin + 1;
for(int j = begin + 1; j < end; j++)
{
if(nums[j] < nums[pivot])
{
swap(nums[i], nums[j]);
i++;
}
}
swap(nums[pivot], nums[i-1]);
_qSort(nums, begin, i-1);
_qSort(nums, i, end);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment