package 排序;
/**
* 歸併排序:使用遞歸算法,分治算法,把大的問題劃分爲小的部分,然後遞歸求解
* @author sunfeilong1993
*/
public class useMSort {
public static void main(String[] args) {
Integer [] b = {1,2,4,5,6,9,7,8,14,3,1000,100,11,15};
Integer[] c = b.clone();
msort(b,c,0,b.length);
for(Integer i: b){
System.out.print(i+" " );
}
}
//參數類型 數組 數組 int int
public static <T extends Comparable<? super T>> void msort(T arr[],T tempArr[],int first,int last){
//如果沒有分解到每一部分只有一個元素繼續分解
if(first + 1 < last){
int midpt = (first+last)/2;
msort(arr,tempArr,first,midpt);
msort(arr,tempArr,midpt,last);
//如果兩個數組的順序直接拍好則直接返回
if(arr[midpt-1].compareTo(arr[midpt]) <= 0){
return;
}
//否則則把數據拷貝到TempArr
int indexA = first;
int indexB = midpt;
int indexC = first;
//當兩個數組都沒有遍歷完的時候,歸併算法
while(indexA < midpt && indexB < last){
if(arr[indexA].compareTo(arr[indexB]) < 0){
tempArr[indexC] = arr[indexA];
indexA ++;
indexC ++;
}
else{
tempArr[indexC] = arr[indexB];
indexB ++;
indexC ++;
}
}//end while
//當B數組歸併完的時候
while(indexA < midpt){
tempArr[indexC] = arr[indexA];
indexA ++;
indexC ++;
}
//當A數組歸併萬的時候
while(indexB < last){
tempArr[indexC] = arr[indexB];
indexB ++;
indexC ++;
}
//複製到原來的數組
for(int i = first ; i < last ; i++){
arr[i] = tempArr[i];
}
}//end if
}//end mosrt()
}//end main
</pre><pre name="code" class="java">
/**
* 插入排序算法策略:排序值列中的前2個值,並在必要時交換它們。
* 在相對於前2個值(有序的)的適當位置插入值列的第三個值。
* 然後,在相對於前3個值(有序的)的適當位置插入值列的第4個值。
* 每進行一次插入操作,有序子集中的數值個數將遞增1。重複該過程,
* 直至值列中的所有值都按照次序排列爲止。插入過程需要移動數組中的
* 其他值,爲插入的元素騰出存儲空間。
*/
public class useInsertSort {
public static void main(String[] args) {
Integer [] b = {1,2,3,4,5,6,9,7,8,14};
insertSort(b);
for(Integer i: b){
System.out.print(i+" " );
}
}
public static <T extends Comparable<? super T>> void insertSort(T[] arr){
T swap = null;
int j;
for(int i = 1 ; i < arr.length ; i++){
j = i;
swap = arr[j];
while(j>0 && swap.compareTo(arr[j-1]) > 0 ){
arr[j] = arr[j-1];
j--;
}
arr[j] = swap;
}
}
}
/**
*冒泡排序:從第一個元素開始依次遍歷如果後面的元素比當前的小,
*則把當前的和後面的交換,最後確保第一次遍歷結束時最後一個
*元素是最大的,第二次遍歷確定倒數第二個元素是最大的,以此類推。
*/
public class bubble {
public static void main(String[] args) {
int [] a = {1,3,6,5,2,4};
bubbleSort(a);
for(int i : a){
System.out.print(i+" ");
}
}
public static void bubbleSort(int [] arr){
int temp;
for(int i =0 ; i < arr.length ; i++ ){
for(int j =0; j <arr.length - i-1 ; j++){
if(arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}//end if
}//end for j
}//end for i
}//end bubbleSort
}
/**
* 選擇排序:每一次選出剩餘的元素中的最小的元素對應的編號,然後和剩餘元素的
* 第一個元素交換,最後生成有序的序列
*/
public class useSelectSort {
public static void main(String[] args) {
int [] a = {1,3,6,5,2,4};
selectSort(a);
for(int i : a){
System.out.print(i+" ");
}
}//end main
//選擇排序
public static void selectSort(int arr[]){
int smallIndex;
int temp;
for(int i =0 ; i < arr.length ; i++){
smallIndex = i;
for(int j = i +1 ; j< arr.length ; j++){
if(arr[j] < arr[smallIndex]){
smallIndex = j;
}
}//end for j
temp = arr[i];
arr[i] = arr[smallIndex];
arr[smallIndex] = temp;
}//end for i
}
}
/**
* 二叉搜索算法:在有序數組的基礎上進行查找
* 把數組從中間分爲兩部分,然後判斷所查找的目標在哪一部分,最後隨着
* 範圍的縮小就可能找到目標,也可能沒有目標。
*/
public class useBinSearch {
public static void main(String[] args) {
Integer [] a = {1,2,3,4,5,6};
int b = binSearch(a, 0,a.length ,3);
System.out.println(b);
}//end main
//使用泛型
public static <T extends Comparable<? super T> > int binSearch(T[] arr,int first,int last,T target){
int mid;
T midValue;
while(first < last){
mid = (first + last) / 2;
midValue = arr[mid];
if(midValue.compareTo(target) == 0){
return mid;
}
else if(midValue.compareTo(target) > 0){
last = mid;
}
else{
first = mid +1;
}
}// end while
return -1;
}//end binSelect()
}//end main
/**
* 漢諾塔問題:使用遞歸求解
*
*/
public class useHanoi {
public static void main(String[] args) {
Hanoi(3, "1", "2", "3");
}
private static void Hanoi(int number ,String A,String B,String C){
if(number == 1){
System.out.println("Move\t"+A+"\t to\t "+C );
}
else{
Hanoi(number-1, A, C, B);
System.out.println("Move\t"+A+"\tto\t"+C );
Hanoi(number-1, B, A, C );
}
}//ent method Hanoi()
}
/**
* 快速排序:選取一個基準,然後根據基準把大於基準的數放在左邊,
* 把小於基準的數放在右邊,然後把原有的數組分爲兩半,在進行排序
* 以此類推,最後得到一個有序的序列。
*/
public class quickSort {
public static void main(String[] args) {
Integer [] a = {3,6,4,2,5,1};
quickSort(a);
for(int i : a){
System.out.print(i+" ");
}
}
public static <T extends Comparable<? super T>> void quickSort( T [] arr){
qsort(arr,0,arr.length);
}
public static <T extends Comparable<? super T>> void qsort(T[] arr,int first,int last){
int pivotloc;
T temp;
//只有一個元素直接返回
if(last - first <= 1){
return;
}//end if
//只有兩個元素則互換位置
else if(last - first == 2){
if(arr[last-1].compareTo(arr[first]) < 0){
temp = arr[first];
arr[first] = arr[last-1];
arr[last-1] = temp;
}
}//end else-if
//其他情況
else{
pivotloc = pivotIndex(arr,first,last);
qsort(arr, first, pivotloc);
qsort(arr, pivotloc,last);
}//end else
}
public static <T extends Comparable<? super T>> int pivotIndex(T[] arr,int first,int last){
int mid,scanUp,scanDown;
T pivot ,temp;
if(first == last){
return last;
}
else if(first == last -1){
return first;
}
else{
mid = (first+last)/2;
pivot = arr[mid];
arr[mid] = arr[first];
arr[first] = pivot;
scanDown = last-1;
scanUp = first+1;
for(;;){
while(scanUp <= scanDown && arr[scanUp].compareTo(pivot) < 0){
scanUp++;
}
while(pivot.compareTo(arr[scanDown]) < 0){
scanDown --;
}
if(scanUp >= scanDown){
break;
}
temp = arr[scanUp];
arr[scanUp] = arr[scanDown];
arr[scanDown] = temp;
scanUp++;
scanDown--;
}//end for
arr[first] = arr[scanDown];
arr[scanDown] = pivot;
return scanDown;
}//end else
}
}