题解 | #排序#

排序

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;
    }
    
    private void quickSort(int[] array,int start,int end){
        if(start<end){
            int key = array[start];
            int i = start;
            for(int j=start;j<=end;j++){
                if(array[j]<key){
                    i++; //标识位加1
                    swap(i,j,array);
                }
            }
            array[start] = array[i];
            array[i]=key;
            quickSort(array,start,i-1);
            quickSort(array,i+1,end);
        }
    }
    
    private void swap(int i,int j,int[] array){
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务