java簡單排序代碼 java快速排序簡單代碼

如何用JAVA實現(xiàn)快速排序算法?

本人特地給你編的代碼\x0d\x0a親測\x0d\x0a\x0d\x0apublicclassQuickSort{\x0d\x0a\x0d\x0apublicstaticintPartition(inta[],intp,intr){\x0d\x0aintx=a[r-1];\x0d\x0ainti=p-1;\x0d\x0ainttemp;\x0d\x0afor(intj=p;jif(a[j-1]//swap(a[j-1],a[i-1]);\x0d\x0ai++;\x0d\x0atemp=a[j-1];\x0d\x0aa[j-1]=a[i-1];\x0d\x0aa[i-1]=temp;\x0d\x0a\x0d\x0a}\x0d\x0a}\x0d\x0a//swap(a[r-1,a[i+1-1]);\x0d\x0atemp=a[r-1];\x0d\x0aa[r-1]=a[i+1-1];\x0d\x0aa[i+1-1]=temp;\x0d\x0a\x0d\x0areturni+1;\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0apublicstaticvoidQuickSort(inta[],intp,intr){\x0d\x0a\x0d\x0aif(p

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:域名注冊、虛擬主機、營銷軟件、網(wǎng)站建設(shè)、渝中網(wǎng)站維護、網(wǎng)站推廣。

寫一個簡單的JAVA排序程序

// 排序

public class Array

{

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

{

if (n0)

{

int table[] = new int[n];

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

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

return table; //返回一個數(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("移動");

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

{

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

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

}

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

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++) //一趟中若干組,每個元素在自己所屬組內(nèi)進行直接插入排序

{

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

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

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

{

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++) //有交換時再進行下一趟,最多n-1趟

{

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

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

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

{

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]; //第一個值作為基準(zhǔn)值

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

{

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

j--;

if (ij)

{

table[i]=table[j]; //較小元素向前移動

i++;

}

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

i++;

if (ij)

{

table[j]=table[i]; //較大元素向后移動

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]開始的子序列中尋找最小元素

int min=i; //設(shè)第i個數(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為根的子樹調(diào)整成最小堆

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

int i=low; //子樹的根

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

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

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

{

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

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

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

{

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

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

j=2*i+1;

}

else

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

}

table[i]=temp; //當(dāng)前子樹的原根值調(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; //已排序的子序列長度,初值為1

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

do

{

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

print(Y);

n*=2; //子序列長度加倍

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("子序列長度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中兩個相鄰子序列歸并到Y(jié)中

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

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

else

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

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

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

while (jr+n jX.length) //將后一個子序列剩余元素復(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) //判斷一個數(shù)據(jù)序列是否為最小堆

{

if (table==null)

return false;

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

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;

}

}

/*

程序運行結(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)書

希爾排序

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)書不同

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

歸并排序

子序列長度n=1 26 52 19 97 8 66 49

子序列長度n=2 19 26 52 97 8 49 66

子序列長度n=4 8 19 26 49 52 66 97

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

最小堆序列? true

*/

java怎么讓數(shù)組的數(shù)字從大到小排序?

將數(shù)字從大到小排序的方法:

例如簡一點的冒泡排序,將第一個數(shù)字和后面的數(shù)字逐個比較大小,如果小于,則互換位置,大于則不動。此時,第一個數(shù)為數(shù)組中的最大數(shù)。然后再將第二個數(shù)與后面的數(shù)逐個比較,以次類推。

示例代碼如下:?

public?class?Test?{?

public?static?void?main(String[]?args)?{?

int?[]?array?=?{12,3,1254,235,435,236,25,34,23};?

int?temp;?

for?(int?i?=?0;?i??array.length;?i++)?{?

for?(int?j?=?i+1;?j??array.length;?j++)?{?

if?(array[i]??array[j])?{?

temp?=?array[i];?

array[i]?=?array[j];?

array[j]?=?temp; //?兩個數(shù)交換位置?

}?

}?

}?

for?(int?i?=?0;?i??array.length;?i++)?{?

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

}?

}?

}

數(shù)組對于每一門編程語言來說都是重要的數(shù)據(jù)結(jié)構(gòu)之一,當(dāng)然不同語言對數(shù)組的實現(xiàn)及處理也不盡相同。

Java 語言中提供的數(shù)組是用來存儲固定大小的同類型元素。

你可以聲明一個數(shù)組變量,如 numbers[100] 來代替直接聲明 100 個獨立變量 number0,number1,....,number99

擴展資料

Java中利用數(shù)組進行數(shù)字排序一般有4種方法:

1、選擇排序是先將數(shù)組中的第一個數(shù)作為最大或最小數(shù),然后通過循環(huán)比較交換最大數(shù)或最小數(shù)與一輪比較中第一個數(shù)位置進行排序。

2、冒泡排序也是先將數(shù)組中的第一個數(shù)作為最大或最小數(shù),循環(huán)比較相鄰兩個數(shù)的大小,滿足條件就互換位置,將最大數(shù)或最小數(shù)沉底。

3、快速排序法主要是運用Arrays類中的Arrays.sort方法()實現(xiàn)。

4、插入排序是選擇一個數(shù)組中的數(shù)據(jù),通過不斷的插入比較最后進行排序。

java快速排序簡單代碼

.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)存中進行排序,而外部排序是因排序的數(shù)據(jù)很大,一次不能容納全部的排序記錄,在排序過程中需要訪問外存。常見的內(nèi)部排序算法有:插入排序、希爾排序、選擇排序、冒泡排序、歸并排序、快速排序、堆排序、基數(shù)排序等。以下是快速排序算法:

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

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

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

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

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

1. 算法步驟

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

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

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

2. 動圖演示

代碼實現(xiàn) JavaScript 實例 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冒泡排序法代碼

冒泡排序是比較經(jīng)典的排序算法。代碼如下:

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

for(int j=1;jarr.length-i;j++){

//交換位置

} ? ?

拓展資料:

原理:比較兩個相鄰的元素,將值大的元素交換至右端。

思路:依次比較相鄰的兩個數(shù),將小數(shù)放在前面,大數(shù)放在后面。即在第一趟:首先比較第1個和第2個數(shù),將小數(shù)放前,大數(shù)放后。然后比較第2個數(shù)和第3個數(shù),將小數(shù)放前,大數(shù)放后,如此繼續(xù),直至比較最后兩個數(shù),將小數(shù)放前,大數(shù)放后。重復(fù)第一趟步驟,直至全部排序完成。

第一趟比較完成后,最后一個數(shù)一定是數(shù)組中最大的一個數(shù),所以第二趟比較的時候最后一個數(shù)不參與比較;

第二趟比較完成后,倒數(shù)第二個數(shù)也一定是數(shù)組中第二大的數(shù),所以第三趟比較的時候最后兩個數(shù)不參與比較;

依次類推,每一趟比較次數(shù)-1;

??

舉例說明:要排序數(shù)組:int[]?arr={6,3,8,2,9,1};?

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

for(int j=1;jarr.length-i;j++){

//交換位置

} ? ?

參考資料:冒泡排序原理

當(dāng)前名稱:java簡單排序代碼 java快速排序簡單代碼
標(biāo)題來源:http://muchs.cn/article0/docojio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、網(wǎng)站制作、品牌網(wǎng)站制作、網(wǎng)站排名標(biāo)簽優(yōu)化、商城網(wǎng)站

廣告

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

成都app開發(fā)公司