Quick Sort

9 篇文章 0 订阅
7 篇文章 0 订阅

Given an array of integers, sort the elements in the array in ascending order. The quick sort algorithm should be used to solve this problem.

Examples

  • {1} is sorted to {1}
  • {1, 2, 3} is sorted to {1, 2, 3}
  • {3, 2, 1} is sorted to {1, 2, 3}
  • {4, 2, -3, 6, 1} is sorted to {-3, 1, 2, 4, 6}

Corner Cases

  • What if the given array is null? In this case, we do not need to do anything.
  • What if the given array is of length zero? In this case, we do not need to do anything.
public class Solution {
  Random rand = new Random();

  public int[] quickSort(int[] array) {
    //base case
    if (array == null || array.length == 1) {
      return array;
    }
    quickSort(array, 0, array.length - 1);
    return array;
  }

  public void quickSort(int[] array, int left, int right) {
    //base case
    if(left >= right) {
      return;
    }
    //randomly select an element
    int pivotIndex = left + rand.nextInt(right - left + 1);
    swap(array, pivotIndex, right);

    //partition logic
    int i = 0;
    int j = right - 1;
    while(i <= j) {
      if(array[i] > array[right]) {
        swap(array, i, j);
        j--;
      } else {
        i++;
      }
    }
    swap(array, i, right);

    //subproblem
    quickSort(array, left, i-1);
    quickSort(array, i+1, right);
  }

  private void swap(int[] array, int i, int j) {
    int temp = array[i];
    array[i] = array[j];
    array[j] = temp;
  }
}

方法二:思路和方法一一样,但是把partition部分抽出来单独写成了一个method,if判断部分方法二写得更详细(但更不好理解),按方法一写即可。另外,random部分两种写法略有区别。

public class Solution {
  public int[] quickSort(int[] array) {
    //base case
    if(array == null) {
      return array;
    }
    quickSort(array, 0, array.length - 1);
    return array;
  }

  public void quickSort(int[] array, int left, int right) {
    //base case
    if(left >= right) {
      return;
    }
    //define a pivot and use the pivot to partiton the arrya
    int pivotPost = partition(array, left, right);
    
    //subproblem 
    quickSort(array, left, pivotPost - 1);
    quickSort(array, pivotPost + 1, right);
  }

  private int partition(int[] array, int left, int right){
    int pivotIndex = pivotIndex(left, right);
    int pivot = array[pivotIndex];

    swap(array, pivotIndex, right);
    int leftBound = left;
    int rightBound = right - 1;
    while(leftBound <= rightBound) {
      if(array[leftBound] < pivot) {
        leftBound++;
      } else if (array[rightBound] >= pivot) {
        rightBound--;
      } else {
        swap(array, leftBound++, rightBound--);
      }
    }
    swap(array, leftBound, right);
    return leftBound;
  }

  private int pivotIndex(int left, int right) {
    return left + (int)(Math.random() * (right - left + 1));
  }

  private void swap(int[] array, int left, int right) {
    int temp = array[left];
    array[left] = array[right];
    array[right] = temp;
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Quick sort is a popular sorting algorithm that works by partitioning an array into two sub-arrays, and then recursively sorting each sub-array. It is a divide-and-conquer algorithm that has an average time complexity of O(n log n), making it one of the fastest sorting algorithms. The basic idea behind quick sort is to select a pivot element, partition the array around the pivot element, and then recursively apply the same process to each of the sub-arrays. The partitioning process involves selecting a pivot element, rearranging the array so that all elements less than the pivot are on one side and all elements greater than the pivot are on the other side, and then returning the index of the pivot element. This pivot index is then used to divide the array into two sub-arrays, which are recursively sorted. Here's an example implementation of quick sort in Python: ``` def quick_sort(arr): if len(arr) <= 1: return arr else: pivot = arr[0] left = [] right = [] for i in range(1, len(arr)): if arr[i] < pivot: left.append(arr[i]) else: right.append(arr[i]) return quick_sort(left) + [pivot] + quick_sort(right) ``` This implementation selects the first element of the array as the pivot, and then uses list comprehensions to create the left and right sub-arrays. The left sub-array contains all elements less than the pivot, while the right sub-array contains all elements greater than or equal to the pivot. The function then recursively sorts the left and right sub-arrays and combines them with the pivot element to produce the final sorted array.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值