Skip to content

Commit fd240e4

Browse files
committed
Another partition function
1 parent c9062e5 commit fd240e4

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

Cpp/quickSort.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,30 @@ int partition(std::vector<int>& vec, int left, int right)
1919
return index - 1;
2020
}
2121

22+
int partition2(std::vector<int>& vec, int left, int right)
23+
{
24+
int pivot = vec[right];
25+
int index = left - 1;
26+
27+
for(int i=left; i < right; ++i)
28+
{
29+
if(vec[i] < pivot)
30+
{
31+
index ++;
32+
std::swap(vec[index], vec[i]);
33+
}
34+
}
35+
36+
std::swap(vec[right], vec[index + 1]);
37+
38+
return index + 1;
39+
}
40+
2241
void quickSort(std::vector<int>& vec, int low, int high)
2342
{
2443
if(low < high)
2544
{
26-
int pivotIndex = partition(vec, low, high);
45+
int pivotIndex = partition2(vec, low, high);
2746
quickSort(vec, low, pivotIndex - 1);
2847
quickSort(vec, pivotIndex + 1, high);
2948
}

0 commit comments

Comments
 (0)