QuickSort の文章、いいよ

本文介绍了一种高效的排序算法——快速排序。该算法采用分而治之的策略,通过递归地将待排序序列划分为两部分并分别进行排序,最终合并为完整的有序序列。文章详细解释了快速排序的基本思想及分区过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Quicksort

 German version  up

Quicksort is one of the fastest and simplest sorting algorithms [Hoa 62]. It works recursively by a divide-and-conquer strategyexplanation.

 

Idea

First, the sequence to be sorted a is partitioned into two parts, such that all elements of the first part b are less than or equal to all elements of the second part c (divide). Then the two parts are sorted separately by recursive application of the same procedure (conquer). Recombination of the two parts yields the sorted sequence (combine). Figure 1 illustrates this approach.

Divide and Conquer: Quicksort(n)
Figure 1:  Quicksort(n)

The first step of the partition procedure is choosing a comparison element x. All elements of the sequence that are less than x are placed in the first part, all elements greater than x are placed in the second part. For elements equal to x it does not matter into which part they come. In the following algorithm it may also happen that an element equal to x remains between the two parts.

 

Algorithm Partition
Input:sequence a0, ..., an-1 with n elements
Output:permutation of the sequence such that all elements a0, ..., aj are less than or equal to all elements ai, ..., an-1   (i > j)
Method:
  1. choose the element in the middle of the sequence as comparison element x

    let i = 0 and j = n-1

    while i<=j

    1. search for the first element ai which is greater than or equal to x

      search for the last element aj which is less than or equal to x

      if i<=j

      1. exchange ai and aj

        let i = i+1 and j = j-1

 

After partitioning the sequence, quicksort treats the two parts recursively by the same procedure. The recursion ends whenever a part consists of one element only.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值