对于一个int数组,请编写一个快速排序算法,对数组元素排序。
给定一个int数组A及数组的大小n,请返回排序后的数组。
测试样例:
[1,2,3,5,2,3],6
[1,2,2,3,3,5]
我的提交
-- coding:utf-8 --
class QuickSort:
def quickSort(self, A, n):
write code here
self.quick(A, 0, n - 1)
return A
def quick(self, A, left, right):
if left >= right:
return
t = A[left]
l = left
r = right
while l < r:
while t <= A[r] and l < r:
r -= 1
if l < r:
A[l] = A[r]
l += 1
while t >= A[l] and l < r:
l += 1
if l < r:
A[r] = A[l]
r -= 1
# if l > r:
A[l] = t
if left < r-1:
self.quick(A, left, r-1)
if r + 1 < right:
self.quick(A, r + 1, right)
return
参考答案
python版本1
-- coding:utf-8 --
class QuickSort:
def quickSort(self, A, n):
write code here
return self.quick_sort(A)
def quick_sort(self, seq):
# 快排: 取一个基准数,自己意愿取最左
#
if seq == []:
return []
else:
pivot = seq[0]
lesser = self.quick_sort([x for x in seq[1:] if x < pivot])
greater = self.quick_sort([y for y in seq[1:] if y >= pivot])
return lesser+[pivot]+greater
python版本2
-- coding:utf-8 --
class QuickSort:
def func(self,a,left, right):
if left>=right:
return
j = left-1
i = left
while i<=right:
if a[i]<=a[right]:
j = j+1
a[j],a[i]=a[i],a[j]
i += 1
self.func(a,left,j-1)
self.func(a,j+1,right)
def quickSort(self, A, n):
left = 0
right = n-1
self.func(A,left,right)
return A
write code here
python 版本2
-- coding:utf-8 --
class QuickSort:
def quickSort(self, A, n):
write code here
self.quick(A, 0, n - 1)
return A
def quick(self, A, left, right):
if left >= right:
return
t = A[left]
l = left # 这里不能为 l = left + 1
r = right
while l < r:
while t <= A[r] and l < r:
r -= 1
while t >= A[l] and l < r:
l += 1
if l < r:
A[l], A[r] = A[r],A[l]
A[left], A[r] = A[r],A[left]
if left < r-1:
self.quick(A, left, r-1)
if r + 1 < right:
self.quick(A, r + 1, right)
return
if name == 'main':
q = QuickSort()
print(q.quickSort([8,12,14,17,26,27,28,35,36,44,44,48,54],6))
java版本
public class QuickSort {
public static void quickSort(int[] arr) {
if (arr == null || arr.length < 2) {
return;
}
process(arr, 0, arr.length - 1);
}
public static void process(int[] arr, int left, int right) {
if (left < right) {
int random = left + (int) (Math.random() * (right - left + 1));
swap(arr, random, right);
int mid = partition(arr, left, right);
process(arr, left, mid - 1);
process(arr, mid + 1, right);
}
}
public static int partition(int[] arr, int left, int right) {
int pivot = left - 1;
int index = left;
while (index <= right) {
if (arr[index] <= arr[right]) {
swap(arr, ++pivot, index);
}
index++;
}
return pivot;
}
public static void swap(int[] arr, int index1, int index2) {
int tmp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = tmp;
}
public static int[] generateArray(int len, int range) {
if (len < 1) {
return null;
}
int[] arr = new int[len];
for (int i = 0; i < len; i++) {
arr[i] = (int) (Math.random() * range);
}
return arr;
}
public static void printArray(int[] arr) {
if (arr == null || arr.length == 0) {
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static boolean isSorted(int[] arr) {
if (arr == null || arr.length < 2) {
return true;
}
for (int i = 1; i < arr.length; i++) {
if (arr[i - 1] > arr[i]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int len = 10;
int range = 10;
int testTimes = 50000;
for (int i = 0; i < testTimes; i++) {
int[] arr = generateArray(len, range);
quickSort(arr);
if (!isSorted(arr)) {
System.out.println("Wrong Case:");
printArray(arr);
break;
}
}
int len2 = 13;
int range2 = 10;
int testTimes2 = 50000;
for (int i = 0; i < testTimes2; i++) {
int[] arr = generateArray(len2, range2);
quickSort(arr);
if (!isSorted(arr)) {
System.out.println("Wrong Case:");
printArray(arr);
break;
}
}
}
}