题解 | #排序#

排序

http://www.nowcoder.com/practice/2baf799ea0594abd974d37139de27896

import java.util.*;

public class Solution {
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 * 将给定数组排序
 * @param arr int整型一维数组 待排序的数组
 * @return int整型一维数组
 */
public int[] MySort (int[] arr) {
    // write code here
    QuickSort(arr,0,arr.length-1);
    return arr;
}
int Partition(int[] arr,int low,int high){
    int pivot=arr[low];
    while(low<high){
        while(low<high&&arr[high]>=pivot)--high;
        arr[low]=arr[high];
        while(low<high&&arr[low]<=pivot)++low;
        arr[high]=arr[low];
    }
    arr[low]=pivot;
    return low;
}

void QuickSort(int[]arr,int low ,int high){
    if(low<high){
        int pivotpos=Partition(arr,low,high);
        QuickSort(arr,low,pivotpos-1);
        QuickSort(arr,pivotpos+1,high);
    }
}

}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务