Skip to content

Commit

Permalink
Random pivot selection
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeBottle committed Nov 1, 2023
1 parent 9f2ceea commit 3d24c1e
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions Algo/code/C-Cpp/PTA/DataStructure/DS-7-12.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include <cstdio>
#include <vector>
#include <cstdlib>

using namespace std;

typedef vector<long> Sequence; // 长整型数字序列

void Swap(long &elem1, long &elem2);
inline void Swap(long &elem1, long &elem2);
void BubbleSort(Sequence &seq); // 冒泡排序
void SelectionSort(Sequence &seq); // 选择排序
void InsertionSort(Sequence &seq); // 插入排序
Expand Down Expand Up @@ -41,7 +42,7 @@ int main()
}

// 交换元素
void Swap(long &elem1, long &elem2)
inline void Swap(long &elem1, long &elem2)
{
long tmp = elem1;
elem1 = elem2;
Expand Down Expand Up @@ -301,7 +302,7 @@ void RecursiveMerge(int start, int end, Sequence &origin, Sequence &buf)
// 快速排序
void QuickSort(Sequence &seq, int left, int right)
{
if (right < left)
if (right <= left)
return;
int pivotInd = Partition(seq, left, right);
QuickSort(seq, left, pivotInd - 1);
Expand All @@ -310,7 +311,9 @@ void QuickSort(Sequence &seq, int left, int right)

int Partition(Sequence &seq, int left, int right)
{
int pivot = seq[left]; // 选取第一个元素作为枢轴
int randInd = rand() % (right - left + 1) + left; // 随机生成一个位置
Swap(seq[left], seq[randInd]); // 把这个随机选取的元素交换到首位(因为首个元素会被覆盖)
int pivot = seq[left]; // 选取第一个元素作为枢轴
while (left < right)
{
while (seq[right] > pivot && right > left)
Expand Down

0 comments on commit 3d24c1e

Please sign in to comment.