快速排序原理簡介
快速排序是我們之前學(xué)習(xí)的冒泡排序的升級,他們都屬于交換類排序,都是采用不斷的比較和移動(dòng)來實(shí)現(xiàn)排序的。快速排序是一種非常高效的排序算法,它的實(shí)現(xiàn),增大了記錄的比較和移動(dòng)的距離,將關(guān)鍵字較大的記錄從前面直接移動(dòng)到后面,關(guān)鍵字較小的記錄從后面直接移動(dòng)到前面,從而減少了總的比較次數(shù)和移動(dòng)次數(shù)。同時(shí)采用“分而治之”的思想,把大的拆分為小的,小的拆分為更小的,其原理如下:對于給定的一組記錄,選擇一個(gè)基準(zhǔn)元素,通常選擇第一個(gè)元素或者最后一個(gè)元素,通過一趟掃描,將待排序列分成兩部分,一部分比基準(zhǔn)元素小,一部分大于等于基準(zhǔn)元素,此時(shí)基準(zhǔn)元素在其排好序后的正確位置,然后再用同樣的方法遞歸地排序劃分的兩部分,直到序列中的所有記錄均有序?yàn)橹埂?/p>
一,最小的k個(gè)數(shù)
輸入n個(gè)數(shù),找出其中最小的k個(gè)數(shù),例如輸入4,5,1,6,2,7,3,8,個(gè)數(shù)字,則最小的數(shù)字是1,2,3,4
基于O(n)的算法,可以用基于Partion函數(shù)解決這個(gè)問題,如果基于數(shù)組的第k個(gè)數(shù)字來調(diào)整,使得比第k個(gè)數(shù)字小的所有數(shù)字都位于數(shù)組的左邊,比第k個(gè)數(shù)組大的所有數(shù)字都位于數(shù)組的右邊,這樣調(diào)整之后數(shù)組左邊的k個(gè)數(shù)字就是最小的k個(gè)數(shù)字,不一定有序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in= new Scanner(System.in); int n=in.nextint(); int k=in.nextint(); int [] num= new int [n]; int [] out= new int [k]; for ( int i= 0 ;i<n;i++){ num[i]=in.nextint(); } Boolean b=GetMinK(n,num,k,out); if (b){ for ( int i= 0 ;i<k;i++){ System.out.print(out[i]+ " " ); } } } public static Boolean GetMinK( int uiInputNum, int [] pInputArray, int uiK, int [] pOutputArray){ if (pInputArray== null ||pOutputArray== null ||uiK>uiInputNum||uiInputNum<= 0 ||uiK<= 0 ){ return false ; } int start= 0 ; int end=uiInputNum- 1 ; int index=partition(pInputArray,start,end); while (index!=uiK- 1 ){ if (index>uiK- 1 ){ //index在k-1的右邊 end=index- 1 ; index=partition(pInputArray,start,end); } else { start=index+ 1 ; index=partition(pInputArray,start,end); } } for ( int i= 0 ;i<uiK;i++){ pOutputArray[i]=pInputArray[i]; } return true ; } //partion分區(qū)函數(shù),返回?cái)?shù)組a的首元素快排的索引值index public static int partition( int [] a, int start, int end){ int privot=a[start]; int i=start; int j=end; while (i<j){ while (i<j&&privot<=a[j]){ j--; } swap(a,i,j); while (i<j&&privot>=a[i]){ i++; } swap(a,i,j); } return i; } public static void swap( int [] a, int i, int j){ int t=a[i]; a[i]=a[j]; a[j]=t; } } |
二,數(shù)組中出現(xiàn)次數(shù)超過一半的數(shù)字
數(shù)組中有一個(gè)數(shù)字出現(xiàn)次數(shù)超過數(shù)組長度的一半,請找出這個(gè)數(shù)字。例如1,2,3,2,2,2,5,4,2,數(shù)字2在數(shù)組中出現(xiàn)了5次,超過數(shù)組長度的一半,輸出2
受快速排序的啟發(fā),在快速排序中,現(xiàn)在數(shù)組中選擇一個(gè)數(shù)字,然后調(diào)整數(shù)組中的數(shù)字的順序,使得比選中數(shù)字小的數(shù)字都排在它的左邊,比選中數(shù)字大的數(shù)字都排在它的右邊。
如果選中的數(shù)字的下標(biāo)剛好是n/2,那么這個(gè)數(shù)字就是數(shù)組中的中位數(shù)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in= new Scanner(System.in); int n=in.nextint(); int [] num= new int [n]; for ( int i= 0 ;i<n;i++){ num[i]=in.nextint(); } int b=GetHalfNum(n,num); if (b!=- 1 ){ System.out.println(b); } } public static int GetHalfNum( int uiInputNum, int [] pInputArray){ if (pInputArray== null ||uiInputNum<= 0 ){ return - 1 ; } int middle=uiInputNum>> 1 ; //長度的一半 int start= 0 ; int end=uiInputNum- 1 ; int index=partition(pInputArray,start,end); while (index!=middle){ //如果不等于長度的一半說明就沒有找到這個(gè)中位數(shù) if (index>middle){ end=index- 1 ; index=partition(pInputArray,start,end); } else { start=index+ 1 ; index=partition(pInputArray,start,end); } } return pInputArray[index]; //index=middle } public static int partition( int [] a, int start, int end){ int privot=a[start]; int i=start; int j=end; while (i<j){ while (i<j&&privot<=a[j]){ j--; } swap(a,i,j); while (i<j&&privot>=a[i]){ i++; } swap(a,i,j); } return i; } public static void swap( int [] a, int i, int j){ int t=a[i]; a[i]=a[j]; a[j]=t; } } |
三,找出數(shù)組中第k個(gè)最小的數(shù)
例如給定數(shù)組1,5,2,6,8,0,6中,第4小的數(shù)字是5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in= new Scanner(System.in); int n=in.nextint(); int k=in.nextint(); int [] num= new int [n]; //int[] out=new int[k]; for ( int i= 0 ;i<n;i++){ num[i]=in.nextint(); } int b=GetMinK(n,num,k); if (b!=- 1 ){ System.out.println(b); } } public static int GetMinK( int uiInputNum, int [] pInputArray, int uiK){ if (pInputArray== null ||uiK>uiInputNum||uiInputNum<= 0 ||uiK<= 0 ){ return - 1 ; } int start= 0 ; int end=uiInputNum- 1 ; int index=partition(pInputArray,start,end); while (index!=uiK- 1 ){ //如果index不是k-1的位置 if (index>uiK- 1 ){ end=index- 1 ; index=partition(pInputArray,start,end); } else { start=index+ 1 ; index=partition(pInputArray,start,end); } } return pInputArray[index]; //返回的這個(gè)位置的數(shù)值 } public static int partition( int [] a, int start, int end){ int privot=a[start]; int i=start; int j=end; while (i<j){ while (i<j&&privot<=a[j]){ j--; } swap(a,i,j); while (i<j&&privot>=a[i]){ i++; } swap(a,i,j); } return i; } public static void swap( int [] a, int i, int j){ int t=a[i]; a[i]=a[j]; a[j]=t; } } |
總結(jié)
以上就是本文關(guān)于Java編程基于快速排序的三個(gè)算法題實(shí)例代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/tuke_tuke/article/details/51476630