kruti-working-branch #14

Closed
kruti wants to merge 15 commits from kruti/Princeton1:kruti-working-branch into main
Showing only changes of commit b8da0cb3b1 - Show all commits

View File

@ -11,23 +11,27 @@ public class Merge<E> extends AbstractCustomSorts<E> {
}
private void merge_sort(E[] arr, int low_index, int high_index){
System.out.println(low_index + " " + high_index);
if(high_index - low_index > 0) {
int mid_index = low_index + (high_index - low_index)/2;
merge_sort(arr, low_index, mid_index);
merge_sort(arr, mid_index + 1, high_index);
// Copy array
E[] auxArr = (E[]) new Object[arr.length];
System.arraycopy(arr, 0, auxArr, 0, arr.length);
E[] auxArr = (E[]) new Object[high_index - low_index + 1];
hitanshu marked this conversation as resolved Outdated

Making a new aux arry with each recursive call is expensive

Making a new aux arry with each recursive call is expensive
int index = 0;
for(int i=low_index; i<= high_index; i++)
auxArr[index++] = arr[i];
// Sort
int low_index_ptr = low_index;
int high_index_ptr = mid_index + 1;
int low_index_ptr = 0;
int mid_index_ptr = (auxArr.length - 1)/2;
int high_index_ptr = mid_index_ptr + 1;
if(!less((Comparable<E>) auxArr[mid_index_ptr], auxArr[high_index_ptr])) {
for (int i = low_index; i <= high_index; i++) {
if(low_index_ptr > mid_index)
arr[i] = auxArr[high_index_ptr++];
else if(high_index_ptr > high_index)
if (low_index_ptr > mid_index_ptr) {
arr[i] = auxArr[high_index_ptr];
high_index_ptr++;
} else if (high_index_ptr >= auxArr.length)
arr[i] = auxArr[low_index_ptr++];
else if (less((Comparable<E>) auxArr[low_index_ptr], auxArr[high_index_ptr]))
arr[i] = auxArr[low_index_ptr++];
@ -37,5 +41,6 @@ public class Merge<E> extends AbstractCustomSorts<E> {
}
}
}
}