Posted 浠g爜姘戝伐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
鍐掓场鎺掑簭
馃挕鍩烘湰鎬濇兂|婕旂ず|绠楁硶浠g爜|鎬ц兘
浠ユ暟鎹?銆?銆?銆?銆?銆?銆?銆?涓轰緥锛屼互涓嬩粎灞曠ず涓€瓒熷揩閫熸帓搴忔搷浣溿€?/p>
澶嶅埗棣栧厓绱犱綔涓烘灑杞村厓绱狅紝浣垮叾鎵€鍦ㄤ綅缃厓绱犲彲琚敼鍐欒€屼笉涓㈠け鏁版嵁
棣栧厛浠庡悗鍚戝墠姣旇緝锛屽皢姣旀灑杞村厓绱?灏忕殑鍏冪礌璧嬬粰low鎸囧悜鐨勪綅缃厓绱狅紱6>4锛宧igh鎸囧悜鍓嶄竴涓厓绱狅紝鎺ョ潃杩涜姣旇緝
2<4锛屽皢2璧嬬粰low鎸囧悜鐨勪綅缃厓绱?/p>
灏嗘瘮鏋㈣酱鍏冪礌澶х殑鏁版嵁璧嬬粰low鎸囧悜鐨勪綅缃厓绱犲悗锛屼粠鍓嶅悜鍚庢瘮杈冿紝灏嗘瘮鏋㈣酱鍏冪礌澶х殑鏁版嵁璧嬬粰鍙充晶high鎸囧悜鐨勪綅缃厓绱?/p>
8>4锛屽皢8璧嬬粰high鎸囧悜鐨勪綅缃厓绱?/p>
鐒跺悗浠庡悗鍚戝墠姣旇緝锛岄噸澶嶄互涓婃搷浣?/p>
1<4
浠庡墠鍚戝悗姣旇緝
3<4锛宭ow鎸囧悜鍚庝竴浣?/p>
7>4
浠庡悗鍚戝墠姣旇緝
5>4锛宧igh鎸囧悜鍓嶄竴涓厓绱狅紝涓巐ow閲嶅悎锛屾鏃跺惊鐜粓姝?/p>
灏嗘灑杞村厓绱犺祴缁檒ow鎸囧悜鐨勪綅缃厓绱?/p>
鎸夌収閫掑綊鎬濇兂锛屽垎鍒宸﹀彸涓や晶鍏冪礌杩涜涓婅堪鎺掑簭鎿嶄綔
閲嶅杩欑鎺掑簭鎿嶄綔锛岀洿鍒版暣缁勬暟鎹湁搴忥紝鎺掑簭缁堟
绗簩瓒?/strong>鎺掑簭鍚庣殑搴忓垪涓?nbsp;1 2 3 4 5 7 8 6
绗笁瓒?/strong>鎺掑簭鍚庣殑搴忓垪涓?nbsp;1 2 3 4 5 6 7 8
蹇€熸帓搴忕畻娉曞垎涓轰袱涓儴鍒嗭紝鍒嗗埆涓哄揩閫熷垝鍒嗙畻娉曞拰閫掑綊鎺掑簭绠楁硶
馃摐C++浠g爜
1#include<iostream>
2using namespace std;
3int Partition(int a[], int low,int high)
4{
5 int pivotkey = a[low];
6 while (low < high)
7 {
8 while (low<high&&a[high]>pivotkey)high--;
9 a[low] = a[high];
10 while (low < high&&a[low] < pivotkey)low++;
11 a[high] = a[low];
12 }
13 a[low] = pivotkey;
14 return low;
15}
16void QuickSort(int a[], int low, int high)
17{
18 int pivotloc;
19 if (low < high)
20 {
21 pivotloc = Partition(a, low, high);
22 QuickSort(a, low, pivotloc - 1);
23 QuickSort(a, pivotloc + 1, high);
24 }
25}
26int main()
27{
28 int a[10] = { 4,8,3,7,5,1,2,6 };
29 QuickSort(a,0,7);
30 for (int i = 0; i < 8; i++)
31 {
32 cout << a[i] << " ";
33 }
34 return 0;
35}
馃摐Python浠g爜
1def Partition(nums,low,high):
2 pivotkey=a[low]
3 while low<high:
4 while low<high and nums[high]>pivotkey:
5 high-=1
6 nums[low]=nums[high]
7 while low<high and nums[low]<pivotkey:
8 low+=1
9 nums[high]=nums[low]
10 a[low]=pivotkey
11 return low
12def QSort(nums,low,high):
13 if low<high:
14 pivotloc=Partition(nums,low,high)
15 QSort(nums,low,pivotloc-1)
16 QSort(nums,pivotloc+1,high)
17def QuickSort(nums):
18 QSort(nums,0,len(nums)-1)
19a=[4,8,3,7,5,1,2,6]
20QuickSort(a)
21print(a)
馃摐Java浠g爜
1package text;
2
3public class QuickSort {
4 public static void main(String[]args){
5 int a[]={4,8,3,7,5,1,2,6};
6 Sort(a);
7 ShowArr(a);
8 }
9 public static int Partition(int[]a,int low,int high){
10 int pivotkey=a[low];
11 while(low<high){
12 while(low<high&&a[high]>pivotkey)high--;
13 a[low]=a[high];
14 while(low<high&&a[low]<pivotkey)low++;
15 a[high]=a[low];
16 }
17 a[low]=pivotkey;
18 return low;
19 }
20 public static void QSort(int[]a,int low,int high){
21 int pivotloc;
22 if(low<high){
23 pivotloc=Partition(a,low,high);
24 QSort(a,low,pivotloc-1);
25 QSort(a,pivotloc+1,high);
26 }
27 }
28 public static void Sort(int[]a){
29 QSort(a,0,a.length-1);
30 }
31 public static void ShowArr(int[]a){
32 for(int x:a)
33 System.out.print(x+" ");
34 System.out.println();
35 }
36
37}
蹇€熸帓搴忕畻娉曟槸涓€绉?span class="mq-361">涓嶇ǔ瀹?/strong>鐨勬帓搴忕畻娉曪紝骞冲潎鏃堕棿澶嶆潅搴︿负O(nlogn)锛屽钩鍧囩┖闂村鏉傚害涓?span class="mq-363">O(logn)
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章