Code improvements
All checks were successful
sample gradle build and test / build (pull_request) Successful in 1m4s
All checks were successful
sample gradle build and test / build (pull_request) Successful in 1m4s
This commit is contained in:
parent
cc3fc5df3d
commit
ca1391130d
@ -25,11 +25,12 @@ public class LinkedQueue<E> extends Queue<E>{
|
||||
|
||||
@Override
|
||||
public E dequeue() {
|
||||
if(isEmpty()) {
|
||||
if(isEmpty())
|
||||
return null;
|
||||
}
|
||||
E currentValue = start.data;
|
||||
start = start.next;
|
||||
if(isEmpty())
|
||||
end = null;
|
||||
return currentValue;
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,6 @@ public class LinkedStack<E> extends Stack<E>{
|
||||
}
|
||||
|
||||
Node<E> head = null;
|
||||
Node<E> endPtr = null;
|
||||
int stackLength = 0;
|
||||
|
||||
@Override
|
||||
@ -28,11 +27,8 @@ public class LinkedStack<E> extends Stack<E>{
|
||||
@Override
|
||||
public void push(E element) {
|
||||
Node<E> new_node = new Node<E>(element);
|
||||
if(stackLength == 0)
|
||||
head = new_node;
|
||||
else
|
||||
endPtr.next = new_node;
|
||||
endPtr = new_node;
|
||||
new_node.next = head;
|
||||
head = new_node;
|
||||
stackLength++;
|
||||
}
|
||||
|
||||
@ -40,15 +36,8 @@ public class LinkedStack<E> extends Stack<E>{
|
||||
public E pop() {
|
||||
if(stackLength == 0)
|
||||
return null;
|
||||
if(stackLength == 1)
|
||||
head = null;
|
||||
Node<E> secondLastPtr = head;
|
||||
while(secondLastPtr!=null && secondLastPtr.next != endPtr && secondLastPtr.next!=null)
|
||||
secondLastPtr = secondLastPtr.next;
|
||||
E currentValue = endPtr.data;
|
||||
endPtr.next = null;
|
||||
endPtr = secondLastPtr;
|
||||
stackLength--;
|
||||
E currentValue = head.data;
|
||||
head = head.next;
|
||||
return currentValue;
|
||||
}
|
||||
|
||||
|
@ -25,14 +25,23 @@ public class Shell<E> extends AbstractCustomSorts<E> {
|
||||
@Override
|
||||
public void sort(E[] arr) {
|
||||
MetaData metaData = new MetaData();
|
||||
for(int i=arr.length/10; i>=0; i--){
|
||||
hsort(arr, 3*i+1, metaData);
|
||||
|
||||
int h = 1;
|
||||
while(3*h + 1 < arr.length){
|
||||
h = 3*h + 1;
|
||||
}
|
||||
while(h>=1){
|
||||
hsort(arr, h, metaData);
|
||||
h= h/3;
|
||||
}
|
||||
// for(int i=arr.length/10; i>=0; i--){
|
||||
// hsort(arr, 3*i+1, metaData);
|
||||
// }
|
||||
System.out.println("Array sorted (shell sort) with " + metaData.compares + " compares and " + metaData.swaps + " swaps");
|
||||
}
|
||||
|
||||
private void hsort(E[] arr, int h, MetaData metadata) {
|
||||
for(int i=0; i<arr.length; i=i+h){
|
||||
for(int i=h; i<arr.length; i=i+1){
|
||||
int k = i;
|
||||
for(int j=i-h; j>=0; j=j-h){
|
||||
metadata.compares++;
|
||||
|
@ -5,8 +5,11 @@ import com.hithomelabs.princeton1.module5.AbstractCustomSorts;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Merge<E> extends AbstractCustomSorts<E> {
|
||||
private E[] auxArr;
|
||||
|
||||
@Override
|
||||
public void sort(E[] arr) {
|
||||
auxArr = (E[]) new Object[arr.length];
|
||||
merge_sort(arr, 0, arr.length-1);
|
||||
}
|
||||
|
||||
@ -17,21 +20,18 @@ public class Merge<E> extends AbstractCustomSorts<E> {
|
||||
merge_sort(arr, mid_index + 1, high_index);
|
||||
|
||||
// Copy array
|
||||
E[] auxArr = (E[]) new Object[high_index - low_index + 1];
|
||||
int index = 0;
|
||||
for(int i=low_index; i<= high_index; i++)
|
||||
auxArr[index++] = arr[i];
|
||||
auxArr[i] = arr[i];
|
||||
|
||||
// Sort
|
||||
int low_index_ptr = 0;
|
||||
int mid_index_ptr = (auxArr.length - 1)/2;
|
||||
int low_index_ptr = low_index;
|
||||
int mid_index_ptr = low_index + (high_index - low_index)/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_ptr) {
|
||||
arr[i] = auxArr[high_index_ptr];
|
||||
high_index_ptr++;
|
||||
} else if (high_index_ptr >= auxArr.length)
|
||||
arr[i] = auxArr[high_index_ptr++];
|
||||
} else if (high_index_ptr > high_index)
|
||||
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++];
|
||||
|
@ -34,23 +34,17 @@ public class Quick<E> extends AbstractCustomSorts<E> {
|
||||
while (i<=high && !less((Comparable<E>) arr[low], arr[i]))
|
||||
i++;
|
||||
// Find the j index less than 1st element
|
||||
while (!less((Comparable<E>) arr[j], arr[low]) && j > low)
|
||||
while (less((Comparable<E>) arr[low], arr[j]) && j > low)
|
||||
j--;
|
||||
// Break if indexes are crossed
|
||||
if(j <= i)
|
||||
if(j < i)
|
||||
break;
|
||||
// Swap index values of i & j
|
||||
if(less((Comparable<E>) arr[j], arr[i]))
|
||||
exch(arr, i++, j--);
|
||||
exch(arr, i, j);
|
||||
}
|
||||
// Swap 1st element to it's correct position
|
||||
int k_index = low+1;
|
||||
while (k_index <= high){
|
||||
if(less((Comparable<E>) arr[low], arr[k_index]))
|
||||
break;
|
||||
k_index++;
|
||||
}
|
||||
exch(arr, low, k_index-1);
|
||||
return k_index - 1;
|
||||
exch(arr, low, j);
|
||||
return j;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user