Java 快速排序(QuickSort)原理及實(shí)現(xiàn)代碼
快速排序(QuickSort )是常用到的效率比較高的一種排序算法,在面試過(guò)程中也經(jīng)常提及。下面就詳細(xì)講解一下他的原理、給出一個(gè)Java版本的實(shí)現(xiàn)。
快速排序思想:
快速排序的過(guò)程——挖坑填數(shù)法(這是一個(gè)很形象的名稱(chēng)),對(duì)一個(gè)元素集合R[ low ... high ] ,首先取一個(gè)數(shù)(一般是R[low] )做參照 , 以R[low]為基準(zhǔn)重新排列所有的元素。
所有比R[low]小的放前面,所有比R[low] 大的放后面,然后以R[low]為分界,對(duì)R[low ... high] 劃分為兩個(gè)子集和,再做劃分。直到low >= high 。
比如:對(duì)R={37, 40, 38, 42, 461, 5, 7, 9, 12}進(jìn)行一趟快速排序的過(guò)程如下(注:下面描述的內(nèi)容中元素下表從 0 開(kāi)始):
| 原始序列 | 37 | 40 | 38 | 42 | 461 | 5 | 7 | 9 | 12 |
| 一:high-->low | 12 | 40 | 38 | 42 | 461 | 5 | 7 | 9 | 12 |
| 一:low --> high | 12 | 40 | 38 | 42 | 461 | 5 | 7 | 9 | 40 |
| 二:high-->low | 12 | 9 | 38 | 42 | 461 | 5 | 7 | 9 | 40 |
| 二:low --> high | 12 | 9 | 38 | 42 | 461 | 5 | 7 | 38 | 40 |
| 三:high --> low | 12 | 9 | 7 | 42 | 461 | 5 | 7 | 38 | 40 |
| 三:low -->high | 12 | 9 | 7 | 42 | 461 | 5 | 42 | 38 | 40 |
| 四:high --> low | 12 | 9 | 7 | 5 | 461 | 5 | 42 | 38 | 40 |
| 四:low --> high | 12 | 9 | 7 | 5 | 461 | 461 | 42 | 38 | 40 |
| 一趟排序結(jié)果 | 12 | 9 | 7 | 5 | 37 | 461 | 42 | 38 | 40 |
開(kāi)始選取基準(zhǔn) base = 37,初始位置下表 low = 0 , high = 8 , 從high=8,開(kāi)始如果R[8] < base , 將high位置中的內(nèi)容寫(xiě)入到R[low]中, 將high位置空出來(lái), low = low +1 ;
從low開(kāi)始探測(cè),由于low=1 , R[low] > base ,所以將R[low]寫(xiě)入到R[high] , high = high -1 ;
檢測(cè)到low < high ,所以第一趟快速排序仍需繼續(xù):
此時(shí)low=1,high=7,因?yàn)?R[high] < base ,所以將 R[high] 寫(xiě)入到到R[low]中,low = low + 1;
從low開(kāi)始探測(cè),low = 2 , R[low] >base ,所以講R[low]寫(xiě)入到R[high],high=high-1;
繼續(xù)檢測(cè)到 low 小于high
此時(shí)low=2,high=6,同理R[high] < base ,將R[high] 寫(xiě)入到R[low]中,low=low+1;
從low繼續(xù)探測(cè),low = 3 , high=6 , R[low] > base , 將R[low]寫(xiě)入到R[high]中,high = high-1;
繼續(xù)探測(cè)到low小于high
此時(shí)low=3,high=5,同理R[high] < base,將R[high]寫(xiě)入到R[low]中,low = low +1;
從low繼續(xù)探測(cè),low = 4,high=5,由于R[low] > base , 將R[low]寫(xiě)入到R[high]中,high = high -1 ;
此時(shí)探測(cè)到low == high == 4 ;該位置即是base所在的位置,將base寫(xiě)入到該位置中.
然后再對(duì)子序列Rs1 = {12,9,7,5} 和 Rs2={461,42,38,40}做一趟快速排序,直到Rsi中只有一個(gè)元素,或沒(méi)有元素。
(注: 在以上表單中可以看到一趟排序中有一些重復(fù)的數(shù)據(jù)(原始數(shù)據(jù)中沒(méi)有重復(fù)的數(shù)據(jù)),這是因?yàn)闆](méi)有清除該位置的數(shù)據(jù),我們?cè)谔囟ǖ臅r(shí)間看該內(nèi)存塊的數(shù)據(jù)依然是它,直到下一次將數(shù)據(jù)寫(xiě)入該位置位置 —— 在此該位置的數(shù)據(jù)是一個(gè)沒(méi)有意義臟數(shù)據(jù),稱(chēng)之為 “坑”)
快速排序的Java實(shí)現(xiàn):
private static boolean isEmpty(int[] n) {
return n == null || n.length == 0;
}
// ///////////////////////////////////////////////////
/**
* 快速排序算法思想——挖坑填數(shù)方法:
*
* @param n 待排序的數(shù)組
*/
public static void quickSort(int[] n) {
if (isEmpty(n))
return;
quickSort(n, 0, n.length - 1);
}
public static void quickSort(int[] n, int l, int h) {
if (isEmpty(n))
return;
if (l < h) {
int pivot = partion(n, l, h);
quickSort(n, l, pivot - 1);
quickSort(n, pivot + 1, h);
}
}
private static int partion(int[] n, int start, int end) {
int tmp = n[start];
while (start < end) {
while (n[end] >= tmp && start < end)
end--;
if (start < end) {
n[start++] = n[end];
}
while (n[start] < tmp && start < end)
start++;
if (start < end) {
n[end--] = n[start];
}
}
n[start] = tmp;
return start;
}
在代碼中有這樣一個(gè)函數(shù):
public static void quickSortSwap(int[] n, int l, int h)
關(guān)于快速排序就寫(xiě)到這里了。
相關(guān)文章
SpringBoot框架整合Mybatis簡(jiǎn)單攻略
這篇文章主要介紹了SpringBoot框架整合Mybatis的簡(jiǎn)單攻略,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-10-10
Spring中的EurekaServer啟動(dòng)詳解
這篇文章主要介紹了Spring中的EurekaServer啟動(dòng)詳解,初始化eureka,包含eureka集群的同步和發(fā)布注冊(cè),這個(gè)方法時(shí)重寫(xiě)ServletContextListener#contextInitialized,是eureka啟動(dòng)的入口了,需要的朋友可以參考下2023-11-11
SpringBoot集成E-mail發(fā)送各種類(lèi)型郵件
這篇文章主要為大家詳細(xì)介紹了SpringBoot集成E-mail發(fā)送各種類(lèi)型郵件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
java實(shí)現(xiàn)同步回調(diào)的示例代碼
同步回調(diào)是一種在調(diào)用代碼中同步執(zhí)行回調(diào)函數(shù)的編程模式,在Java中,通過(guò)定義和實(shí)現(xiàn)接口來(lái)構(gòu)建同步回調(diào),本文就來(lái)介紹一下如何實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-09-09
fastJson泛型如何轉(zhuǎn)換的實(shí)現(xiàn)
這篇文章主要介紹了fastJson泛型如何轉(zhuǎn)換的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Java生成范圍內(nèi)隨機(jī)整數(shù)的三種方法
在Java中生成隨機(jī)數(shù)的場(chǎng)景有很多,下面這篇文章主要給大家介紹了關(guān)于Java生成范圍內(nèi)隨機(jī)整數(shù)的三種方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
解決Java?結(jié)構(gòu)化數(shù)據(jù)處理開(kāi)源庫(kù)?SPL的問(wèn)題
這篇文章主要介紹了Java?結(jié)構(gòu)化數(shù)據(jù)處理開(kāi)源庫(kù)?SPL的問(wèn)題,Scala提供了較豐富的結(jié)構(gòu)化數(shù)據(jù)計(jì)算函數(shù),但編譯型語(yǔ)言的特點(diǎn),也使它不能成為理想的結(jié)構(gòu)化數(shù)據(jù)計(jì)算類(lèi)庫(kù),對(duì)此內(nèi)容感興趣的朋友一起看看吧2022-03-03

