Skip to content
143 changes: 107 additions & 36 deletions week1/1-Vector/Vector.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,115 @@
public class Vector<T> {

// Adds value at a specific index in the Vector.
// Complexity: O(n)
public void insert(int index, T value) {
// ...
}
/**
*
* @author qvka
*/
public class Vector<T> {
private T[] array;
private int size;
public Vector(){
this.size = 0;
this.array = (T[]) new Object[8];
}
public Vector(int capacity){
this.size = 0;
this.array = (T[]) new Object[capacity];
}
// Adds value at a specific index in the Vector.
// Complexity: O(n)
public void insert(int index, T value) {
if(index > size || index < 0){
throw new IndexOutOfBoundsException();
}
if(size == array.length){
expand();
}
int i = size;

while( i > index ){
array[i] = array[i-1];
i--;
}
array[index] = value;
size++;
}

// Adds value to the end of the Vector.
// Complexity: O(1)
public void add(T value) {
// ...
}
// Adds value to the end of the Vector.
// Complexity: O(1)
public void add(T value) {
if(size == array.length){
expand();
}
array[size] = value;
size++;
}
private void expand(){
T[] temp = (T[]) new Object[array.length*2];
System.arraycopy(array, 0, temp, 0, size);
array = temp;
}
// Returns value at a specific index in the Vector
// Complexity: O(1)
public T get(int index) {
if( index >= size || index < 0){
throw new IndexOutOfBoundsException();
}
return (T)array[index];
}

// Returns value at a specific index in the Vector
// Complexity: O(1)
public T get(int index) {
// ...
}
// Removes element at the specific index
// Complexity: O(n)
public void remove(int index) {
if(size == 0){
return;
}
if( index < 0 || index >= size){
throw new IndexOutOfBoundsException();
}
int i = index+1;
while( i < size){
array[i-1] = array[i];
i++;
}
size--;
}

// Removes element at the specific index
// Complexity: O(n)
public void remove(int index) {
// ...
}
// Removes element at the last index
// Complexity: O(1)
public T pop() {
if(size == 0){
return null;
}
size--;
return (T)array[size+1];
}

// Removes element at the last index
// Complexity: O(1)
public T pop() {
// ...
}
// Returns the number of elements in the Vector.
// Complexity: O(1)
public int size() {
return size;
}

// Returns the number of elements in the Vector.
// Complexity: O(1)
public int size() {
// ...
}
// Returns the total capacity of the Vector.
// Complexity: O(1)
public int capacity() {
return array.length;
}

// Returns the total capacity of the Vector.
// Complexity: O(1)
public int capacity() {
// ...
}
public static void main(String[] args) {
Vector<Integer> a = new Vector(5);
System.out.println(a.capacity() + " " +a.size());
a.add(2);
System.out.println(a.get(0));
a.insert(0, 1);
System.out.println(a.get(0));
a.remove(0);
a.add(6);
a.add(6);
a.add(6);
a.add(6);
a.add(6);
a.add(6);
System.out.println(a.capacity());

}
}
57 changes: 36 additions & 21 deletions week1/2-Queue/Queue.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
class Queue<T> {

// Adds value to the end of the Queue.
// Complexity: O(1)
public void push(T value) {
// ...
}
import java.util.LinkedList;


class Queue<T> {
private LinkedList<T> q;
public Queue(){
q = new LinkedList();
}
// Adds value to the end of the Queue.
// Complexity: O(1)
public void push(T value) {
q.add(value);
}

// Returns value from the front of the Queue and removes it.
// Complexity: O(1)
public T pop() {
// ...
}
// Returns value from the front of the Queue and removes it.
// Complexity: O(1)
public T pop() {
if(q.size() < 1){
return null;
}
T temp = q.getFirst();
q.removeFirst();
return temp;
}

// Returns value from the front of the Queue without removing it.
// Complexity: O(1)
public T peek() {
// ...
}
// Returns value from the front of the Queue without removing it.
// Complexity: O(1)
public T peek() {
if(q.size() < 1){
return null;
}
return q.getFirst();
}

// Returns the number of elements in the Queue.
// Complexity: O(1)
public int size() {
// ...
}
// Returns the number of elements in the Queue.
// Complexity: O(1)
public int size() {
return q.size();
}
}
10 changes: 5 additions & 5 deletions week1/4-Complexities/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ is_prime(number) {
}
```

Complexity: ...
Complexity: O(n)

## Checking if a string is palindrome

Expand All @@ -37,7 +37,7 @@ is_palindrome(string) {
}
```

Complexity: ...
Complexity: O(n)

## Summing elements of a matrix

Expand All @@ -49,7 +49,7 @@ for (i = 0; i < n; i++) {
}
```

Complexity: ...
Complexity: O(n*m)

## Counting 1

Expand All @@ -61,7 +61,7 @@ for (i = 0; i < n; i++) {
}
```

Complexity: ...
Complexity: O(n^2)

## Counting 2

Expand All @@ -73,4 +73,4 @@ for (i = 0; i < n; i++) {
}
```

Complexity: ...
Complexity: O(n logn)
23 changes: 20 additions & 3 deletions week2/1-Roots/Roots.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
public class Roots {

// Finds the square root of a number using binary search.
public double squareRoot(int number) {
// ...
}
public static double findRoot(int n){
double left = 1;
double right = n;
double mid = left + (right-left)/2;
int counter=0;
while (Math.abs((mid * mid) - n) > 0.000001) {
mid = left + ( right - left ) / 2;
if( (mid * mid) > n ){
right=mid;
}else{
left= mid;
}
counter++;
}
if(counter >= 68){
System.out.println(n +" steps : " + counter);
}
return mid;

}
}
Loading