DELPHI 代码
procedure TForm1.Button2Click(Sender: TObject);
var
A1: array[0..20] of Integer;
I: Integer;
begin
for I := Low(A1) to High(A1) do
begin
Randomize;
A1[i] := Random(100);
end;
QuickSort1(A1, Low(a), High(A1), 1);
for I := Low(A1) to High(A1) do
begin
Randomize;
Memo1.Lines.Add(IntToStr(A1[i]));
end;
end;
procedure QuickSort1(var A: array of Integer; iLo, iHi: Integer; code: Integer);
var
Lo, Hi, Mid, T: Integer;
begin
Lo := iLo;
Hi := iHi;
Mid := A[(Lo + Hi) div 2];
repeat
case code of
1:
begin
while ((A[Lo] mod 2) = 0)do
Inc(Lo);
while ((A[Hi] mod 2) = 1 )
do Dec(Hi);
if (Lo <= Hi) then
begin
T := A[Lo];
A[Lo] := A[Hi];
A[Hi] := T;
Inc(Lo);
Dec(Hi);
end;
end;
2:
begin
while A[Lo] < Mid do Inc(Lo);
while A[Hi] > Mid do Dec(Hi);
if Lo <= Hi then
begin
T := A[Lo];
A[Lo] := A[Hi];
A[Hi] := T;
Inc(Lo);
Dec(Hi);
end;
end;
end;
until Lo > Hi;
if code = 1 then
begin
QuickSort1(A, iLo, hi, 2);
QuickSort1(A, lo, ihi, 2);
end
else
begin
if Hi > iLo then QuickSort1(A, iLo, Hi, 2);
if Lo < iHi then QuickSort1(A, Lo, iHi, 2);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
A1: array[0..20] of Integer;
I: Integer;
begin
for I := Low(A1) to High(A1) do
begin
Randomize;
A1[i] := Random(100);
end;
QuickSort1(A1, Low(a), High(A1), 1);
for I := Low(A1) to High(A1) do
begin
Randomize;
Memo1.Lines.Add(IntToStr(A1[i]));
end;
end;
procedure QuickSort1(var A: array of Integer; iLo, iHi: Integer; code: Integer);
var
Lo, Hi, Mid, T: Integer;
begin
Lo := iLo;
Hi := iHi;
Mid := A[(Lo + Hi) div 2];
repeat
case code of
1:
begin
while ((A[Lo] mod 2) = 0)do
Inc(Lo);
while ((A[Hi] mod 2) = 1 )
do Dec(Hi);
if (Lo <= Hi) then
begin
T := A[Lo];
A[Lo] := A[Hi];
A[Hi] := T;
Inc(Lo);
Dec(Hi);
end;
end;
2:
begin
while A[Lo] < Mid do Inc(Lo);
while A[Hi] > Mid do Dec(Hi);
if Lo <= Hi then
begin
T := A[Lo];
A[Lo] := A[Hi];
A[Hi] := T;
Inc(Lo);
Dec(Hi);
end;
end;
end;
until Lo > Hi;
if code = 1 then
begin
QuickSort1(A, iLo, hi, 2);
QuickSort1(A, lo, ihi, 2);
end
else
begin
if Hi > iLo then QuickSort1(A, iLo, Hi, 2);
if Lo < iHi then QuickSort1(A, Lo, iHi, 2);
end;
end;
C++ 代码
#include <iostream>
using namespace std;
void QucikSort(int a[], int lo, int hi, int code)
{
int ilo, ihi, mid, temp;
ilo = lo;
ihi = hi;
mid = a[(lo + hi) / 2];
while (ilo <= ihi)
{
switch(code)
{
case 1:
{
while ((a[ilo] % 2) == 0)
++ilo;
while ((a[ihi] % 2) == 1)
--ihi;
if (ilo <= ihi)
{
temp = a[ilo];
a[ilo] = a[ihi];
a[ihi] = temp;
++ilo;
--ihi;
}
break;
}
case 2:
{
while (a[ilo] < mid)
++ilo;
while (a[ihi] > mid)
--ihi;
if (ilo <= ihi)
{
temp = a[ilo];
a[ilo] = a[ihi];
a[ihi] = temp;
++ilo;
--ihi;
}
break;
}
default: break;
}
}
if (code == 1)
{
QucikSort(a, lo, ihi, 2);
QucikSort(a, ilo, hi, 2);
}
else if (code == 2)
{
if (ihi > lo)
QucikSort(a, lo, ihi, 2);
if (ilo < hi)
QucikSort(a, ilo, hi, 2);
}
}
int main()
{
int arr[7] = {5, 3, 1, 2, 4, 8, 9};
QucikSort(arr, 0, 6, 1);
for (int i = 0; i <= 6; i++)
cout<<arr[i]<<endl;
return 0;
}
#include <iostream>
using namespace std;
void QucikSort(int a[], int lo, int hi, int code)
{
int ilo, ihi, mid, temp;
ilo = lo;
ihi = hi;
mid = a[(lo + hi) / 2];
while (ilo <= ihi)
{
switch(code)
{
case 1:
{
while ((a[ilo] % 2) == 0)
++ilo;
while ((a[ihi] % 2) == 1)
--ihi;
if (ilo <= ihi)
{
temp = a[ilo];
a[ilo] = a[ihi];
a[ihi] = temp;
++ilo;
--ihi;
}
break;
}
case 2:
{
while (a[ilo] < mid)
++ilo;
while (a[ihi] > mid)
--ihi;
if (ilo <= ihi)
{
temp = a[ilo];
a[ilo] = a[ihi];
a[ihi] = temp;
++ilo;
--ihi;
}
break;
}
default: break;
}
}
if (code == 1)
{
QucikSort(a, lo, ihi, 2);
QucikSort(a, ilo, hi, 2);
}
else if (code == 2)
{
if (ihi > lo)
QucikSort(a, lo, ihi, 2);
if (ilo < hi)
QucikSort(a, ilo, hi, 2);
}
}
int main()
{
int arr[7] = {5, 3, 1, 2, 4, 8, 9};
QucikSort(arr, 0, 6, 1);
for (int i = 0; i <= 6; i++)
cout<<arr[i]<<endl;
return 0;
}
思路: 先用快速排序把偶数排前边,奇数排后边,这里只用快速排序算法第前半部分就可以完成一次遍历
第2步是就是调用常规快速排序了