js版本
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 将给定数组排序
* @param arr int整型一维数组 待排序的数组
* @return int整型一维数组
*/
function MySort( arr ) {
function quicksortb(arr) {
if (arr.length==1){
return arr
}
return quicksort(arr, 0, arr.length - 1);
}
function swap(arr, left, right) {
let tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;
}
function partion(arr, start, end) {
let pivot = end;
let boundary = start - 1;
for (i = start; i <= end; i++) {
if (arr[i] <= arr[pivot]) {
swap(arr, ++boundary, i);
}
}
return boundary;
}
function quicksort(arr, start, end) {
if (start >= end) {
return 0;
}
let boundary = partion(arr, start, end);
quicksort(arr, start, boundary - 1);
quicksort(arr, boundary + 1, end);
return arr;
}
return quicksortb(arr)
}
module.exports = {
MySort : MySort
};
python版本:
class Solution:
def MySort(self , arr: List[int]) -> List[int]:
def quicksort(arr,left,right):
if left>=right:
return 0
boundary = partion(arr,left,right)
quicksort(arr,boundary+1,right)
quicksort(arr, left, boundary-1)
return arr
def partion(arr,left,right):
boundary = -1+left
index = right
for i in range(left,right+1):
if arr[i]<=arr[index]:
boundary+=1
swap(arr,i,boundary)
return boundary
def swap(arr,left,right):
tmp = arr[left]
arr[left]=arr[right]
arr[right]=tmp
return quicksort(arr,0,len(arr)-1)