quick_sort
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void quick_sort1(int* a, int n) {
if (n < 2)
return;
int l = 0,
r = n - 1;
int t = a[0];
int f = 2;
while(l < r){
if (f == 2)
{
if (a[r] >= t)
{
r--;
continue;
}
a[l] = a[r];
f = 1;
l++;
continue;
}
if (f == 1)
{
if (a[l] <= t)
{
l++;
continue;
}
a[r] = a[l];
f = 2;
r--;
continue;
}
}
a[l] = t;
quick_sort1(a, l);
quick_sort1(a + l + 1, n - l - 1);
return;
}
void insert_sort(int* a, int n) {
if (n < 2)
return;
int t, j;
for (int i = 1; i < n; i++) {
t = a[i];
for (j = i - 1; j >= 0 && t < a[j]; j--)
a[j + 1] = a[j];
a[j + 1] = t;
}
return;
}
void quick_sort2(int* a, int n) {
int l = 0,
r = n - 1;
int t = a[0];
int f = 2;
while (l < r) {
if (f == 2)
{
if (a[r] >= t)
{
r--;
continue;
}
a[l] = a[r];
f = 1;
l++;
continue;
}
if (f == 1)
{
if (a[l] <= t)
{
l++;
continue;
}
a[r] = a[l];
f = 2;
r--;
continue;
}
}
a[l] = t;
if (l<10)
insert_sort(a, l);
else
quick_sort1(a, l);
if (n - l - 1 < 10)
insert_sort(a + l + 1, n - l - 1);
else
quick_sort1(a + l + 1, n - l - 1);
}
void quick_sort3(int* a, int n) {
if (n < 2)
return;
int c = 0;
vector<int> v;
for(int i=0;i<n;i+=5)
v.push_back(i);
sort(v.begin(), v.end());
int vl = v.size();
for (int i = 0; i < n; i += 5) {
if (a[i] == v[vl / 2]){
c = i;
break;
}
}
int t = a[c];
int l = 0,
r = n - 1;
int f = 2;
a[c] = a[l];
while (l < r) {
if (f == 2)
{
if (a[r] >= t)
{
r--;
continue;
}
a[l] = a[r];
f = 1;
l++;
continue;
}
if (f == 1)
{
if (a[l] <= t)
{
l++;
continue;
}
a[r] = a[l];
f = 2;
r--;
continue;
}
}
a[l] = t;
quick_sort3(a, l);
quick_sort3(a + l + 1, n - l - 1);
}
int main() {
int a[] = { 44,3,38,5,47,15,36,26,27,2,46,4,19,50,48 };
int c[] = { 44,3,38,5,47,15,36,26,27,2,46,4,19,50,48 };
quick_sort3(c, sizeof(c) / sizeof(c[0]));
for (const auto& e : c)
cout << e << " ";
cout << endl;
return 0;
}