无需进行合并,因为是在原数组上进行的排序
QUICKSORT(A, p, r)
if p < r
q = PARTITION(A, p, r)
QUICKSORT(A, p, q-1)
QUICKSORT(A, q+1, r)
PARTITION(A, p, r)
x = A[r]
i = p - 1
for j=p to r-1
if A[j] <= x
i = i + 1
exchange A[i] with A[j]
return i+1
def partition(A, p, r):
x = A[r]
i = p - 1
j = p
while j <= r:
if A[j] <= x:
i = i + 1
temp = A[i]
A[i] = A[j]
A[j] = temp
j = j + 1
return i
def quick_sort(A, p, r):
if p < r:
q = partition(A, p, r)
quick_sort(A, p, q-1)
quick_sort(A, q+1, r)
def main():
A = [2, 8, 7, 1, 3, 5, 6, 4]
print(A)
quick_sort(A, 0, 7)
print(A)
if __name__ == "__main__":
main()
#include <iostream>
using namespace std;
int partition(int A[], int p, int r)
{
int x = A[r];
int temp;
int i = p-1;
int j = p;
for (; j<=r; j++)
{
if (A[j]<=x)
{
i++;
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
return i;
}
void quick_sort(int A[], int p, int r)
{
if (p<r)
{
int q = partition(A, p, r);
quick_sort(A, p, q-1);
quick_sort(A, q+1, r);
}
}
int main()
{
int A[] = {2, 8, 7, 1, 3, 5, 6, 4};
for (int i=0; i<=7; i++)
{
cout << A[i] << " ";
}
cout << endl;
quick_sort(A, 0, 7);
for (int i=0; i<=7; i++)
{
cout << A[i] << " ";
}
cout << endl;
return 0;
}
快排是一个分治策略
时间复杂度 \(\theta(n\lg{n})\)
原文:https://www.cnblogs.com/vito_wang/p/10808769.html