题解 | JAVA QuickSort#排序# [P0]

排序

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

时间O(NlogN)
空间 avg O(logN), worst O(N), 栈高
import java.util.*;

public class Solution {
    public int[] MySort (int[] arr) {
      quickSort(0, arr.length-1, arr);
      return arr;
    }
  
    void quickSort(int start, int end, int[] arr) {
      if (start < end) {
        int pivot = partition(start, end, arr);
        quickSort(start, pivot-1, arr);
        quickSort(pivot+1, end, arr);
      }
    }
  
    int partition(int start, int end, int[] arr) {
      int pV = arr[start];
      int i = start, j = end;
      while (i < j) {
        while (i < end && arr[i] <= pV) i++;
        while (j > start && arr[j] >= pV) j--;
        if (i < j) swap(i, j, arr);
      }
      swap(start, j, arr);
      return j;
    }
  
    void swap(int i, int j, int[] arr) {
      int tmp = arr[i];
      arr[i] = arr[j];
      arr[j] = tmp;
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 11:30
找工作7个月,投了7000封,3段世界五百强实习,才有一个offer,牛油们肯定比我强吧
码农索隆:不对不对不对,实习经历这么厉害,简历也没少投,问题出在哪呢
点赞 评论 收藏
分享
07-05 16:23
门头沟学院 Java
mengnankk:我投了300,约了5 6个面试。感觉项目写的太多了。一个项目就写五六个亮点,不是把整个项目的功能描述下。其他的没啥,简历看起来有点长
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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