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
}
}