Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Dystopia {
- // this is not merge sort... this will merge two arrays that are already sorted in ascending order
- public static int[] mergeSorted(final int[] arr1, final int[] arr2){
- final int a1len = arr1.length, a2len = arr2.length;
- int[] ret = new int[a1len + a2len];
- int a1cnt = 0, a2cnt = 0, retcnt = 0;
- while(a1cnt < arr1.length && a2cnt < arr2.length){
- ret[retcnt++] = arr1[a1cnt] < arr2[a2cnt] ? arr1[a1cnt++] : arr2[a2cnt++];
- }
- while(a1cnt < a1len) {
- ret[retcnt++] = arr1[a1cnt++];
- }
- while(a2cnt < a2len){
- ret[retcnt++] = arr2[a2cnt++];
- }
- return ret;
- }
- public static void quicksort(int arr[]){
- quicksort(arr, 0, arr.length - 1);
- }
- public static void quicksort(int arr[], final int low, final int high){
- if(arr == null || arr.length == 0 || low >= high){
- return;
- }
- int l = low, h = high;
- final int pivot = arr[low + (high - low) / 2]; // get the middle term of the segment of arr[] requested
- while(l <= h){
- while(arr[l] < pivot){ ++l; }
- while(arr[h] > pivot){ --h; }
- if(l <= h){
- int tmp = arr[l];
- arr[l++] = arr[h];
- arr[h--] = tmp;
- }
- }
- if(low < h) {
- quicksort(arr, low, h);
- }
- if(high > l){
- quicksort(arr, l, high);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement