微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[数据结构与算法]快速排序

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

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

 

快速排序是C.R.A.Hoare于1962年提出的一种划分交换排序。它采用了一种分治的策略,通常称其为分治法(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

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

以上来自:

http://blog.csdn.net/morewindows/article/details/6684558

C#版本算法:

        public void QSort() 
        {
            RecQSort(0,numElements-1);
        }
        public void RecQSort(int first,int last) 
        {
            if ((last - first) <= 0)   
            {
                return;
            }
            else
            {
                int part=this.Partition(first,last);
                 RecQSort(first,part-1);
                 RecQSort(part+1,last);
            }
        }
        public int Partition(int first,int last) 
        {
            int pivotVal = arr[first];
            int theFirst = first;
            bool okSide;
            do
            {
                okSide=true;
                while(okSide)
                {
                    if(arr[first]>pivotVal)
                        okSide=false;
                    else
                    {
                        first++;
                        okSide=(first<=last);
                    }
                }
                okSide=false ;
                while(okSide) 
                {
                    if(arr[last]<=pivotVal)
                        okSide=false ;
                    else
                    {
                        last--;
                        okSide=(first<=last);
                    }
                }
                if(first<last)
                {
                    Swap(first,last);
                    this.displayElements();
                    first++;
                    last--;
                }
            } while (first <= last);
            Swap(theFirst,last);
            this.displayElements();
            return last;
              
        }
        public void Swap(int item1,int item2) 
        {
            int temp = arr[item1];
            arr[item1]=arr[item2];
            arr[item2] = temp;
        }

        public void test()
        {
            const int SIZE = 10;
            CArray theArray = new CArray(SIZE);
            Random random = new Random();

            for (int index = 0; index < SIZE; index++)
            {
                theArray.Insert(random.Next(100) + 1);
            }
            //49,38,65,97,26,13,27,49,55,4
            //theArray.Insert(49);
            //theArray.Insert(39);
            //theArray.Insert(65);
            //theArray.Insert(97);
            //theArray.Insert(26);
            //theArray.Insert(13);
            //theArray.Insert(27);
            //theArray.Insert(49);
            //theArray.Insert(55);
            //theArray.Insert(4);


            Console.WriteLine();
            theArray.displayElements();
            Console.WriteLine();
            theArray.QSort();
            theArray.displayElements();
      
        }

测试:


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐