2. 選擇排序
3.插入排序
4.歸併排序
5.快速排序
public class demoarr1 ;
int temp;
for(int i = 0; i < arr.length -1; i++) }}
//迴圈遍歷輸出
for(int a:arr)
}}
public class demoarr11 ;
scanner superman = new scanner(system.in);
//氣泡排序
int temp;
for(int i = 0; i < arr.length-1; i++)
}} //二分查詢
system.out.println("--輸入乙個數字:");
int num = superman.nextint();
int start = 0;
int end = arr.length-1;
int middle = (start + end)/2;
while(arr[middle] != num) else if(num < arr[middle])
if(start > end)
middle = (start + end)/2;
} system.out.println("位置:" + middle);
if(middle == -1) else
}}
public class demoarr2 ;
int temp;
for(int i = 0; i < arr.length -1; i ++)
}} for(int a : arr)
}}
public class demoarr3 ;
int temp;
for(int i = 1; i < arr.length; i++)
//覆蓋插入
arr[j] = temp;}}
} for(int a :arr)
}}
public class mergesort01 ;
int arr2=;
int mergesort = mergesort(arr1, arr2);
system.out.println(arrays.tostring(mergesort));
} public static int mergesort(int arr1,int arr2)
} //說明有乙個陣列已經沒有元素了
//另外乙個陣列只需要全部過去
while(m<=arr1.length-1)
while(n<=arr2.length-1)
return newarr;
}}
public class quicksort ;
system.out.println(arrays.tostring(arr1));
quicksortleft(arr1, 0, arr1.length-1);
system.out.println(arrays.tostring(arr1));
} /*
* 引數列表:
* int 陣列 大陣列
* int left 小集合的左側邊界
* int right 小集合的右側邊界
* */
public static void quicksortleft(int arr,int left,int right)else
} //獲取每一次的分界線的最終位置 僅考慮乙個小集合
public static int getindex(int arr, int left, int right)
//出了迴圈 arr[right]arr[left]=arr[right];
//從左向右比較
while(arr[left]<=key && left//arr[left]》key
arr[right]=arr[left];
} //出了迴圈 最終的界限定了 left=right
//給分界點 賦值
arr[left]=key;
return left;
}}
java各種排序方法
package org.rut.util.algorithm.support import org.rut.util.algorithm.sortutil author treeroot since 2006 2 2 version 1.0 public class insertsort imple...
java各種排序方法
package org.rut.util.algorithm.support import org.rut.util.algorithm.sortutil author treeroot since 2006 2 2 version 1.0 public class insertsort imple...
各種排序演算法彙總
各種排序演算法彙總 include include include include using namespace std template void insertsort t a,int n 直接插入排序,時間複雜度為o n 2 a j d temp 這個while實際上是直接插入排序 d 1 即...