public class Mergesort { private void memeryArray(int[] a, int first, int mid, int last,int[] temp) { int i = first,j = mid + 1; int m = mid,n = last; int k = 0; while(i <= m && j <= n) { if(a[i] < a[j]) temp[k++] = a[i++]; else temp[k++] = a[j++]; } while(i <= m) temp[k++] = a[i++]; while(j <= n) temp[k++] = a[j++]; for(i = 0; i < k; i++) a[first+i] = temp[i]; } public void mergesort(int[] a, int first, int last, int[] temp) { if(first < last) { int mid = (first+last)/2; mergesort(a,first,mid,temp); mergesort(a,mid+1,last,temp); memeryArray(a,first,mid,last,temp); } } }
} public static void TestQuickSort(){ QuickSort quickSort = new QuickSort(); int[] a = {4,7,2,9,17,23,8,1,3}; quickSort.quickSort(a,0,a.length-1); for(int i = 0;i < a.length; i++){ System.out.print(a[i]+" "); } }
public static void TestBinarysearch(){ Binarysearch binarysearch = new Binarysearch(); int[] a ={1,2,3,12,14,16,45,67,89}; int result = binarysearch.search(a,16,0,a.length-1); System.out.println(result); }