美文网首页
快速排序初窥

快速排序初窥

作者: Yeahyeah313 | 来源:发表于2017-09-26 09:21 被阅读0次

partiton

1.在数组a[]找到一个枢纽元(pivot),pivot与第一个元素交换

2.a[low]为第二个元素,a[high]为倒数第一个元素

3.当low元素小于pivot,low++,当high元素大于pivot,high++,当i,j都停止自增时,若a[i]在a[j]左侧,则交换元素

4.当a[low]在a[high]右侧,停止

5.当high>first且A[high]>=pivot时,high--

6.当a[high] < pivot,交换,返回high,否则返回first

partition代码

 public int partition(int[] A, int first, int last){
        //确定一个pivot,左边小于,右边大于
        //区间第一个元素为pivot
        int pivot = A[first];
        int low = first + 1;
        int high = last;

        while (low < high){
            if(low < high && A[low] <= pivot){
                low++;
            }
            if(low < high && A[high] > pivot){
                high--;
            }
            if(low < high){
                int temp = A[high];
                A[high] = A[low];
                A[low] = temp;
            }
        }
        while (high > first && A[high] >= pivot){
            //查找结束时A[high]等于主元或查找不执行时只有两个元素
            high--;
        }
        if(pivot > A[high]){
            A[first] = A[high];
            A[high] = pivot;
            return high;
        }
        return first;
    }

整体代码

 public  int[] quickSort(int[] A, int n){
        sort(A, 0, n - 1);
        return A;
    }

    public void sort(int[] A, int first, int last){
        if (first < last){
            int partition = partition(A, first, last);
            sort(A, first, partition - 1);
            sort(A, partition+1, last);
        }
    }

    public int partition(int[] A, int first, int last){
        //确定一个pivot,左边小于,右边大于
        //区间第一个元素为pivot
        int pivot = A[first];
        int low = first + 1;
        int high = last;

        while (low < high){
            if(low < high && A[low] <= pivot){
                low++;
            }
            if(low < high && A[high] > pivot){
                high--;
            }
            if(low < high){
                int temp = A[high];
                A[high] = A[low];
                A[low] = temp;
            }
        }
        while (high > first && A[high] >= pivot){
            //查找结束时A[high]等于主元或查找不执行时只有两个元素
            high--;
        }
        if(pivot > A[high]){
            A[first] = A[high];
            A[high] = pivot;
            return high;
        }
        return first;
    }

emmm,影响性能的主要是partition代码,等慢慢学习后会继续修改

相关文章

  • 快速排序初窥

    partiton 1.在数组a[]找到一个枢纽元(pivot),pivot与第一个元素交换 2.a[low]为第二...

  • 初窥

    初窥世界的美好 白的风 白的肌肤 和盈盈一握的腰肢 在你的头发里 有一个蓝色大海的梦 蓝的帆船和蓝的浪花 自然是美...

  • 初窥

    这个积分管理项目已经到收尾的阶段了,感受良多。 对于错误处理这方面这周我的规划也更为清楚一些了。明天如果能够预期结...

  • 七大排序算法之快速排序

    七大排序算法之快速排序 @(算法笔记)[排序算法, 快速排序, C++实现] [TOC] 快速排序的介绍: 快速排...

  • 面试准备--排序

    堆排序 快速排序(simple) 快速排序(regular) 归并排序 Shell排序 插入排序 选择排序 冒泡排序

  • 排序

    插入排序 选择排序 冒泡排序 归并排序 快速排序* 三路快速排序

  • 算法笔记01-排序#2

    快速排序敢叫快速排序,那它一定得快。 快速排序 概述 快速排序也是分治排序的典型,它快,而且是原地排序。不过,要防...

  • PHP 实现快速排序

    导语 这篇了解下快速排序。 快速排序 快速排序(英语:Quicksort),又称划分交换排序(partition-...

  • 快速排序的Python实现

    目录 快速排序的介绍 快速排序的Python实现 快速排序的介绍 快速排序(quick sort)的采用了分治的策...

  • 数据结构与算法 快速排序

    起因:快速排序,又称分区交换排序,简称快排,之前没有了解过,抽空学习一下。 快速排序 1 快速排序 快速排序的定义...

网友评论

      本文标题:快速排序初窥

      本文链接:https://www.haomeiwen.com/subject/sdodextx.html