package algorithm.sort; import java.util.Random; public class Sort { private static Random rand = new Random(); public void BubbleSort(int[] a, int length) { for (int i = 0; i < length; i++) for (int j = i + 1; j < length; j++) { if (a[i] >= a[j]) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } } } public void InsertSort(int [] a,int length){ int temp,j; for(int i=0;i<length;i++){ temp = a[i]; j = i-1; while(j>=0&&temp<a[j]){ a[j+1] = a[j]; j--; } a[j+1] = temp; } } public void BinSort(int[] t, int length) { int key, i, j, low, high, mid; for (i = 1; i < length; i++) { low = 0; high = i - 1; key = t[i]; while (low <= high) { mid = (low + high) / 2; if (key < t[mid]) { high = mid - 1; } else { low = mid + 1; } } for (j = i; j > high + 1; j--) { t[j] = t[j - 1]; } t[high + 1] = key; } } private void MAX_HEAPIFY(int[] a, int i, int heapSize) { int left = i * 2; int right = i * 2 + 1; int largest; if (left <= heapSize && a[left] > a[i]) { largest = left; } else { largest = i; } if (right <= heapSize && a[right] > a[largest]) { largest = right; } if (largest != i) { int temp = a[i]; a[i] = a[largest]; a[largest] = temp; MAX_HEAPIFY(a, largest, heapSize); } } private void BUILD_MAX_HEAP(int[] a, int heapSize) { for (int i = a.length / 2; i >= 0; i--) { MAX_HEAPIFY(a, i, heapSize); } } public void HeapSort(int[] a) { BUILD_MAX_HEAP(a, a.length - 1); int heapSize = a.length - 1; for (int i = a.length - 1; i >= 1; i--) { int temp = a[0]; a[0] = a[i]; a[i] = temp; heapSize = heapSize - 1; MAX_HEAPIFY(a, 0, heapSize); } } public void QuickSort(int[] a, int p, int r) { if (p < r) { int q = partition(a, p, r); QuickSort(a, p, q - 1); QuickSort(a, q + 1, r); } } private int partition(int[] a, int p, int r) { int i = p + rand.nextInt(r - p + 1); swap(a,p,i); int pivot = a[p]; int low = p; int high = r; while(low<high){ while(low<high&&a[high]>pivot){ high--; } if(low<high){ a[low]=a[high]; low++; } while(low<high&&a[low]<pivot){ low++; } if(low<high){ a[high]=a[low]; high--; } } a[low] = pivot; return low; } private static void swap(int x[],int a,int b){ int temp = x[a]; x[a] = x[b]; x[b] = temp; } public void mergeSort(int[] a,int[] temp,int low,int high){ if(low < high){ int mid = (low+high)/2; mergeSort(a,temp,low,mid); mergeSort(a,temp,mid+1,high); merge(a,temp,low,mid,high); } } private void merge(int[] a,int[] temp,int low,int mid,int high){ for(int i=low;i<=high;i++){ temp[i] = a[i]; } int i=low,j=mid+1,k=low; while((i<=low)&&(j<=high)){ if(temp[i]<=temp[j]){ a[k++] = temp[i++]; }else{ a[k++] = temp[j++]; } } while(i<=mid){ a[k++] = temp[i++]; } while(j<=high){ a[k++] = temp[j++]; } } public static void main(String[] args) { Sort sort = new Sort(); int a[] = { 7, 4, 3, 8, 9, 65, 23, 99, 15, 2, 0, 47 }; int temp[] = new int[a.length]; // sort.BubbleSort(a, a.length); sort.InsertSort(a, a.length); //sort.BinSort(a, a.length); //sort.QuickSort(a,0,a.length-1); // sort.HeapSort(a); //sort.mergeSort(a, temp,0, a.length-1); for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } }