递归快排一
void quickSort(int *array, int left, int right)
{
if(left < right)
{
int pivot = array[left];
int low = left, high = right;
while(low < high)
{
while(array[high] >= pivot && low < high)
high--;
array[low] = array[high];
while(array[low] <= pivot && low < high)
low++;
array[high] = array[low];
}
array[low] = pivot;
quickSort(array, left, low - 1);
quickSort(array, low + 1, right);
}
}
递归快排二
#include
#include
using namespace std;
int Parition(vector<int>& br,int left,int right)
{
if (br.empty()) return -1;
int i = left, j = right;
int tmp = br[left];
while (i<j)
{
while (i < j && br[j] > tmp) --j;//从右往左找小元素,前置运算符防止越界
if (i < j) br[i] = br[j];
while (i < j && br[i] <= tmp) ++i;//从左往右找大元素
if (i < j) br[j] = br[i];
}
br[i] = tmp;
return i;
}
void QuickPass(vector<int>& br, int left, int right)
{
if (left < right)
{
int pos = Parition(br, left, right);//分区并各自排序
QuickPass(br,left,pos -1);//左子区递归
QuickPass(br,pos+1,right);//右子区递归
}
}
void QuickSort(vector<int> &br)
{
QuickPass(br,0,br.size()-1);
}
//测试
int main()
{
vector<int > ar = {
56,34,23,78,90,12,45,67,89,100};
for (auto& x : ar)
{
cout << x << ",";
}
cout << endl;
QuickSort(ar);
for (auto&x:ar)
{
cout << x << ",";
}
cout << endl;
return 0;
}
非递归快排
int Parition(vector<int>& br,int left,int right)
{
if (br.empty()) return -1;
int i = left, j = right;
int tmp = br[left];
while (i<j)
{
while (i < j && br[j] > tmp) --j;//从右往左找小元素,前置运算符防止越界
if (i < j) br[i] = br[j];
while (i < j && br[i] <= tmp) ++i;//从左往右找大元素
if (i < j) br[j] = br[i];
}
br[i] = tmp;
return i;
}
void QuickSort(vector<int>& br)
{
if (br.empty()) return;
int left = 0, right = br.size() - 1;
deque<int> qu;
qu.push_back(left);//尾插
qu.push_back(right);
while (!qu.empty())
{
left = qu.front(); qu.pop_front();//头出
right = qu.front(); qu.pop_front();
int pos = Parition(br, left, right);
if (left < pos - 1)
{
qu.push_back(left);
qu.push_back(pos - 1);
}
if (pos + 1 < right)
{
qu.push_back(pos + 1);
qu.push_back(right);
}
}
}
随机划分区间
当数据完全有序时,快排的性能会变的很差,因此可以选择随机划分区间的方法。
int RandParition(vector<int>& br, int left, int right)//随机划分
{
srand(time(nullptr));//更随机
int index = rand() % (right - left+1)+ left;
std::swap(br[left], br[index]);
return Parition(br, left, right);
}
单向划分
int OWParition(vector<int>& br, int left, int right)
{
int i = left, j = left + 1;
int tmp = br[i];
while (j <= right)
{
if (br[j] <= tmp)
{
++i;
std::swap(br[i], br[j]);//交换
}
++j;
}
std::swap(br[left], br[i]);//划分区间
return i;
}
链表快排
typedef int ElemType;
typedef struct ListNode
{
ElemType data;
struct ListNode* next;
}ListNode,*LinkList;
//区间划分
ListNode* LinkParition(ListNode* left, ListNode* right)
{
ListNode* ip = left;
ListNode* jp = left->next;
int tmp = left->data;
while (jp != right)
{
if (jp->data <= tmp)
{
ip = ip->next;
std::swap(ip->data, jp->data);
}
jp = jp->next;
}
std::swap(left->data, ip->data);
return ip;
}
void QuickPass(ListNode* left, ListNode* right)
{
if (left != right)
{
ListNode* p = LinkParition(left, right);
QuickPass(left, p);
QuickPass(p->next, right);
}
}
void ListQuickSort(ListNode* head)
{
QuickPass(head, nullptr);
}
图论