quick sort

快速排序由于排序效率在同为O(N*logN)的几种排序方法中效率较高,因此经常被采用,再加上快速排序思想----分治法也确实实用,因此很多软件公司的笔试面试,包括像腾讯,微软等知名IT公司都喜欢考这个,还有大大小的程序方面的考试如软考,考研中也常常出现快速排序的身影。

 

总的说来,要直接默写出快速排序还是有一定难度的,因为本人就自己的理解对快速排序作了下白话解释,希望对大家理解有帮助,达到快速排序,快速搞定。

 

快速排序是C.R.A.Hoare1962年提出的一种划分交换排序。它采用了一种分治的策略,通常称其为分治法(Divide-and-ConquerMethod)

该方法的基本思想是:

1.先从数列中取出一个数作为基准数。

2.分区过程,将比这个数大的数全放到它的右边,小于或等于它的数全放到它的左边。

3.再对左右区间重复第二步,直到各区间只有一个数。

 

虽然快速排序称为分治法,但分治法这三个字显然无法很好的概括快速排序的全部步骤。因此我的对快速排序作了进一步的说明:挖坑填数+分治法:

先来看实例吧,定义下面再给出(最好能用自己的话来总结定义,这样对实现代码会有帮助)。

 

以一个数组作为示例,取区间第一个数为基准数。

0

1

2

3

4

5

6

7

8

9

72

6

57

88

60

42

83

73

48

85

初始时,i = 0;  j = 9;   X = a[i] = 72

由于已经将a[0]中的数保存到X中,可以理解成在数组a[0]上挖了个坑,可以将其它数据填充到这来。

j开始向前找一个比X小或等于X的数。当j=8,符合条件,将a[8]挖出再填到上一个坑a[0]中。a[0]=a[8]; i++;  这样一个坑a[0]就被搞定了,但又形成了一个新坑a[8],这怎么办了?简单,再找数字来填a[8]这个坑。这次从i开始向后找一个大于X的数,当i=3,符合条件,将a[3]挖出再填到上一个坑中a[8]=a[3]; j--;

 

数组变为:

0

1

2

3

4

5

6

7

8

9

48

6

57

88

60

42

83

73

88

85

 i = 3;   j = 7;   X=72

再重复上面的步骤,先从后向前找,再从前向后找

j开始向前找,当j=5,符合条件,将a[5]挖出填到上一个坑中,a[3] = a[5]; i++;

i开始向后找,当i=5时,由于i==j退出。

此时,i = j = 5,而a[5]刚好又是上次挖的坑,因此将X填入a[5]

 

数组变为:

0

1

2

3

4

5

6

7

8

9

48

6

57

42

60

72

83

73

88

85

可以看出a[5]前面的数字都小于它,a[5]后面的数字都大于它。因此再对a[0…4]a[6…9]这二个子区间重复上述步骤就可以了。

 

 

对挖坑填数进行总结

1.i =L; j = R; 将基准数挖出形成第一个坑a[i]

2.j--由后向前找比它小的数,找到后挖出此数填前一个坑a[i]中。

3.i++由前向后找比它大的数,找到后也挖出此数填到前一个坑a[j]中。

4.再重复执行23二步,直到i==j,将基准数填入a[i]中。

照着这个总结很容易实现挖坑填数的代码:

int AdjustArray(int s[], int l, int r) //返回调整后基准数的位置

{

       int i = lj = r;

       int x = s[l]; //s[l]s[i]就是第一个坑

       while (i < j)

       {

              // 从右向左找小于x的数来填s[i]

              while(i < j && s[j] >= x)

                     j--; 

              if(i < j)

              {

                     s[i] = s[j]; //s[j]填到s[i]中,s[j]就形成了一个新的坑

                     i++;

              }

 

              // 从左向右找大于或等于x的数来填s[j]

              while(i < j && s[i] < x)

                     i++; 

              if(i < j)

              {

                     s[j] = s[i]; //s[i]填到s[j]中,s[i]就形成了一个新的坑

                     j--;

              }

       }

       //退出时,i等于j。将x填到这个坑中。

       s[i] = x;

 

       return i;

}

 

再写分治法的代码:

void quick_sort1(int s[], int l, int r)

{

       if (l < r)

    {

              int i = AdjustArray(slr);//先成挖坑填数法调整s[]

              quick_sort1(sli - 1); // 递归调用

              quick_sort1(si + 1, r);

       }

}

这样的代码显然不够简洁,对其组合整理下:

//快速排序

void quick_sort(int s[], int l, int r)

{

    if (l < r)

    {

              //Swap(s[l], s[(l + r) / 2]); //将中间的这个数和第一个数交换 参见注1

        int i = lj = rx = s[l];

        while (i < j)

        {

            while(i < j && s[j] >= x// 从右向左找第一个小于x的数

                            j--; 

            if(i < j)

                            s[i++] = s[j];

                    

            while(i < j && s[i] < x// 从左向右找第一个大于等于x的数

                            i++; 

            if(i < j)

                            s[j--] = s[i];

        }

        s[i] = x;

        quick_sort(sli - 1); // 递归调用

        quick_sort(si + 1, r);

    }

}

 

快速排序还有很多改进版本,如随机选择基准数,区间内数据较少时直接用另的方法排序以减小递归深度。有兴趣的筒子可以再深入的研究下。

 

1,有的书上是以中间的数作为基准数的,要实现这个方便非常方便,直接将中间的数和第一个数进行交换就可以了。

转载请标明出处,原文地址:http://www.cnblogs.com/morewindows/archive/2011/08/13/2137415.html

  • 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、付费专栏及课程。

余额充值