/* 折半查找(二分查找)的递归和非递归算法 */ public class BinarySearch<T extends Comparable<T>> { private T[] data; public BinarySearch(T[] data) { this.data = data; } public int search(T key) { int low; int high; int mid; if(data == null) return -1; low = 0; high = data.length - 1; while(low <= high) { mid = (low + high) / 2; System.out.println("mid " + mid + "mid value:" + data[mid]); if(key.compareTo(data[mid]) < 0) { high = mid - 1; } else if(key.compareTo(data[mid]) > 0) { low = mid + 1; } else if(key.compareTo(data[mid]) == 0) { return mid; } } return -1; } private int doSearchRecursively(int low,int high,T key) { int mid; int result; if(low <= high) { mid = (low + high) / 2; result = key.compareTo(data[mid]); System.out.println("mid " + mid + " mid value:" + data[mid]); if(result < 0) { return doSearchRecursively(low,mid - 1,key); } else if(result > 0) { return doSearchRecursively(mid+1,high,key); }else if(result == 0) { return mid; } } return -1; } public int searchRecursively(T key) { if(data == null) return -1; return doSearchRecursively(0,data.length - 1,key); } public static void main(String[] args) { Integer []data = {1,4,5,8,15,33,48,77,96}; //BinarySearch<Integer> binSearch = new BinarySearch<Integer>(data); //System.out.println("Key index:" + binSearch.searchRecursively(3)); //System.out.println("Key index:" + binSearch.search(33)); //String []dataStr = {"A","C","F","J","L","N","T"}; //BinarySearch<String> binSearch = new BinarySearch<String>(dataStr); //System.out.println("Key index:"+binSearch.search("F")); } } public class Test1 { public static void main(String []args) { int t[] = {1,2,4,3}; range(t,0); } public static void range(int []a,int start) { if(start == a.length) { for(int i =0;i<a.length;i++) { System.out.print(a[i]); } System.out.println(); return; } for(int i = start;i<a.length;i++) { int temp = a[start]; a[start] = a[i]; a[i] = temp; range(a,start+1); a[i] = a[start]; a[start] = temp; } } } public class Test2 { public static void main(String args[]) { int t[] = {1,2,4,3}; range(t,0); } public static void range(int[] a,int start) { if(start == a.length) { for(int i=0;i<a.length;i++) { System.out.print(a[i]); } System.out.println(); return; } int dest[] = new int[a.length]; System.arraycopy(a,0,dest,0,a.length); for(int i = start;i<a.length;i++) { int temp = dest[start]; dest[start] = dest[i]; dest[i] = temp; range(dest,start + 1); } } } public class Test3 { public static void main(String args[]) { int t[] = {1,2,3,4}; range(t,0); } public static void range(int[] a,int start) { if(start == a.length) { for(int i=0;i<a.length;i++) { System.out.print(a[i]); } System.out.println(); return; } for(int i = start;i<a.length;i++) { int dest[] = new int[a.length]; System.arraycopy(a,0,dest,0,a.length); int temp = dest[start]; dest[start] = dest[i]; dest[i] = temp; range(dest,start + 1); } } } public class ArraySh { int []theArray; int nElems; int count = 0; ArraySh(int max) { theArray = new int[max]; nElems = max; } public static void main(String[] args) { int maxSize = 16; ArraySh arr; arr = new ArraySh(maxSize); for(int j=0;j<maxSize;j++) { int n = (int)(java.lang.Math.random() * 99); arr.insert(n); } arr.display(); arr.quickSort(); arr.display(); } public void insert(int i) { theArray[count++] = i; } public void display() { for(int i : theArray) { System.out.print(i+" "); } System.out.println(); } public void quickSort() { recQuickSort(0,nElems - 1); } public void recQuickSort(int left,int right) { if(right - left <= 0) return; else { long pivot = theArray[right]; int partition = partitionIt(left,right,pivot); recQuickSort(left,partition-1); recQuickSort(partition+1,right); } } public int partitionIt(int left,int right,long pivot) { int leftPtr = left - 1; int rightPtr = right; while(true) { while(theArray[++leftPtr] < pivot); while(rightPtr > 0 && theArray[--rightPtr] > pivot); if(leftPtr >= rightPtr)break; else swap(leftPtr,rightPtr); } swap(leftPtr,right); return leftPtr; } public void swap(int dex1,int dex2) { int temp = theArray[dex1]; theArray[dex1] = theArray[dex2]; theArray[dex2] = temp; } } //package sort; import java.util.Date; import java.util.Random; /* * 插入排序 */ public class InsertSort { public static void main(String args[]) { int len = 20; Date date = new Date(); Random random = new Random(date.getSeconds()); int data[]=new int[len]; for(int i = 0; i < len; i++) { data[i]=(int)(random.nextFloat()*100+1); } show(data); insertSort(data,data.length); show(data); System.out.println(binarySearch(data,1,data.length,55)); System.out.println(binarySearch2(data,55)); insertSortDesc(data); show(data); System.out.println(binarySearch(data,1,data.length,55)); System.out.println(binarySearch2(data,55)); } /* * 插入排序核心 */ private static void insertSortDesc(int[] data) { int length = data.length; for(int i = 1; i < length; i++) { int temp = data[i]; int j = i-1; while(j >= 0 && data[j] < temp) { data[j+1] = data[j]; j--; } data[j+1] = temp; } } private static void insertSortAsc(int[] data) { int length = data.length; for(int i = 1; i < length; i++) { int temp = data[i]; int j = i-1; while(j >= 0 && data[j] > temp) { data[j+1] = data[j]; j--; } data[j+1] = temp; } } private static void show(int[] data) { System.out.println("========================"); for(int i = 0; i < data.length; i++) { System.out.print(data[i] + " "); } System.out.println(); System.out.println("========================"); } /* * 使用递归实现的插入排序算法 */ private static void insertSort(int[] data,int n) { if(n>1) { insertSort(data,n-1); merge(data,n-1,n); } } private static void merge(int[] data,int end,int n) { int temp=data[n-1]; int i; for( i=end-1; i>=0; i--) { if(data[i]>temp) data[i+1]=data[i]; else break; } data[i+1]=temp; } /* * 递归版的二分查找算法,只能是按照升序排列的数组 */ private static int binarySearch(int[] data,int start,int end,int value) { if(end>=start) { int pos=(start+end)/2; if(value==data[pos-1]) return pos; else if(value>data[pos-1]) return binarySearch(data,pos+1,end,value); else return binarySearch(data,start,pos-1,value); } return -1; } /* * 迭代版二分查找算法,只能查找按照升序排列的数组 */ private static int binarySearch2(int[] data,int value) { int start = 0 ; int end = data.length-1; int mid; while(end>=start) { mid=(end+start)/2; if(value>data[mid]) start=mid+1; else if(value<data[mid]) end=mid-1; else return mid+1; } return -1; } }