kruti-working-branch #14

Closed
kruti wants to merge 15 commits from kruti/Princeton1:kruti-working-branch into main
8 changed files with 31 additions and 302 deletions
Showing only changes of commit 34dac53f86 - Show all commits

View File

@ -5,100 +5,28 @@ import java.util.Iterator;
import javax.annotation.Nonnull;
public class ArrayQueue<E> extends Queue<E>{
// insertion from tail, removal from head
public static final int DEFAULT_CAPACITY = 10;
private int capacity;
private int tail;
private int head;
private int size;
private E[] arr;
public ArrayQueue(){
this(DEFAULT_CAPACITY);
}
public ArrayQueue(int capacity){
this.capacity = capacity;
arr = (E[]) new Object[this.capacity];
size = 0;
}
@Override
public boolean isEmpty() {
return size == 0;
return false;
}
@Override
public E dequeue() {
if(isEmpty())
return null;
else{
E element = arr[head];
// Garbage collection
arr[head] = null;
head = (head+1)%capacity;
size = size - 1;
if(capacity >= 40 && size < capacity/4){
capacity = capacity/2;
resize(capacity, capacity*2);
}
return element;
}
}
@Override
public void enqueue(E element) {
// We plan capacity expansion if needed
if (size == capacity){
capacity = capacity * 2;
resize(capacity, capacity/2);
}
arr[tail] = element;
tail = (tail + 1) % capacity;
++size;
}
// When resize takes place always the original array is full, so copy the complete array as is
private void resize(int capacity, int oldCapacity) {
E[] resizedArr = (E[]) new Object[capacity];
for(int i = 0; i < size; i++) {
resizedArr[i] = arr[head];
// halving because capacity has now doubled
arr[head] = null;
head = (head + 1) % oldCapacity;
}
arr = resizedArr;
// When resizing takes place, we bring the head to 0 and the tail to size
// tail is where new inserts will be made and head will be where elements will be removed
tail = size;
head = 0;
}
@Override
public int size() {
return size;
return 0;
}
@Override
@Nonnull
public Iterator<E> iterator() {
return new Iterator<E>() {
int counter = size;
int pointer = head;
@Override
public boolean hasNext() {
return counter != 0;
}
@Override
public E next() {
E element = arr[pointer];
pointer = (pointer + 1)% capacity;
--counter;
return element;
}
};
return null;
}
}

View File

@ -5,94 +5,28 @@ import javax.annotation.Nonnull;
// Concrete implementation of stack using arrays
// Creating a generic stack of type E
public class ArrayStack<E> extends Stack<E> {
// Capacity and size are two variables, capacity determines total capacity of array, capacity defaults at 10
// every time size == capacity, capacity = 2 * capacity
private static final int DEFAULT_CAPACITY = 10;
private int capacity;
private int size;
private E[] arr;
public ArrayStack(int capacity){
this.capacity = capacity;
arr = (E[]) new Object[this.capacity];
}
// Constructor chaining, default constructor will call parametrized constructor with default initial capacity 10
public ArrayStack(){
this(DEFAULT_CAPACITY);
}
@Override
public boolean isEmpty() {
return size == 0;
return false;
}
private void changeCapacity(int newCapacity){
E[] resizedArr = (E[]) new Object[newCapacity];
for (int i = 0; i < size; i++)
resizedArr[i] = arr[i];
arr = resizedArr;
}
private void incrementSize(){
if (size == capacity){
capacity = 2 * capacity;
changeCapacity(capacity);
}
}
// Push always happens at the end of the stack
// Say the size of the stack is 1, new element gets inserted at 1
@Override
public void push(E element) {
// Lazy approach, we assume size to always be lesser than capacity
incrementSize();
arr[size++] = element;
}
@Override
public E pop() {
if (isEmpty())
return null;
else{
E e = arr[--size];
arr[size] = null;
checkResize();
return e;
}
}
private void checkResize() {
if (size < capacity / 4 && capacity >= 20){
capacity = capacity / 2;
changeCapacity(capacity);
}
}
@Override
public int size() {
return size;
return 0;
}
@Override
@Nonnull
public Iterator<E> iterator() {
return new Iterator<E>() {
int current = 0;
@Override
public boolean hasNext() {
return current != size;
}
@Override
public E next() {
E element = arr[current];
current = current + 1;
return element;
}
};
return null;
}
}

View File

@ -4,71 +4,28 @@ import javax.annotation.Nonnull;
import java.util.Iterator;
public class LinkedQueue<E> extends Queue<E>{
Node head;
Node tail;
int size;
private class Node{
E value;
Node next;
Node(E value){
this.value = value;
}
}
@Override
public boolean isEmpty() {
return size==0;
return false;
}
@Override
public E dequeue() {
if(isEmpty())
return null;
E element = head.value;
// Only a single element is present
if (head == tail){
tail = null;
}
head = head.next;
--size;
return element;
}
@Override
public void enqueue(E element) {
Node newNode = new Node(element);
if(isEmpty())
head = newNode;
else
tail.next = newNode;
tail = newNode;
++size;
}
@Override
public int size() {
return size;
return 0;
}
@Override
@Nonnull
public Iterator<E> iterator() {
return new Iterator<E>() {
Node current = head;
@Override
public boolean hasNext() {
return current != null;
}
@Override
public E next() {
E element = current.value;
current = current.next;
return element;
}
};
return null;
}
}

View File

@ -3,70 +3,28 @@ import java.util.Iterator;
// Creating a concrete linked Implementation of Stack
public class LinkedStack<E> extends Stack<E>{
// No need for an explicit constructor as size will be initialized to 0 and root to null
private int size;
private Node first;
// By default instance variables are package private
private class Node{
E value;
Node next;
}
// Will return true if size is 0
@Override
public boolean isEmpty() {
return (this.size == 0);
return false;
}
// Adds an element to the start of a linked list
@Override
public void push(E element) {
Node newNode = new Node();
newNode.value = element;
newNode.next = first;
first = newNode;
this.size = this.size + 1;
}
@Override
public E pop() {
if (this.isEmpty())
return null;
else{
Node toBePopped = first;
first = first.next;
this.size = this.size - 1;
return toBePopped.value;
}
}
hitanshu marked this conversation as resolved
Review

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
@Override
public int size() {
return this.size;
return 0;
}
@Override
public Iterator<E> iterator() {
return new Iterator<E>() {
// Internal classes can access outer objects
Node current = first;
@Override
public boolean hasNext() {
return current != null;
return null;
}
@Override
public E next() {
E element = current.value;
current = current.next;
return element;
}
};
}
}

View File

@ -4,12 +4,11 @@ public abstract class AbstractCustomSorts<E> {
public abstract void sort(E[] arr);
// TODO: Implement this method
public void exch(E[] arr, int j, int i) {
E temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// TODO: Implement this method
public boolean less(Comparable<E> e1, E e2) {
return e1.compareTo(e2) < 0;
}

View File

@ -1,23 +1,9 @@
package com.hithomelabs.princeton1.module5;
public class Insertion<E> extends AbstractCustomSorts<E> {
@Override
public void sort(E[] arr) {
if (arr == null) return;
else{
int N = arr.length;
// * * swap arr[i] with each element greater to it's left
for (int i = 1; i < N; i++){
int j = i;
while(j >= 1 && less((Comparable<E>)arr[j], arr[j-1])){
exch(arr, j, j-1);
j = j-1;
}
}
}
}
}

View File

@ -1,23 +1,13 @@
package com.hithomelabs.princeton1.module5;
public class Selection<E> extends AbstractCustomSorts<E>{
/*
* * Selection sort "selects" the smallest element and swaps it with arr[0] of the array
* * Then proceeds to do the same swapping arr[i] with arr[i:arr.length-1]
*/
public class Selection<E> extends AbstractCustomSorts<E>{
@Override
public void sort(E[] arr) {
if (arr == null) return;
Comparable<E>[] arr1 = (Comparable<E>[]) arr;
for(int i = 0; i < arr1.length - 1; i++){
int minIndex = i;
for(int j = i+1; j < arr.length; j ++){
if (less((Comparable<E>) arr[j], arr[minIndex])) minIndex = j;
}
exch(arr, i, minIndex);
}
}
}

View File

@ -24,33 +24,10 @@ public class Shell<E> extends AbstractCustomSorts<E> {
*/
@Override
public void sort(E[] arr) {
MetaData metaData = new MetaData();
int N = arr.length;
int h = 1;
// * * Calculates the largest value of h greater than n
while (3 * h + 1 < N) {
h = 3 * h + 1;
}
while (h >= 1) {
h = hsort(arr, h, metaData);
h = h / 3;
}
System.out.println("Array sorted (shell sort) with " + metaData.compares + " compares and " + metaData.swaps + " swaps");
}
private int hsort(E[] arr, int h, MetaData metadata) {
int N = arr.length;
for(int i = h; i < N; i++){
int j = i;
++metadata.compares;
while(j >= h && less((Comparable<E>) arr[j], arr[j-h])){
++metadata.swaps;
exch(arr, j, j-h);
j = j - h;
++metadata.compares;
}
}
return h;
return 0;
}
/*
! sample implementation of insertion sort as h-sort of h = 1