Quick Sort

Quick sort 是一种常用的算法。
quick sort 的常用算法实现。其中有三个重要的点

  1. 求出pivot点,求去该点的时候可以使用
    int pivot = nums[(start + end) / 2];
    不使用int pivot = nums[0] 或 int pivot = nums[end] 是为了避免出现时间复杂度为O(n ^ 2)的情况出现。当array是已经排序好的数组的时候,直接使用pivot = nums[start]会出现极端的情况。

  2. 写while循环的时候,一定要使用
    left <= right
    if 循环的时候,也是同样。当只是使用left < right 的情况下因为left 和 right 可能在循环的时候并没有相互的交错开会出现内存溢出的情况。

  3. 使用left 和 pivot点进行表的使用left < pivot,而不是使用
    left <= right, 同样这也是为了避免极端情况的发生。

使用LintCode的Integer Sort为例进行代码的处理。
description:
Given an integer array, sort it in ascending order. Use selection sort, bubble sort, insertion sort or any O(n2) algorithm.
Example
Given [3, 2, 1, 4, 5], return [1, 2, 3, 4, 5].

public class Solution {
    /**
     * @param A an integer array
     * @return void
     */
    public void sortIntegers(int[] A) {
        // Write your code here
        int start = 0;
        int end = A.length - 1;
        util(A, start, end);
    }
    private void util(int[] A, int start, int end) {
        if (start >= end) {
            return;
        }
        int pivot = A[(start + end) / 2];
        int left = start;
        int right = end;
        while (left <= right) {
            while (left <= right && A[left] < pivot) {
                left++;
            }
            while (left <= right && A[right] > pivot) {
                right--;
            }
            if (left <= right) {
                int temp = A[right];
                A[right] = A[left];
                A[left] = temp;
                right--;
                left++;
            }
        }
        util(A, start, right);
        util(A, left, end);
    }
}
  • 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
发出的红包

打赏作者

ncst

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值