2021-07-13 快排

2021-07-13  本文已影响0人  hlchengzi

20分钟写出来的快排

private void quickSort(int[] nums,int start,int end){
      try {
          if(start>=end){
              return;
          }
          if(start+1 == end){
              if(nums[start]>nums[end]){
                  swap(nums,start,end);
              }
              return;
          }
          int mid = nums[start];
          List<Integer> left = new ArrayList<>();
          List<Integer> right = new ArrayList<>();
          for (int i = start+1; i <= end; i++) {
              if(nums[i]< mid){
                  left.add(nums[i]);
              }
              else {
                  right.add(nums[i]);
              }
          }
          int midNum = start+left.size();
          int index = start;
          for (int i = 0; i < left.size(); i++) {
              nums[index] = left.get(i);
              index++;
          }
          nums[index] = mid;
          index++;
          for (int i = 0; i < right.size(); i++) {
              nums[index] = right.get(i);
              index++;
          }
          quickSort(nums,start,midNum);
          quickSort(nums,midNum+1,end);
      } catch (Error e) {
          System.out.println("Exce:"+start +"  "+ end);
      }
  }

  public void swap(int[] nums,int left,int right){
      int temp = left;
      left = right;
      right = temp;
  }

今天公司宣布 14薪资慢慢变成16薪资了,唉,努力

上一篇 下一篇

猜你喜欢

热点阅读