Android面試題目之四: 歸併排序
歸併的核心思想是歸併。歸併的速度直接影響到演算法的快慢。
1. 簡單插入歸併
public static class MergeSorter1 implements Sorter {
public void sort(int[] values) {
int step = 1;
while (step <= values.length) {
int index = 0;
while (index < values.length) {
int low, high, end = 0;
if (index + step < values.length) {
low = index;
high = index + step;
if (index + step + step < values.length) {
end = index + step + step;
} else {
end = values.length;
}
merge(values, low, high, end);
index = end;
} else {
break;
}
}
step = 2 * step;
}
}
private void merge(int[] values, int low, int high, int end) {
while (high < end) {
while (low < high) {
if (values[low] <= values[high]) {
low++;
} else {
break;
}
}
if (low != high) {
int temp = values[high];
for (int i = low; i <= high; ++i) {
int _temp = values[i];
values[i] = temp;
temp = _temp;
}
}
high++;
}
}
}
簡單插入歸併的性能列印:
.... take time: 654
2. 額外建立一個臨時數組歸併:
public static class MergeSorter2 implements Sorter {
public void sort(int[] values) {
int step = 1;
while (step <= values.length) {
int index = 0;
while (index < values.length) {
merge(values, index, index + step, index + step + step);
index = index + step + step;
}
step = 2 * step;
}
}
private void merge(int[] values, int low, int high, int end) {
if (low >= values.length) {
return;
}
if (high > values.length) {
high = values.length;
}
if (end > values.length) {
end = values.length;
}
int originLow = low;
int originHigh = high;
int originEnd = end;
int tempValues = new int[end - low];
int p = 0;
while (p < tempValues.length) {
if (low < originHigh) {
if (high == originEnd || values[low] <= values[high]) {
tempValues[p] = values[low];
low++;
p++;
}
}
if (high < originEnd) {
if (low == originHigh || values[low] >= values[high]) {
tempValues[p] = values[high];
high++;
p++;
}
}
}
for (int i = 0; i < tempValues.length; ++i) {
values[originLow + i] = tempValues[i];
}
}
}
性能列印:
.... take time: 1520
3. 使用公用臨時數組歸併
public static class MergeSorter3 implements Sorter {
public int tempValues;
public void sort(int[] values) {
boolean isInTemp = false;
int originValues = values;
tempValues = new int[values.length];
int step = 1;
while (step <= values.length) {
int index = 0;
while (index < values.length) {
merge(values, index, index + step, index + step + step);
index = index + step + step;
}
// printValues(tempValues);
int _tempValues = tempValues;
tempValues = values;
values = _tempValues;
isInTemp = !isInTemp;
step = 2 * step;
}
if (isInTemp) {
for (int i = 0; i < values.length; ++i) {
originValues[i] = values[i];
}
}
}
private void merge(int[] values, int low, int high, int end) {
if (low >= values.length) {
return;
}
if (high > values.length) {
high = values.length;
}
if (end > values.length) {
end = values.length;
}
int pLow = low;
int pHigh = high;
int pMerged = low;
while (pMerged < end) {
if (pLow < high) {
if (pHigh >= end) {
tempValues[pMerged] = values[pLow];
pLow++;
pMerged++;
} else if (values[pLow] < values[pHigh]) {
tempValues[pMerged] = values[pLow];
pLow++;
pMerged++;
} else if (values[pLow] == values[pHigh]) {
tempValues[pMerged] = values[pLow];
pLow++;
pMerged++;
tempValues[pMerged] = values[pHigh];
pHigh++;
pMerged++;
}
}
if (pHigh < end) {
if (pLow >= high) {
tempValues[pMerged] = values[pHigh];
pHigh++;
pMerged++;
} else if (values[pLow] > values[pHigh]) {
tempValues[pMerged] = values[pHigh];
pHigh++;
pMerged++;
} else if (values[pLow] == values[pHigh]) {
tempValues[pMerged] = values[pLow];
pLow++;
pMerged++;
tempValues[pMerged] = values[pHigh];
pHigh++;
pMerged++;
}
}
}
}
}
時間列印:
.... take time: 784
4. 在演算法3基礎上進行優化
public static class MergeSorter4 implements Sorter {
public int tempValues;
public void sort(int[] values) {
boolean isInTemp = false;
int originValues = values;
tempValues = new int[values.length];
int step = 1;
while (step <= values.length) {
int index = 0;
while (index < values.length) {
int end = index + step + step;
merge(values, index, index + step, end);
index = end;
}
// printValues(tempValues);
int _tempValues = tempValues;
tempValues = values;
values = _tempValues;
isInTemp = !isInTemp;
step = 2 * step;
}
if (isInTemp) {
for (int i = 0; i < values.length; ++i) {
originValues[i] = values[i];
}
}
}
private void merge(int[] values, int low, int high, int end) {
if (low >= values.length) {
return;
}
if (high > values.length) {
high = values.length;
}
if (end > values.length) {
end = values.length;
}
int pLow = low;
int pHigh = high;
int pMerged = low;
while (pLow < high && pHigh < end) {
if (values[pLow] <= values[pHigh]) {
tempValues[pMerged++] = values[pLow++];
} else {
tempValues[pMerged++] = values[pHigh++];
}
}
while (pLow < high) {
tempValues[pMerged++] = values[pLow++];
}
while(pHigh< end) {
tempValues[pMerged++] = values[pHigh++];
}
}
}
列印時間:
.... take time: 573
5. Java SDK歸併
public static class SDKSorter implements Sorter {
public void sort(int[] values) {
Arrays.parallelSort(values);
}
}
時間列印:
.... take time: 338
可見android SDK的歸併效率很高,值得使用。


※如何恢復未釋放租約的HDFS文件
※C中調用HttpWebRequest類中Get/Post請求無故失效的詭異問題
※Windows10系統PHP開發環境配置
※從 Vue 1.x 遷移—Vue.js
※初識 tk.mybatis.mapper 通用mapper
TAG:科技優家 |
※澳大利亞工作室宣布虛擬桌面題目表:The Crooked Crown
※VulnHub中LazySysAdmin 題目詳解
※2019年Common Application申請文書題目解讀及優秀文書寫作指南
※Essay精析:CBS今年的Essay題目,有點不一樣
※RCTF 2018 Magic題目詳解
※4道與CVE結合web題目
※新年開工,這份 Oracle DBA的面試題目,看看你合格不?
※Twitter上一道很火的題目,你能答對嗎?
※2020 申請er 注意!CA公布最新Essay題目!誰在無所事事?
※開源聚合杯網路空間安全大賽官方部分題目writeup
※路由器漏洞復現分析第三彈:DVRF INTRO題目分析
※Angelababy化身美拍千萬女神出題官 網友:題目滿滿少女心!
※強網杯web題目四道
※分享一些PHP面試題目
※IPhone XR降價後表現不錯,但蘋果還有四大難題目前無解
※emmm,不知道該起個啥題目
※TOPIK考試題目看不懂?題干高頻核心辭彙大整理
※居然噴《青春豬頭少年》垃圾,知名youtober表示被題目騙到:「為什麼取這種讓人誤會的糞作書名?」
※你們要的60屆TOPIK中高級答案來了!題目和解析全都有
※戚薇女兒lucky火了,「喜提」作文題目,網友:送分題?