你的程序無(wú)限循環(huán)的主要原因是for(int j=0;jh;j=j++)中的j=j++語(yǔ)句造成的,在java中j++是先賦值后自加,j永遠(yuǎn)也不會(huì)自增,

10年積累的網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶(hù)對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶(hù)得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先做網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有順德免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
其它也有不對(duì)的地方,我都幫你改正了,完整的程序如下(改動(dòng)的地方見(jiàn)注釋)
import java.util.Arrays;
public class test3 {
public static void main(String[] args) {
int[] arr = {2,9,41,5,7,2,6};
sort(arr);
System.out.println(Arrays.toString(arr));
}
public static int[] sort(int[] arr){
int h = arr.length;//這里把h=1改成h=arr.length
//這里去掉下面的while循環(huán)
//while(h arr.length/2){
//?h = h * 2 + 1;
//}
while(h 1){ //這里把h=1改成h1
h = (int)(h / 2);//這里對(duì)h取整數(shù)并且移到這里
for(int j = 0; j h; j++) { //這里把j = j++改成j++
for (int i = j + h; i arr.length; i = i + h) {
for(int z = i -h; z = 0; z--) { //這里把z=i -1改成z=i-h
if (arr[z] arr[i]) {
int temp;
temp = arr[z];
arr[z] = arr[i];
arr[i] = temp;
}
}
}
}
}
return arr;
}
}
// 排序
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ù)組是引用類(lèi)型,元素值將被改變
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
*/
本節(jié)內(nèi)容是排序算法系列之一: 希爾排序 ,主要講解了希爾排序的主體思路,選取了一個(gè)待排序的數(shù)字列表對(duì)希爾排序算法進(jìn)行了演示,給出了希爾排序算法的 Java 代碼實(shí)現(xiàn),幫助大家可以更好的理解希爾排序算法。
希爾排序(Shell Sort),是計(jì)算機(jī)科學(xué)與技術(shù)領(lǐng)域中較為簡(jiǎn)單的一種排序算法。
希爾排序是插入排序的一種,有時(shí)候也被稱(chēng)為 “縮小增量排序”。它是插入排序的改進(jìn)版,與插入排序的不同之處在于,希爾排序會(huì)優(yōu)先比較距離較遠(yuǎn)的元素。希爾排序是按照其設(shè)計(jì)者希爾(Donald Shell)的名字命名而來(lái),并于 1959 年公布出來(lái)。
在介紹完希爾排序之后,我們一起來(lái)看一下希爾排序的實(shí)現(xiàn)步驟具體是什么樣的吧。這里我們假設(shè)待排序的序列為 [9,2,11,7,12,5],我們按照從小到大的序列進(jìn)行排序。
選擇一個(gè)增量序列 k1,k2, … km,其中 k1k2…km=1,即增量序列大小依次減小,并且最后一個(gè)增量序列大小為 1。
按照增量序列的個(gè)數(shù) m,對(duì)整個(gè)待排序序列進(jìn)行 m 趟排序。
每一趟排序,根據(jù)對(duì)應(yīng)的增量 ki,需要將待排序的序列分成對(duì)應(yīng)長(zhǎng)度的子序列,分別在子序列上面進(jìn)行直接插入排序。當(dāng)且僅當(dāng)增量序列為 1 時(shí),整個(gè)序列作為一個(gè)整體處理。
其實(shí),上面的 步驟 1 和 步驟 2 都是在排序之前進(jìn)行的處理,選擇對(duì)應(yīng)的增量。上面的 步驟 3 每執(zhí)行一次,就相當(dāng)于是進(jìn)行了一次插入排序,只是每次都會(huì)選擇一個(gè)增量,將整個(gè)待排序序列按照增量進(jìn)行劃分,然后在對(duì)應(yīng)增量上面進(jìn)行插入排序。接下來(lái),讓我們用上面的待排序數(shù)字隊(duì)列 [9,2,11,7,12,5] 進(jìn)行整個(gè)算法步驟的排序演示工作。
按照 2.1 節(jié)的排序步驟,我們需要先選擇對(duì)應(yīng)的希爾排序中的增量值,按照一般性的原則,我們可以將增量按照待排序的序列長(zhǎng)度依次整除 2,直到增量為 1 停止,得到對(duì)應(yīng)的增量。如下:
接著,我們調(diào)用 2.1 中的 步驟 2, 步驟 3 ,按照增量值的取法,依次進(jìn)行對(duì)應(yīng)序列的插入排序,首先我們?nèi)≡隽恐禐?3,對(duì)應(yīng)排序示例如下:
在完成增量為 3 的插入排序之后,我們接著進(jìn)行增量為 1 的插入排序,這個(gè)步驟其實(shí)跟我們之前的插入排序步驟完全一致。整個(gè)過(guò)程如下:
從上面的示例可以看出,其實(shí)整個(gè)希爾排序的過(guò)程,就是根據(jù)增量大小依次進(jìn)行插入排序,本質(zhì)上還是針對(duì)插入排序的一種優(yōu)化。
在說(shuō)明希爾排序的整個(gè)過(guò)程之后,接下來(lái),我們看看如何用 Java 代碼實(shí)現(xiàn)希爾排序算法。
運(yùn)行結(jié)果如下:
代碼中的第 8 行初始化一個(gè)需要排序的數(shù)組,后面按照從小到大的排序規(guī)則,實(shí)現(xiàn)了數(shù)組的排序。第 12 行至 30 行是整個(gè)希爾排序的流程。第 14 行代碼表示希爾排序中的增量每次整除 2 取得,第 17 行至 25 行是一個(gè) for 循環(huán)結(jié)構(gòu),表明按照增量進(jìn)行插入排序。最后第 32 行代碼輸出排序好的數(shù)組。
本節(jié)主要學(xué)習(xí)了希爾排序算法,通過(guò)本節(jié)課程的學(xué)習(xí),需要熟悉希爾排序的算法流程,知道希爾排序算法的實(shí)現(xiàn)思路,可以自己用代碼實(shí)現(xiàn)希爾排序算法。至此,我們已經(jīng)學(xué)習(xí)了排序算法中的冒泡排序、插入排序、選擇排序、希爾排序。
public?class?ShellSort?{??
//交換數(shù)組元素??
private?static?void?swap(int[]?a,?int?i,?int?j)?{??
int?t?=?a[i];??
a[i]?=?a[j];??
a[j]?=?t;??
}??
public?static?void?sort(int[]?a)?{??
int?h?=?1;??
while?(h??a.length?/?3)?{//尋找合適的間隔h??
h?=?3?*?h?+?1;??
}??
while?(h?=?1)?{??
//將數(shù)組變?yōu)殚g隔h個(gè)元素有序??
for?(int?i?=?h;?i??a.length;?i++)?{??
//間隔h插入排序??
for?(int?j?=?i;?j?=?h??a[j]??a[j?-?h];?j?-=?h)?{??
swap(a,?j,?j?-?h);??
}??
}??
h?/=?3;??
}??
}??
}
給你介紹4種排序方法及源碼,供參考
1.冒泡排序
主要思路: 從前往后依次交換兩個(gè)相鄰的元素,大的交換到后面,這樣每次大的數(shù)據(jù)就到后面,每一次遍歷,最大的數(shù)據(jù)到達(dá)最后面,時(shí)間復(fù)雜度是O(n^2)。
public?static?void?bubbleSort(int[]?arr){
for(int?i?=0;?i??arr.length?-?1;?i++){
for(int?j=0;?j??arr.length-1;?j++){
if(arr[j]??arr[j+1]){
arr[j]?=?arr[j]^arr[j+1];
arr[j+1]?=?arr[j]^arr[j+1];
arr[j]?=?arr[j]^arr[j+1];
}
}
}
}
2.選擇排序
主要思路:每次遍歷序列,從中選取最小的元素放到最前面,n次選擇后,前面就都是最小元素的排列了,時(shí)間復(fù)雜度是O(n^2)。
public?static?void?selectSort(int[]?arr){
for(int?i?=?0;?i?arr.length?-1;?i++){
for(int?j?=?i+1;?j??arr.length;?j++){
if(arr[j]??arr[i]){
arr[j]?=?arr[j]^arr[i];
arr[i]?=?arr[j]^arr[i];
arr[j]?=?arr[j]^arr[i];
}
}
}
}
3.插入排序
主要思路:使用了兩層嵌套循環(huán),逐個(gè)處理待排序的記錄。每個(gè)記錄與前面已經(jīng)排好序的記錄序列進(jìn)行比較,并將其插入到合適的位置,時(shí)間復(fù)雜度是O(n^2)。
public?static?void?insertionSort(int[]?arr){
int?j;
for(int?p?=?1;?p??arr.length;?p++){
int?temp?=?arr[p];???//保存要插入的數(shù)據(jù)
//將無(wú)序中的數(shù)和前面有序的數(shù)據(jù)相比,將比它大的數(shù),向后移動(dòng)
for(j=p;?j0??temp?arr[j-1];?j--){
arr[j]?=?arr[j-1];
}
//正確的位置設(shè)置成保存的數(shù)據(jù)
arr[j]?=?temp;
}
}
4.希爾排序
主要思路:用步長(zhǎng)分組,每個(gè)分組進(jìn)行插入排序,再慢慢減小步長(zhǎng),當(dāng)步長(zhǎng)為1的時(shí)候完成一次插入排序,? 希爾排序的時(shí)間復(fù)雜度是:O(nlogn)~O(n2),平均時(shí)間復(fù)雜度大致是O(n^1.5)
public?static?void?shellSort(int[]?arr){
int?j?;
for(int?gap?=?arr.length/2;?gap??0?;?gap/=2){
for(int?i?=?gap;?i??arr.length;?i++){
int?temp?=?arr[i];
for(j?=?i;?j=gap??temparr[j-gap];?j-=gap){
arr[j]?=?arr[j-gap];
}
arr[j]?=?temp;
}
}
}
網(wǎng)站欄目:希爾排序基礎(chǔ)java代碼 希爾排序基礎(chǔ)java代碼
URL網(wǎng)址:http://chinadenli.net/article22/hpjejc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、ChatGPT、、網(wǎng)站導(dǎo)航、網(wǎng)頁(yè)設(shè)計(jì)公司、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)