Bubble sort
public static void bubbleSort(int[] arr) { int temp = 0; for (int i = arr.length - 1; i > 0; Int j = 0; int j = 0; j < i; If (arr[j] > arr[j + 1]) {temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } }//loop j }//loop i }// method bubbleSort
Quick sort
“public static void quickSort(int[] arr){ qsort(arr, 0, arr.length-1); } private static void qsort(int[] arr, int low, int high){ if (low >= high) return; int pivot = partition(arr, low, high); // Divide the array into two parts qsort(ARr, low, pivot-1); // Recursively sort the left subarray qsort(arr, pivot+1, high); Private static int partition(int[] arr, int low, int high){int pivot = arr[low]; // benchmark while (low < high){while (low < high && arr[high] >= pivot) –high; arr[low]=arr[high]; While (low < high && arr[low] <= pivot) ++low; arr[high] = arr[low]; Arr [low] = pivot; arr[low] = pivot; // return low; } `
Hill sorting
` public static void shellSort(int[] arrays) {
Array.length /2 for (int step = array.length /2; step > 0; For (int I = step; int I = step; i < arrays.length; i++) { int j = i; int temp = arrays[j]; While (j-step >= 0 && Arrays [j-step] > temp) {arrays[j] = arrays[j-step]; j = j - step; } arrays[j] = temp; `}}}Copy the code
To find the
public int binarySearch(int[] A, int target, int n){ int low = 0, high = n, mid; while(low <= high){ mid = low + (high - low) / 2; if(A[mid] == target){ return mid; }else if(A[mid] > target){ high = mid - 1; }else{ low = mid + 1; } } return -1; }