package com.flyingh.demo; public class Demo { public static void main(String[] args) { int[] arr = { 8, 1, 7, 2, 5, 9, 0, 3, 6, 4 }; print(arr); // bubbleSort(arr); // selectSort(arr); // swapSort(arr); // insertSort(arr); // insertSort(arr,arr.length-1); // shellSort(arr); // mergeSort(arr,0,arr.length-1); // quickSort(arr,0,arr.length-1); // heapSort(arr,arr.length); // countingSort(arr); // radixSort(arr); // bucketSort(arr); print(arr); // System.out.println(binarySearch(arr,0,arr.length-1,5)); System.out.println(select(arr, 1, 8, 3)); } public static int select(int[] arr, int i, int j, int k) { if (k < 1 || k > j - i + 1) { return -1; } // int t = partition(arr, i, j); int t = partition2(arr, i, j); if (t == k + i - 1) { return arr[t]; } else if (t < k + i - 1) { return select(arr, t + 1, j, k + i - 1 - t); } else { return select(arr, i, t - 1, k); } } public static int binarySearch(int[] arr, int m, int n, int x) { if (arr[m] > x || arr[n] < x) { return -1; } int mid = (m + n) / 2; if (arr[mid] > x) { return binarySearch(arr, m, mid, x); } else if (arr[mid] < x) { return binarySearch(arr, mid + 1, n, x); } else { return mid; } } public static void bucketSort(int[] arr) { int max = getMax(arr); int[] c = new int[max + 1]; for (int i = 0; i < arr.length; i++) { ++c[arr[i]]; } for (int i = 1; i < c.length; i++) { c[i] += c[i - 1]; } int[] tmp = new int[arr.length]; for (int i = 0; i < tmp.length; i++) { tmp[--c[arr[i]]] = arr[i]; } System.arraycopy(tmp, 0, arr, 0, tmp.length); } public static void radixSort(int[] arr) { int max = getMax(arr); int len = String.valueOf(max).length(); for (int i = 0; i < len;) { sort(arr, i++); } } private static final int RADIX = 10; private static void sort(int[] arr, int m) { int[] c = new int[RADIX]; for (int i = 0; i < arr.length; i++) { ++c[digital(arr[i], m)]; } for (int i = 1; i < c.length; i++) { c[i] += c[i - 1]; } int[] tmp = new int[arr.length]; for (int i = tmp.length - 1; i >= 0; --i) { tmp[--c[digital(arr[i], m)]] = arr[i]; } System.arraycopy(tmp, 0, arr, 0, tmp.length); } private static int digital(int x, int m) { return (int) (x / Math.pow(RADIX, m) % RADIX); } public static void countingSort(int[] arr) { int max = getMax(arr); int[] c = new int[max + 1]; for (int i = 0; i < arr.length; i++) { ++c[arr[i]]; } for (int i = 1; i < c.length; i++) { c[i] += c[i - 1]; } int[] tmp = new int[arr.length]; for (int i = tmp.length - 1; i >= 0; --i) { tmp[--c[arr[i]]] = arr[i]; } System.arraycopy(tmp, 0, arr, 0, tmp.length); } private static int getMax(int[] arr) { int max = arr[0]; for (int i = 1; i < arr.length; ++i) { if (max < arr[i]) { max = arr[i]; } } // for (int i : arr) { // if(max<i){ // max=i; // } // } return max; } public static void heapSort(int[] arr, int len) { buildHeap(arr, len); if (arr[0] > arr[len - 1]) { swap(arr, 0, len - 1); heapSort(arr, len - 1); } } private static void buildHeap(int[] arr, int len) { for (int i = len / 2 - 1; i >= 0; --i) { heapify(arr, i, len); } } private static void heapify(int[] arr, int i, int len) { int l = 2 * i + 1; int r = 2 * i + 2; if (r < len) { int large = i; if (arr[large] < arr[l]) { large = l; } if (arr[large] < arr[r]) { large = r; } if (large != i) { swap(arr, i, large); heapify(arr, large, len); } } } public static void quickSort(int[] arr, int m, int n) { if (m < n) { // int t=partition(arr,m,n); int t = partition2(arr, m, n); quickSort(arr, m, t - 1); quickSort(arr, t + 1, n); } } private static int partition2(int[] arr, int m, int n) { int key = arr[n]; int i = m - 1; for (int j = m; j < n; j++) { if (arr[j] < key) { swap(arr, ++i, j); } } swap(arr, ++i, n); return i; } @SuppressWarnings("unused") private static int partition(int[] arr, int m, int n) { int key = arr[m]; while (m < n) { while (m < n && arr[n] >= key) { --n; } arr[m] = arr[n]; while (m < n && arr[m] <= key) { ++m; } arr[n] = arr[m]; } arr[m] = key; return m; } public static void mergeSort(int[] arr, int m, int n) { if (m < n) { int mid = (m + n) / 2; mergeSort(arr, m, mid); mergeSort(arr, mid + 1, n); merge(arr, m, mid, n); } } private static void merge(int[] arr, int m, int mid, int n) { int[] l = new int[mid - m + 1]; int[] r = new int[n - mid]; for (int i = 0; i < l.length; i++) { l[i] = arr[m + i]; } for (int i = 0; i < r.length; i++) { r[i] = arr[mid + 1 + i]; } int i = 0; int j = 0; while (i < l.length && j < r.length) { if (l[i] < r[j]) { arr[m++] = l[i++]; } else { arr[m++] = r[j++]; } } while (i < l.length) { arr[m++] = l[i++]; } while (j < r.length) { arr[m++] = r[j++]; } } public static void shellSort(int[] arr) { for (int gap = arr.length / 2; gap > 0; gap /= 2) { for (int i = gap; i < arr.length; i++) { int key = arr[i]; int j = i - gap; for (; j >= 0 && arr[j] > key; j -= gap) { arr[j + gap] = arr[j]; } arr[j + gap] = key; } } } public static void insertSort(int[] arr, int m) { if (m > 0) { insertSort(arr, m - 1); int key = arr[m]; int i = m - 1; for (; i >= 0 && arr[i] > key; --i) { arr[i + 1] = arr[i]; } arr[i + 1] = key; } } public static void insertSort(int[] arr) { for (int i = 1; i < arr.length; i++) { int key = arr[i]; int j = i - 1; for (; j >= 0 && arr[j] > key; --j) { arr[j + 1] = arr[j]; } arr[j + 1] = key; } } public static void swapSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { for (int j = i + 1; j < arr.length; j++) { if (arr[i] > arr[j]) { swap(arr, i, j); } } } } public static void selectSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { int k = i; for (int j = i + 1; j < arr.length; j++) { if (arr[k] > arr[j]) { k = j; } } if (k != i) { swap(arr, k, i); } } } public static void bubbleSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { swap(arr, j, j + 1); } } } } private static void swap(int[] arr, int j, int j2) { int tmp = arr[j]; arr[j] = arr[j2]; arr[j2] = tmp; } private static void print(int[] arr) { for (int i : arr) { System.out.print(i + " "); } System.out.println(); } }