6hz7vif 发表于 2024-10-10 04:21:20

快速排序的JS实现(JavaScript)


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">快排在我心中<span style="color: black;">始终</span>有一个<span style="color: black;">尤其</span>的位置,无论是<span style="color: black;">各样</span>笔试面试常考的排序算法,还是在现实实践中最快的排序算法,快排始终在一个<span style="color: black;">尤其</span>的位置上。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">算法的平均时间<span style="color: black;">繁杂</span>度: n *logn</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">最坏的时间<span style="color: black;">繁杂</span>度:n^2,<span style="color: black;">出现</span>的情景:完全逆序的序列</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">关于快排需要<span style="color: black;">晓得</span>一个概念: pivot,<span style="color: black;">通常</span>把这个译作准元(还是主元来着)。pivot,排序时<span style="color: black;">按照</span>pivot的值,将大于他的值移动到他的右边,<span style="color: black;">少于</span>他的值移动到他的左边。和归并排序<span style="color: black;">同样</span>快排<span style="color: black;">亦</span>是分而治之思想的一种典型应用。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在这个算法中<span style="color: black;">首要</span>会用到一个交换元素的<span style="color: black;">办法</span>:</p><span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">swap</span>(<span style="color: black;"> i , j ,arr</span>)</span>{
    <span style="color: black;">if</span> ( i == j ) <span style="color: black;">return</span>;
    <span style="color: black;">let</span>temp = arr;
    arr = arr;
    arr = temp;
    }<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span>选准元,选准元的方式可有<span style="color: black;">非常多</span><span style="color: black;">办法</span>,<span style="color: black;">咱们</span><span style="color: black;">运用</span><span style="color: black;">通常</span>的方式,<span style="color: black;">便是</span>比较一个序列最左边,中间元素,以及最右边元素的<span style="color: black;">体积</span>,<span style="color: black;">选择</span>三个元素的中位数<span style="color: black;">做为</span>准元,最后将中位数和倒数第二个元素交换,返回准元:</p>function selectPivot(<span style="color: black;">left</span>,<span style="color: black;">right</span>,arr){
    <span style="color: black;">let</span> mid = <span style="color: black;">Math</span>.floor (( <span style="color: black;">left</span> + <span style="color: black;">right</span> )/ <span style="color: black;">2</span>);
    <span style="color: black;">if</span>( arr[<span style="color: black;">left</span>] &gt; arr[<span style="color: black;">right</span>] ){
    <span style="color: black;">swap</span>(<span style="color: black;">left</span>,<span style="color: black;">right</span>,arr)
    };
    <span style="color: black;">if</span>( arr[<span style="color: black;">left</span>] &gt; arr ){
    <span style="color: black;">swap</span>( <span style="color: black;">left</span>, mid,arr )
    }
    <span style="color: black;">if</span>( arr[ mid ] &gt; arr[<span style="color: black;">right</span>] ){
    <span style="color: black;">swap</span>( mid,<span style="color: black;">right</span>,arr )
    }
    <span style="color: black;">swap</span>( mid,<span style="color: black;">right</span>-<span style="color: black;">1</span>,arr );
    <span style="color: black;">return</span> arr[ <span style="color: black;">right</span> - <span style="color: black;">1</span> ]
    }
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有了 pivot之后,就<span style="color: black;">能够</span><span style="color: black;">起始</span>主体部分了,将<span style="color: black;">少于</span>等于主元的元素放到他左边,大于等于主元的元素放到他右边,分治的时候<span style="color: black;">咱们</span>会<span style="color: black;">运用</span>到两个指针,分别从数组的(<span style="color: black;">初始</span>位置+1)和 (pivot-1)的位置扫描元素,扫描完毕之后,递归排序pivot前面的部分和后面的部分:</p>function <span style="color: black;">quickSort</span>(arr,<span style="color: black;">left</span> = <span style="color: black;">0</span>,<span style="color: black;">right</span>){
    <span style="color: black;">left</span> = <span style="color: black;">left</span> ,<span style="color: black;">right</span> = <span style="color: black;">right</span> !== undefined ? <span style="color: black;">right</span> : arr.length - <span style="color: black;">1</span>;
    <span style="color: black;">if</span>( <span style="color: black;">left</span> &lt; <span style="color: black;">right</span> ){
    <span style="color: black;">let</span> pivot = selectPivot( <span style="color: black;">left</span> , <span style="color: black;">right</span> ,arr );
    <span style="color: black;">let</span> i = <span style="color: black;">left</span>, j = <span style="color: black;">right</span> - <span style="color: black;">1</span>;
    <span style="color: black;">for</span>( ;; ){
    <span style="color: black;">while</span>( arr[++i] &lt; pivot ){};<span style="color: black;">while</span>( arr[ --j ] &gt; pivot ){};
    <span style="color: black;">if</span>( i &lt; j ){
    <span style="color: black;">swap</span>( i,j,arr )
    }<span style="color: black;">else</span>{
    <span style="color: black;">break</span>;
    }
    }
    <span style="color: black;">if</span>( i &lt; <span style="color: black;">right</span> - <span style="color: black;">1</span> )
    <span style="color: black;">swap</span>( i,<span style="color: black;">right</span>-<span style="color: black;">1</span>,arr );
    <span style="color: black;">quickSort</span>( arr,<span style="color: black;">left</span>, i - <span style="color: black;">1</span> );
    <span style="color: black;">quickSort</span>( arr,i+<span style="color: black;">1</span>,<span style="color: black;">right</span> )
    }<span style="color: black;">else</span>{
    <span style="color: black;">return</span> quickSorttings;
    }
    }
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以上,是看完浙大陈姥姥的数据结构之后自己用js重写的,与c语言版本的相比多了几处改动,之前完全仿照c语言版的写过一版快排,<span style="color: black;">然则</span>在一定数量级之后这个东西就<span style="color: black;">起始</span><span style="color: black;">显现</span>bug了。这次重写之后bug<span style="color: black;">处理</span>了,<span style="color: black;">显现</span>bug的<span style="color: black;">原由</span>在16行,在交换两个元素的时候,假如 i = right -1= pivot的位置,此时是不需要交换的,假如再交换,<span style="color: black;">实质</span>上是把主元给交换到比他之后的位置了,<span style="color: black;">因此呢</span>完全扰乱了算法的序列。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">后记:记得去年还在中国大学mooc完完整整的看完了陈姥姥的数据结构,没想到前几天面试的时候算法竟然生疏到这个程度了,哎,以后<span style="color: black;">每日</span>都得看一遍这个。</p>




页: [1]
查看完整版本: 快速排序的JS实现(JavaScript)