用java代碼實(shí)現(xiàn)堆排序 用java代碼實(shí)現(xiàn)堆排序的方法

請(qǐng)給出java幾種排序方法

java常見(jiàn)的排序分為:

成都創(chuàng)新互聯(lián)從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、成都網(wǎng)站建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元巴林右旗做網(wǎng)站,已為上家服務(wù),為巴林右旗各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792

1 插入類排序

主要就是對(duì)于一個(gè)已經(jīng)有序的序列中,插入一個(gè)新的記錄。它包括:直接插入排序,折半插入排序和希爾排序

2 交換類排序

這類排序的核心就是每次比較都要“交換”,在每一趟排序都會(huì)兩兩發(fā)生一系列的“交換”排序,但是每一趟排序都會(huì)讓一個(gè)記錄排序到它的最終位置上。它包括:起泡排序,快速排序

3 選擇類排序

每一趟排序都從一系列數(shù)據(jù)中選擇一個(gè)最大或最小的記錄,將它放置到第一個(gè)或最后一個(gè)為位置交換,只有在選擇后才交換,比起交換類排序,減少了交換記錄的時(shí)間。屬于它的排序:簡(jiǎn)單選擇排序,堆排序

4 歸并類排序

將兩個(gè)或兩個(gè)以上的有序序列合并成一個(gè)新的序列

5 基數(shù)排序

主要基于多個(gè)關(guān)鍵字排序的。

下面針對(duì)上面所述的算法,講解一些常用的java代碼寫的算法

二 插入類排序之直接插入排序

直接插入排序,一般對(duì)于已經(jīng)有序的隊(duì)列排序效果好。

基本思想:每趟將一個(gè)待排序的關(guān)鍵字按照大小插入到已經(jīng)排序好的位置上。

算法思路,從后往前先找到要插入的位置,如果小于則就交換,將元素向后移動(dòng),將要插入數(shù)據(jù)插入該位置即可。時(shí)間復(fù)雜度為O(n2),空間復(fù)雜度為O(1)

package sort.algorithm;

public class DirectInsertSort {

public static void main(String[] args) {

// TODO Auto-generated method stub

int data[] = { 2, 6, 10, 3, 9, 80, 1, 16, 27, 20 };

int temp, j;

for (int i = 1; i data.length; i++) {

temp = data[i];

j = i - 1;

// 每次比較都是對(duì)于已經(jīng)有序的

while (j = 0 data[j] temp) {

data[j + 1] = data[j];

j--;

}

data[j + 1] = temp;

}

// 輸出排序好的數(shù)據(jù)

for (int k = 0; k data.length; k++) {

System.out.print(data[k] + " ");

}

}

}

三 插入類排序之折半插入排序(二分法排序)

條件:在一個(gè)已經(jīng)有序的隊(duì)列中,插入一個(gè)新的元素

折半插入排序記錄的比較次數(shù)與初始序列無(wú)關(guān)

思想:折半插入就是首先將隊(duì)列中取最小位置low和最大位置high,然后算出中間位置mid

將中間位置mid與待插入的數(shù)據(jù)data進(jìn)行比較,

如果mid大于data,則就表示插入的數(shù)據(jù)在mid的左邊,high=mid-1;

如果mid小于data,則就表示插入的數(shù)據(jù)在mid的右邊,low=mid+1

最后整體進(jìn)行右移操作。

時(shí)間復(fù)雜度O(n2),空間復(fù)雜度O(1)

package sort.algorithm;

//折半插入排序

public class HalfInsertSort {

public static void main(String[] args) {

int data[] = { 2, 6, 10, 3, 9, 80, 1, 16, 27, 20 };

// 存放臨時(shí)要插入的元素?cái)?shù)據(jù)

int temp;

int low, mid, high;

for (int i = 1; i data.length; i++) {

temp = data[i];

// 在待插入排序的序號(hào)之前進(jìn)行折半插入

low = 0;

high = i - 1;

while (low = high) {

mid = (low + high) / 2;

if (temp data[mid])

high = mid - 1;

else

// low=high的時(shí)候也就是找到了要插入的位置,

// 此時(shí)進(jìn)入循環(huán)中,將low加1,則就是要插入的位置了

low = mid + 1;

}

// 找到了要插入的位置,從該位置一直到插入數(shù)據(jù)的位置之間數(shù)據(jù)向后移動(dòng)

for (int j = i; j = low + 1; j--)

data[j] = data[j - 1];

// low已經(jīng)代表了要插入的位置了

data[low] = temp;

}

for (int k = 0; k data.length; k++) {

System.out.print(data[k] + " ");

}

}

}

四 插入類排序之希爾排序

希爾排序,也叫縮小增量排序,目的就是盡可能的減少交換次數(shù),每一個(gè)組內(nèi)最后都是有序的。

將待續(xù)按照某一種規(guī)則分為幾個(gè)子序列,不斷縮小規(guī)則,最后用一個(gè)直接插入排序合成

空間復(fù)雜度為O(1),時(shí)間復(fù)雜度為O(nlog2n)

算法先將要排序的一組數(shù)按某個(gè)增量d(n/2,n為要排序數(shù)的個(gè)數(shù))分成若干組,每組中記錄的下標(biāo)相差d.對(duì)每組中全部元素進(jìn)行直接插入排序,然后再用一個(gè)較小的增量(d/2)對(duì)它進(jìn)行分組,在每組中再進(jìn)行直接插入排序。當(dāng)增量減到1時(shí),進(jìn)行直接插入排序后,排序完成。

package sort.algorithm;

public class ShellSort {

public static void main(String[] args) {

int a[] = { 1, 54, 6, 3, 78, 34, 12, 45, 56, 100 };

double d1 = a.length;

int temp = 0;

while (true)

{

//利用這個(gè)在將組內(nèi)倍數(shù)減小

//這里依次為5,3,2,1

d1 = Math.ceil(d1 / 2);

//d為增量每個(gè)分組之間索引的增量

int d = (int) d1;

//每個(gè)分組內(nèi)部排序

for (int x = 0; x d; x++)

{

//組內(nèi)利用直接插入排序

for (int i = x + d; i a.length; i += d) {

int j = i - d;

temp = a[i];

for (; j = 0 temp a[j]; j -= d) {

a[j + d] = a[j];

}

a[j + d] = temp;

}

}

if (d == 1)

break;

}

for (int i = 0; i a.length; i++)

System.out.print(a[i]+" ");

}

}

五 交換類排序之冒泡排序

交換類排序核心就是每次比較都要進(jìn)行交換

冒泡排序:是一種交換排序

每一趟比較相鄰的元素,較若大小不同則就會(huì)發(fā)生交換,每一趟排序都能將一個(gè)元素放到它最終的位置!每一趟就進(jìn)行比較。

時(shí)間復(fù)雜度O(n2),空間復(fù)雜度O(1)

package sort.algorithm;

//冒泡排序:是一種交換排序

public class BubbleSort {

// 按照遞增順序排序

public static void main(String[] args) {

// TODO Auto-generated method stub

int data[] = { 2, 6, 10, 3, 9, 80, 1, 16, 27, 20, 13, 100, 37, 16 };

int temp = 0;

// 排序的比較趟數(shù),每一趟都會(huì)將剩余最大數(shù)放在最后面

for (int i = 0; i data.length - 1; i++) {

// 每一趟從開(kāi)始進(jìn)行比較,將該元素與其余的元素進(jìn)行比較

for (int j = 0; j data.length - 1; j++) {

if (data[j] data[j + 1]) {

temp = data[j];

data[j] = data[j + 1];

data[j + 1] = temp;

}

}

}

for (int i = 0; i data.length; i++)

System.out.print(data[i] + " ");

}

}

java快速排序簡(jiǎn)單代碼

.example-btn{color:#fff;background-color:#5cb85c;border-color:#4cae4c}.example-btn:hover{color:#fff;background-color:#47a447;border-color:#398439}.example-btn:active{background-image:none}div.example{width:98%;color:#000;background-color:#f6f4f0;background-color:#d0e69c;background-color:#dcecb5;background-color:#e5eecc;margin:0 0 5px 0;padding:5px;border:1px solid #d4d4d4;background-image:-webkit-linear-gradient(#fff,#e5eecc 100px);background-image:linear-gradient(#fff,#e5eecc 100px)}div.example_code{line-height:1.4em;width:98%;background-color:#fff;padding:5px;border:1px solid #d4d4d4;font-size:110%;font-family:Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;word-break:break-all;word-wrap:break-word}div.example_result{background-color:#fff;padding:4px;border:1px solid #d4d4d4;width:98%}div.code{width:98%;border:1px solid #d4d4d4;background-color:#f6f4f0;color:#444;padding:5px;margin:0}div.code div{font-size:110%}div.code div,div.code p,div.example_code p{font-family:"courier new"}pre{margin:15px auto;font:12px/20px Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;white-space:pre-wrap;word-break:break-all;word-wrap:break-word;border:1px solid #ddd;border-left-width:4px;padding:10px 15px} 排序算法是《數(shù)據(jù)結(jié)構(gòu)與算法》中最基本的算法之一。排序算法可以分為內(nèi)部排序和外部排序,內(nèi)部排序是數(shù)據(jù)記錄在內(nèi)存中進(jìn)行排序,而外部排序是因排序的數(shù)據(jù)很大,一次不能容納全部的排序記錄,在排序過(guò)程中需要訪問(wèn)外存。常見(jiàn)的內(nèi)部排序算法有:插入排序、希爾排序、選擇排序、冒泡排序、歸并排序、快速排序、堆排序、基數(shù)排序等。以下是快速排序算法:

快速排序是由東尼·霍爾所發(fā)展的一種排序算法。在平均狀況下,排序 n 個(gè)項(xiàng)目要 Ο(nlogn) 次比較。在最壞狀況下則需要 Ο(n2) 次比較,但這種狀況并不常見(jiàn)。事實(shí)上,快速排序通常明顯比其他 Ο(nlogn) 算法更快,因?yàn)樗膬?nèi)部循環(huán)(inner loop)可以在大部分的架構(gòu)上很有效率地被實(shí)現(xiàn)出來(lái)。

快速排序使用分治法(Divide and conquer)策略來(lái)把一個(gè)串行(list)分為兩個(gè)子串行(sub-lists)。

快速排序又是一種分而治之思想在排序算法上的典型應(yīng)用。本質(zhì)上來(lái)看,快速排序應(yīng)該算是在冒泡排序基礎(chǔ)上的遞歸分治法。

快速排序的名字起的是簡(jiǎn)單粗暴,因?yàn)橐宦?tīng)到這個(gè)名字你就知道它存在的意義,就是快,而且效率高!它是處理大數(shù)據(jù)最快的排序算法之一了。雖然 Worst Case 的時(shí)間復(fù)雜度達(dá)到了 O(n?),但是人家就是優(yōu)秀,在大多數(shù)情況下都比平均時(shí)間復(fù)雜度為 O(n logn) 的排序算法表現(xiàn)要更好,可是這是為什么呢,我也不知道。好在我的強(qiáng)迫癥又犯了,查了 N 多資料終于在《算法藝術(shù)與信息學(xué)競(jìng)賽》上找到了滿意的答案:

快速排序的最壞運(yùn)行情況是 O(n?),比如說(shuō)順序數(shù)列的快排。但它的平攤期望時(shí)間是 O(nlogn),且 O(nlogn) 記號(hào)中隱含的常數(shù)因子很小,比復(fù)雜度穩(wěn)定等于 O(nlogn) 的歸并排序要小很多。所以,對(duì)絕大多數(shù)順序性較弱的隨機(jī)數(shù)列而言,快速排序總是優(yōu)于歸并排序。

1. 算法步驟

從數(shù)列中挑出一個(gè)元素,稱為 "基準(zhǔn)"(pivot);

重新排序數(shù)列,所有元素比基準(zhǔn)值小的擺放在基準(zhǔn)前面,所有元素比基準(zhǔn)值大的擺在基準(zhǔn)的后面(相同的數(shù)可以到任一邊)。在這個(gè)分區(qū)退出之后,該基準(zhǔn)就處于數(shù)列的中間位置。這個(gè)稱為分區(qū)(partition)操作;

遞歸地(recursive)把小于基準(zhǔn)值元素的子數(shù)列和大于基準(zhǔn)值元素的子數(shù)列排序;

2. 動(dòng)圖演示

代碼實(shí)現(xiàn) JavaScript 實(shí)例 function quickSort ( arr , left , right ) {

var len = arr. length ,

? ? partitionIndex ,

? ? left = typeof left != 'number' ? 0 : left ,

? ? right = typeof right != 'number' ? len - 1 : right ;

if ( left

java堆排序代碼

//從a[index]到a[len]除了a[index]外其它元素滿足一個(gè)堆,把a(bǔ)[index]調(diào)整到合適位置

//這個(gè)堆滿足父節(jié)點(diǎn)孩子結(jié)點(diǎn),且要保證2*index能取到index的左孩子,

public static void adjustHeap(int[] a,int index,int len){

int scn=a[index];

for(int i=2*index;i=m;i*=2){

if(ima[i]a[i+1])i+=1;

if(!a[i]scn)break;

a[index]=a[i];index=i;

}

a[index]=scn;

}

//數(shù)組a從a[1]開(kāi)始存放元素,如果想從a[0]開(kāi)始則要調(diào)整adjustHeap代碼,以便滿足完全二叉樹(shù)

//性質(zhì),代碼未經(jīng)測(cè)試

public static void heapSort(int[] a){

for(int i=(a.length-1)/2;i0;i--)

adjustHeap(a,i,a.length-1);

int tmp;

for(int i=a.length-1;i1;i--){

tmp=a[i];

a[i]=a[1];

a[1]=tmp;

adjustHeap(a,1,i-1);

}

}

寫一個(gè)簡(jiǎn)單的JAVA排序程序

// 排序

public class Array

{

public static int[] random(int n) //產(chǎn)生n個(gè)隨機(jī)數(shù),返回整型數(shù)組

{

if (n0)

{

int table[] = new int[n];

for (int i=0; itable.length; i++)

table[i] = (int)(Math.random()*100); //產(chǎn)生一個(gè)0~100之間的隨機(jī)數(shù)

return table; //返回一個(gè)數(shù)組

}

return null;

}

public static void print(int[] table) //輸出數(shù)組元素

{

if (table!=null)

for (int i=0; itable.length; i++)

System.out.print(" "+table[i]);

System.out.println();

}

public static void insertSort(int[] table) //直接插入排序

{ //數(shù)組是引用類型,元素值將被改變

System.out.println("直接插入排序");

for (int i=1; itable.length; i++) //n-1趟掃描

{

int temp=table[i], j; //每趟將table[i]插入到前面已排序的序列中

// System.out.print("移動(dòng)");

for (j=i-1; j-1 temptable[j]; j--) //將前面較大元素向后移動(dòng)

{

// System.out.print(table[j]+", ");

table[j+1] = table[j];

}

table[j+1] = temp; //temp值到達(dá)插入位置

System.out.print("第"+i+"趟: ");

print(table);

}

}

public static void shellSort(int[] table) //希爾排序

{

System.out.println("希爾排序");

for (int delta=table.length/2; delta0; delta/=2) //控制增量,增量減半,若干趟掃描

{

for (int i=delta; itable.length; i++) //一趟中若干組,每個(gè)元素在自己所屬組內(nèi)進(jìn)行直接插入排序

{

int temp = table[i]; //當(dāng)前待插入元素

int j=i-delta; //相距delta遠(yuǎn)

while (j=0 temptable[j]) //一組中前面較大的元素向后移動(dòng)

{

table[j+delta] = table[j];

j-=delta; //繼續(xù)與前面的元素比較

}

table[j+delta] = temp; //插入元素位置

}

System.out.print("delta="+delta+" ");

print(table);

}

}

private static void swap(int[] table, int i, int j) //交換數(shù)組中下標(biāo)為i、j的元素

{

if (i=0 itable.length j=0 jtable.length i!=j) //判斷i、j是否越界

{

int temp = table[j];

table[j] = table[i];

table[i] = temp;

}

}

public static void bubbleSort(int[] table) //冒泡排序

{

System.out.println("冒泡排序");

boolean exchange=true; //是否交換的標(biāo)記

for (int i=1; itable.length exchange; i++) //有交換時(shí)再進(jìn)行下一趟,最多n-1趟

{

exchange=false; //假定元素未交換

for (int j=0; jtable.length-i; j++) //一次比較、交換

if (table[j]table[j+1]) //反序時(shí),交換

{

int temp = table[j];

table[j] = table[j+1];

table[j+1] = temp;

exchange=true; //有交換

}

System.out.print("第"+i+"趟: ");

print(table);

}

}

public static void quickSort(int[] table) //快速排序

{

quickSort(table, 0, table.length-1);

}

private static void quickSort(int[] table, int low, int high) //一趟快速排序,遞歸算法

{ //low、high指定序列的下界和上界

if (lowhigh) //序列有效

{

int i=low, j=high;

int vot=table[i]; //第一個(gè)值作為基準(zhǔn)值

while (i!=j) //一趟排序

{

while (ij vot=table[j]) //從后向前尋找較小值

j--;

if (ij)

{

table[i]=table[j]; //較小元素向前移動(dòng)

i++;

}

while (ij table[i]vot) //從前向后尋找較大值

i++;

if (ij)

{

table[j]=table[i]; //較大元素向后移動(dòng)

j--;

}

}

table[i]=vot; //基準(zhǔn)值的最終位置

System.out.print(low+".."+high+", vot="+vot+" ");

print(table);

quickSort(table, low, j-1); //前端子序列再排序

quickSort(table, i+1, high); //后端子序列再排序

}

}

public static void selectSort(int[] table) //直接選擇排序

{

System.out.println("直接選擇排序");

for (int i=0; itable.length-1; i++) //n-1趟排序

{ //每趟在從table[i]開(kāi)始的子序列中尋找最小元素

int min=i; //設(shè)第i個(gè)數(shù)據(jù)元素最小

for (int j=i+1; jtable.length; j++) //在子序列中查找最小值

if (table[j]table[min])

min = j; //記住最小元素下標(biāo)

if (min!=i) //將本趟最小元素交換到前邊

{

int temp = table[i];

table[i] = table[min];

table[min] = temp;

}

System.out.print("第"+i+"趟: ");

print(table);

}

}

private static void sift(int[] table, int low, int high) //將以low為根的子樹(shù)調(diào)整成最小堆

{ //low、high是序列下界和上界

int i=low; //子樹(shù)的根

int j=2*i+1; //j為i結(jié)點(diǎn)的左孩子

int temp=table[i]; //獲得第i個(gè)元素的值

while (j=high) //沿較小值孩子結(jié)點(diǎn)向下篩選

{

if (jhigh table[j]table[j+1]) //數(shù)組元素比較(改成為最大堆)

j++; //j為左右孩子的較小者

if (temptable[j]) //若父母結(jié)點(diǎn)值較大(改成為最大堆)

{

table[i]=table[j]; //孩子結(jié)點(diǎn)中的較小值上移

i=j; //i、j向下一層

j=2*i+1;

}

else

j=high+1; //退出循環(huán)

}

table[i]=temp; //當(dāng)前子樹(shù)的原根值調(diào)整后的位置

System.out.print("sift "+low+".."+high+" ");

print(table);

}

public static void heapSort(int[] table)

{

System.out.println("堆排序");

int n=table.length;

for (int j=n/2-1; j=0; j--) //創(chuàng)建最小堆

sift(table, j, n-1);

// System.out.println("最小堆? "+isMinHeap(table));

for (int j=n-1; j0; j--) //每趟將最小值交換到后面,再調(diào)整成堆

{

int temp = table[0];

table[0] = table[j];

table[j] = temp;

sift(table, 0, j-1);

}

}

public static void mergeSort(int[] X) //歸并排序

{

System.out.println("歸并排序");

int n=1; //已排序的子序列長(zhǎng)度,初值為1

int[] Y = new int[X.length]; //Y數(shù)組長(zhǎng)度同X數(shù)組

do

{

mergepass(X, Y, n); //一趟歸并,將X數(shù)組中各子序列歸并到Y(jié)中

print(Y);

n*=2; //子序列長(zhǎng)度加倍

if (nX.length)

{

mergepass(Y, X, n); //將Y數(shù)組中各子序列再歸并到X中

print(X);

n*=2;

}

} while (nX.length);

}

private static void mergepass(int[] X, int[] Y, int n) //一趟歸并

{

System.out.print("子序列長(zhǎng)度n="+n+" ");

int i=0;

while (iX.length-2*n+1)

{

merge(X,Y,i,i+n,n);

i += 2*n;

}

if (i+nX.length)

merge(X,Y,i,i+n,n); //再一次歸并

else

for (int j=i; jX.length; j++) //將X剩余元素復(fù)制到Y(jié)中

Y[j]=X[j];

}

private static void merge(int[] X, int[] Y, int m, int r, int n) //一次歸并

{

int i=m, j=r, k=m;

while (ir jr+n jX.length) //將X中兩個(gè)相鄰子序列歸并到Y(jié)中

if (X[i]X[j]) //較小值復(fù)制到Y(jié)中

Y[k++]=X[i++];

else

Y[k++]=X[j++];

while (ir) //將前一個(gè)子序列剩余元素復(fù)制到Y(jié)中

Y[k++]=X[i++];

while (jr+n jX.length) //將后一個(gè)子序列剩余元素復(fù)制到Y(jié)中

Y[k++]=X[j++];

}

public static void main(String[] args)

{

// int[] table = {52,26,97,19,66,8,49};//Array.random(9);{49,65,13,81,76,97,38,49};////{85,12,36,24,47,30,53,91,76};//;//{4,5,8,1,2,7,3,6};// {32,26,87,72,26,17};//

int[] table = {13,27,38,49,97,76,49,81}; //最小堆

System.out.print("關(guān)鍵字序列: ");

Array.print(table);

// Array.insertSort(table);

// Array.shellSort(table);

// Array.bubbleSort(table);

// Array.quickSort(table);

// Array.selectSort(table);

// Array.heapSort(table);

// Array.mergeSort(table);

System.out.println("最小堆序列? "+Array.isMinHeap(table));

}

//第9章習(xí)題

public static boolean isMinHeap(int[] table) //判斷一個(gè)數(shù)據(jù)序列是否為最小堆

{

if (table==null)

return false;

int i = table.length/2 -1; //最深一棵子樹(shù)的根結(jié)點(diǎn)

while (i=0)

{

int j=2*i+1; //左孩子

if (jtable.length)

if (table[i]table[j])

return false;

else

if (j+1table.length table[i]table[j+1]) //右孩子

return false;

i--;

}

return true;

}

}

/*

程序運(yùn)行結(jié)果如下:

關(guān)鍵字序列: 32 26 87 72 26 17 8 40

直接插入排序

第1趟排序: 26 32 87 72 26 17 8 40

第2趟排序: 26 32 87 72 26 17 8 40

第3趟排序: 26 32 72 87 26 17 8 40

第4趟排序: 26 26 32 72 87 17 8 40 //排序算法穩(wěn)定

第5趟排序: 17 26 26 32 72 87 8 40

第6趟排序: 8 17 26 26 32 72 87 40

第7趟排序: 8 17 26 26 32 40 72 87

關(guān)鍵字序列: 42 1 74 25 45 29 87 53

直接插入排序

第1趟排序: 1 42 74 25 45 29 87 53

第2趟排序: 1 42 74 25 45 29 87 53

第3趟排序: 1 25 42 74 45 29 87 53

第4趟排序: 1 25 42 45 74 29 87 53

第5趟排序: 1 25 29 42 45 74 87 53

第6趟排序: 1 25 29 42 45 74 87 53

第7趟排序: 1 25 29 42 45 53 74 87

關(guān)鍵字序列: 21 12 2 40 99 97 68 57

直接插入排序

第1趟排序: 12 21 2 40 99 97 68 57

第2趟排序: 2 12 21 40 99 97 68 57

第3趟排序: 2 12 21 40 99 97 68 57

第4趟排序: 2 12 21 40 99 97 68 57

第5趟排序: 2 12 21 40 97 99 68 57

第6趟排序: 2 12 21 40 68 97 99 57

第7趟排序: 2 12 21 40 57 68 97 99

關(guān)鍵字序列: 27 38 65 97 76 13 27 49 55 4

希爾排序

delta=5 13 27 49 55 4 27 38 65 97 76

delta=2 4 27 13 27 38 55 49 65 97 76

delta=1 4 13 27 27 38 49 55 65 76 97

關(guān)鍵字序列: 49 38 65 97 76 13 27 49 55 4 //嚴(yán)書(shū)

希爾排序

delta=5 13 27 49 55 4 49 38 65 97 76

delta=2 4 27 13 49 38 55 49 65 97 76 //與嚴(yán)書(shū)不同

delta=1 4 13 27 38 49 49 55 65 76 97

關(guān)鍵字序列: 65 34 25 87 12 38 56 46 14 77 92 23

希爾排序

delta=6 56 34 14 77 12 23 65 46 25 87 92 38

delta=3 56 12 14 65 34 23 77 46 25 87 92 38

delta=1 12 14 23 25 34 38 46 56 65 77 87 92

關(guān)鍵字序列: 84 12 43 62 86 7 90 91

希爾排序

delta=4 84 7 43 62 86 12 90 91

delta=2 43 7 84 12 86 62 90 91

delta=1 7 12 43 62 84 86 90 91

關(guān)鍵字序列: 32 26 87 72 26 17

冒泡排序

第1趟排序: 26 32 72 26 17 87

第2趟排序: 26 32 26 17 72 87

第3趟排序: 26 26 17 32 72 87

第4趟排序: 26 17 26 32 72 87

第5趟排序: 17 26 26 32 72 87

關(guān)鍵字序列: 1 2 3 4 5 6 7 8

冒泡排序

第1趟排序: 1 2 3 4 5 6 7 8

關(guān)鍵字序列: 1 3 2 4 5 8 6 7

冒泡排序

第1趟排序: 1 2 3 4 5 6 7 8

第2趟排序: 1 2 3 4 5 6 7 8

關(guān)鍵字序列: 4 5 8 1 2 7 3 6

冒泡排序

第1趟排序: 4 5 1 2 7 3 6 8

第2趟排序: 4 1 2 5 3 6 7 8

第3趟排序: 1 2 4 3 5 6 7 8

第4趟排序: 1 2 3 4 5 6 7 8

第5趟排序: 1 2 3 4 5 6 7 8

關(guān)鍵字序列: 38 26 97 19 66 1 5 49

0..7, vot=38 5 26 1 19 38 66 97 49

0..3, vot=5 1 5 26 19 38 66 97 49

2..3, vot=26 1 5 19 26 38 66 97 49

5..7, vot=66 1 5 19 26 38 49 66 97

關(guān)鍵字序列: 38 5 49 26 19 97 1 66

0..7, vot=38 1 5 19 26 38 97 49 66

0..3, vot=1 1 5 19 26 38 97 49 66

1..3, vot=5 1 5 19 26 38 97 49 66

2..3, vot=19 1 5 19 26 38 97 49 66

5..7, vot=97 1 5 19 26 38 66 49 97

5..6, vot=66 1 5 19 26 38 49 66 97

關(guān)鍵字序列: 49 38 65 97 76 13 27 49

0..7, vot=49 49 38 27 13 49 76 97 65

0..3, vot=49 13 38 27 49 49 76 97 65

0..2, vot=13 13 38 27 49 49 76 97 65

1..2, vot=38 13 27 38 49 49 76 97 65

5..7, vot=76 13 27 38 49 49 65 76 97

關(guān)鍵字序列: 27 38 65 97 76 13 27 49 55 4

low=0 high=9 vot=27 4 27 13 27 76 97 65 49 55 38

low=0 high=2 vot=4 4 27 13 27 76 97 65 49 55 38

low=1 high=2 vot=27 4 13 27 27 76 97 65 49 55 38

low=4 high=9 vot=76 4 13 27 27 38 55 65 49 76 97

low=4 high=7 vot=38 4 13 27 27 38 55 65 49 76 97

low=5 high=7 vot=55 4 13 27 27 38 49 55 65 76 97

關(guān)鍵字序列: 38 26 97 19 66 1 5 49

直接選擇排序

第0趟排序: 1 26 97 19 66 38 5 49

第1趟排序: 1 5 97 19 66 38 26 49

第2趟排序: 1 5 19 97 66 38 26 49

第3趟排序: 1 5 19 26 66 38 97 49

第4趟排序: 1 5 19 26 38 66 97 49

第5趟排序: 1 5 19 26 38 49 97 66

第6趟排序: 1 5 19 26 38 49 66 97

最小堆

關(guān)鍵字序列: 81 49 76 27 97 38 49 13 65

sift 3..8 81 49 76 13 97 38 49 27 65

sift 2..8 81 49 38 13 97 76 49 27 65

sift 1..8 81 13 38 27 97 76 49 49 65

sift 0..8 13 27 38 49 97 76 49 81 65

13 27 38 49 97 76 49 81 65

sift 0..7 27 49 38 65 97 76 49 81 13

sift 0..6 38 49 49 65 97 76 81 27 13

sift 0..5 49 65 49 81 97 76 38 27 13

sift 0..4 49 65 76 81 97 49 38 27 13

sift 0..3 65 81 76 97 49 49 38 27 13

sift 0..2 76 81 97 65 49 49 38 27 13

sift 0..1 81 97 76 65 49 49 38 27 13

sift 0..0 97 81 76 65 49 49 38 27 13

最大堆

關(guān)鍵字序列: 49 65 13 81 76 27 97 38 49

sift 3..8 49 65 13 81 76 27 97 38 49

sift 2..8 49 65 97 81 76 27 13 38 49

sift 1..8 49 81 97 65 76 27 13 38 49

sift 0..8 97 81 49 65 76 27 13 38 49

97 81 49 65 76 27 13 38 49

sift 0..7 81 76 49 65 49 27 13 38 97

sift 0..6 76 65 49 38 49 27 13 81 97

sift 0..5 65 49 49 38 13 27 76 81 97

sift 0..4 49 38 49 27 13 65 76 81 97

sift 0..3 49 38 13 27 49 65 76 81 97

sift 0..2 38 27 13 49 49 65 76 81 97

sift 0..1 27 13 38 49 49 65 76 81 97

sift 0..0 13 27 38 49 49 65 76 81 97

關(guān)鍵字序列: 52 26 97 19 66 8 49

歸并排序

子序列長(zhǎng)度n=1 26 52 19 97 8 66 49

子序列長(zhǎng)度n=2 19 26 52 97 8 49 66

子序列長(zhǎng)度n=4 8 19 26 49 52 66 97

關(guān)鍵字序列: 13 27 38 49 97 76 49 81 65

最小堆序列? true

*/

分享名稱:用java代碼實(shí)現(xiàn)堆排序 用java代碼實(shí)現(xiàn)堆排序的方法
瀏覽地址:http://bm7419.com/article2/dohpsoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、電子商務(wù)、品牌網(wǎng)站建設(shè)網(wǎng)站設(shè)計(jì)、域名注冊(cè)、小程序開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名