kruti-working-branch #14
@ -25,11 +25,12 @@ public class LinkedQueue<E> extends Queue<E>{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public E dequeue() {
|
public E dequeue() {
|
||||||
if(isEmpty()) {
|
if(isEmpty())
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
E currentValue = start.data;
|
E currentValue = start.data;
|
||||||
start = start.next;
|
start = start.next;
|
||||||
|
if(isEmpty())
|
||||||
|
end = null;
|
||||||
hitanshu marked this conversation as resolved
Outdated
|
|||||||
return currentValue;
|
return currentValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,6 @@ public class LinkedStack<E> extends Stack<E>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
Node<E> head = null;
|
Node<E> head = null;
|
||||||
hitanshu marked this conversation as resolved
hitanshu
commented
Why do you need two pointers, both insertion and deletion happen at head Why do you need two pointers, both insertion and deletion happen at head
|
|||||||
Node<E> endPtr = null;
|
|
||||||
int stackLength = 0;
|
int stackLength = 0;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -28,11 +27,8 @@ public class LinkedStack<E> extends Stack<E>{
|
|||||||
@Override
|
@Override
|
||||||
public void push(E element) {
|
public void push(E element) {
|
||||||
Node<E> new_node = new Node<E>(element);
|
Node<E> new_node = new Node<E>(element);
|
||||||
if(stackLength == 0)
|
new_node.next = head;
|
||||||
head = new_node;
|
head = new_node;
|
||||||
else
|
|
||||||
endPtr.next = new_node;
|
|
||||||
endPtr = new_node;
|
|
||||||
stackLength++;
|
stackLength++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,15 +36,8 @@ public class LinkedStack<E> extends Stack<E>{
|
|||||||
public E pop() {
|
public E pop() {
|
||||||
if(stackLength == 0)
|
if(stackLength == 0)
|
||||||
return null;
|
return null;
|
||||||
if(stackLength == 1)
|
E currentValue = head.data;
|
||||||
head = null;
|
head = head.next;
|
||||||
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--;
|
|
||||||
return currentValue;
|
return currentValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,14 +25,23 @@ public class Shell<E> extends AbstractCustomSorts<E> {
|
|||||||
@Override
|
@Override
|
||||||
public void sort(E[] arr) {
|
public void sort(E[] arr) {
|
||||||
MetaData metaData = new MetaData();
|
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");
|
System.out.println("Array sorted (shell sort) with " + metaData.compares + " compares and " + metaData.swaps + " swaps");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void hsort(E[] arr, int h, MetaData metadata) {
|
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;
|
int k = i;
|
||||||
for(int j=i-h; j>=0; j=j-h){
|
for(int j=i-h; j>=0; j=j-h){
|
||||||
metadata.compares++;
|
metadata.compares++;
|
||||||
|
@ -5,8 +5,11 @@ import com.hithomelabs.princeton1.module5.AbstractCustomSorts;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class Merge<E> extends AbstractCustomSorts<E> {
|
public class Merge<E> extends AbstractCustomSorts<E> {
|
||||||
|
private E[] auxArr;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sort(E[] arr) {
|
public void sort(E[] arr) {
|
||||||
|
auxArr = (E[]) new Object[arr.length];
|
||||||
merge_sort(arr, 0, arr.length-1);
|
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);
|
merge_sort(arr, mid_index + 1, high_index);
|
||||||
hitanshu marked this conversation as resolved
Outdated
hitanshu
commented
Making a new aux arry with each recursive call is expensive Making a new aux arry with each recursive call is expensive
|
|||||||
|
|
||||||
// Copy array
|
// Copy array
|
||||||
E[] auxArr = (E[]) new Object[high_index - low_index + 1];
|
|
||||||
int index = 0;
|
|
||||||
for(int i=low_index; i<= high_index; i++)
|
for(int i=low_index; i<= high_index; i++)
|
||||||
auxArr[index++] = arr[i];
|
auxArr[i] = arr[i];
|
||||||
|
|
||||||
// Sort
|
// Sort
|
||||||
int low_index_ptr = 0;
|
int low_index_ptr = low_index;
|
||||||
int mid_index_ptr = (auxArr.length - 1)/2;
|
int mid_index_ptr = low_index + (high_index - low_index)/2;
|
||||||
int high_index_ptr = mid_index_ptr + 1;
|
int high_index_ptr = mid_index_ptr + 1;
|
||||||
if(!less((Comparable<E>) auxArr[mid_index_ptr], auxArr[high_index_ptr])) {
|
if(!less((Comparable<E>) auxArr[mid_index_ptr], auxArr[high_index_ptr])) {
|
||||||
for (int i = low_index; i <= high_index; i++) {
|
for (int i = low_index; i <= high_index; i++) {
|
||||||
if (low_index_ptr > mid_index_ptr) {
|
if (low_index_ptr > mid_index_ptr) {
|
||||||
arr[i] = auxArr[high_index_ptr];
|
arr[i] = auxArr[high_index_ptr++];
|
||||||
high_index_ptr++;
|
} else if (high_index_ptr > high_index)
|
||||||
} else if (high_index_ptr >= auxArr.length)
|
|
||||||
arr[i] = auxArr[low_index_ptr++];
|
arr[i] = auxArr[low_index_ptr++];
|
||||||
else if (less((Comparable<E>) auxArr[low_index_ptr], auxArr[high_index_ptr]))
|
else if (less((Comparable<E>) auxArr[low_index_ptr], auxArr[high_index_ptr]))
|
||||||
arr[i] = auxArr[low_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]))
|
while (i<=high && !less((Comparable<E>) arr[low], arr[i]))
|
||||||
i++;
|
i++;
|
||||||
// Find the j index less than 1st element
|
// 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--;
|
j--;
|
||||||
// Break if indexes are crossed
|
// Break if indexes are crossed
|
||||||
if(j <= i)
|
if(j < i)
|
||||||
break;
|
break;
|
||||||
// Swap index values of i & j
|
// Swap index values of i & j
|
||||||
if(less((Comparable<E>) arr[j], arr[i]))
|
if(less((Comparable<E>) arr[j], arr[i]))
|
||||||
exch(arr, i++, j--);
|
exch(arr, i, j);
|
||||||
}
|
}
|
||||||
// Swap 1st element to it's correct position
|
// Swap 1st element to it's correct position
|
||||||
int k_index = low+1;
|
exch(arr, low, j);
|
||||||
while (k_index <= high){
|
return j;
|
||||||
if(less((Comparable<E>) arr[low], arr[k_index]))
|
|
||||||
break;
|
|
||||||
k_index++;
|
|
||||||
}
|
|
||||||
exch(arr, low, k_index-1);
|
|
||||||
return k_index - 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user
Case where only 1 element in queue, end pointer is dangling.