直接上代码,里面有一个注意点就是当从右向左的时候(假设是按升序来排序),对于跟基准值相等的元素一定要进行处理,而不是跳过。或者从左向右的时候,对于跟基准值相等的一定要跳过,否则容易进入死循环,或者对有重复数字的数据中间过程有“假”快速排序的情况(比如2 2 9 1 2 3, 第一次循环很可能就变成了2 2 1 9 2 3,虽然最终结果是对的,但是这一步并没有把数据按照基准值进行划分)。
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
#pragma region Sort
namespace Sort
{
int iArray[] = { 3, 8, 1, 6, 2, 5, 4, 9, 7 };
int iArray1[] = { 2,2,18,9,1,3,2,0,19,20,15,6,12 };
int iArray2[] = { 2,1,1,2 };
vector<int>iVector = { 3, 8, 1, 6, 2, 5, 4, 9, 7 };
void QuickSort(int data[], int left, int right)
{
if (left >= right)
return;
int start = left, end = right;
int temp = data[left];
while (end > start)
{
while (end > start && data[end] > temp)
end--;
while (end > start && data[start] <= temp)
start++;
if (end > start)
{
int leftTemp = data[start];
data[start] = data[end];
data[end] = leftTemp;
}
}
data[left] = data[end];
data[end] = temp;
QuickSort(data, left, end - 1);
QuickSort(data, end + 1, right);
}
void QuickSort2(int data[], int left, int right)
{
if (left >= right)
return;
int start = left, end = right;
int base = data[start];
while (end > start)
{
while (end > start && data[end] > base)
end--;
data[start] = data[end];
while (end > start && data[start] <= base)
start++;
data[end] = data[start];
}
data[start] = base;
QuickSort2(data, left, end - 1);
QuickSort2(data, end + 1, right);
}
void QuickSortWithComment(int data[], int left, int right)
{
if (left >= right)
{
cout << "******Startindex " << left << " >=end index " << right << endl;
return;
}
cout << "Sorting";
for (int i = left; i <= right; i++)
{
cout << "_" << data[i];
}
cout << endl;
int start = left, end = right;
int temp = data[left];
while (end > start)
{
while (end > start && data[end] > temp)
end--;
while (end > start && data[start] <= temp)
start++;
if (end > start)
{
int leftTemp = data[start];
data[start] = data[end];
data[end] = leftTemp;
}
}
data[left] = data[end];
data[end] = temp;
cout << "TempResult with KEY: " << temp << ":";
for (int i = left; i <= right; i++)
{
if (i == end)
{
cout << "_(" << data[i] << ")";
}
else
{
cout << "_" << data[i];
}
}
cout << endl;
QuickSort(data, left, end - 1);
QuickSort(data, end + 1, right);
cout << "Resultwith KEY: " << temp << ":";
for (int i = left; i <= right; i++)
{
cout << "_" << data[i];
}
cout << endl;
}
int Patition(int data[], int left, int right)
{
if (left < right)
{
int base = data[left];
while (left < right)
{
while (left < right&& data[right] > base)
right--;
data[left] = data[right];
while (left < right&& data[left] <= base)
left++;
data[right] = data[left];
}
data[left] = base;
}
return left;
}
void QuickSortWithoutRecursive(int data[], int left, int right)
{
if (left < right)
{
stack<int> st;
st.push(right);
st.push(left);
while (!st.empty())
{
left = st.top();
st.pop();
right = st.top();
st.pop();
int patition = Patition(data, left, right);
if (patition > left)
{
st.push(patition- 1);
st.push(left);
}
if (patition < right)
{
st.push(right);
st.push(patition+ 1);
}
}
}
}
void TestQuickSort()
{
//QuickSortWithoutRecursive(iArray,0, 8);
//QuickSortWithoutRecursive(iArray1,0, 12);
//QuickSortWithoutRecursive(iArray2,0, 3);
//QuickSort(iArray,0, 8);
//QuickSort(iArray1,0, 12);
QuickSort2(iArray1, 0, 12);
//QuickSort(iArray2,0,3);
QuickSort2(iArray2, 0, 3);
}
}
#pragma endregion
#define TEST(uSpace,method)\
{\
using namespace uSpace;\
method;\
}
int main()
{
TEST(Sort, TestQuickSort());
return 0;
}

本文通过详细的代码示例介绍了快速排序算法的实现方式,并讨论了在排序过程中处理重复元素的重要性,以避免算法陷入死循环或出现不正确的中间状态。
260

被折叠的 条评论
为什么被折叠?



