Skip to content

dynamic programming with java #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions Java Collections Dynamic Programming/linkedlist/LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package linkedlist;

public class LinkedList
{
Node head;
public void add(int data) {
Node node = new Node();
node.data = data;

if(head == null) {
head = node;
}
else {
Node n = head;
while(n.next != null) {
n = n.next;
}
n.next = node;
}
}


public int get(int index) {
Node n = head;
for(int i = 0; i < index; i++) {
n = n.next;
}
return n.data;
}

public int getFirst() {
return head.data;
}

public int getLast() {
return get(size() - 1);
}

public int size() {
Node n = head;
int size = 0;
if(head != null) {
while(n.next != null) {
size++;
n = n.next;
}
size++;
}
return size;
}

public boolean isEmpty() {
return size() == 0;
}

public void clear() {
head = null;
}

public void show() {
Node n = head;
while(n.next != null) {
System.out.println(n.data);
n = n.next;
}
System.out.println(n.data);
}
}
7 changes: 7 additions & 0 deletions Java Collections Dynamic Programming/linkedlist/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package linkedlist;

public class Node
{
int data;
Node next;
}
106 changes: 106 additions & 0 deletions Java Collections Dynamic Programming/queue/MyQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package queue;

public class MyQueue
{
private int[] arr;
private int size = 0;

public void enqueue(int data) {
expand();
arr[size-1] = data;
}

private void expand() {
int tempSize = size + 1;
int temp[] = new int[tempSize];
for(int i = 0; i < size; i++) {
temp[i] = arr[i];
}
arr = temp;
size = tempSize;
}

public int dequeue() {
int skip = 1;
int tempSize = size-1;
int temp[] = new int[tempSize];
for(int i = 0; i < size-1; i++) {
temp[i] = arr[skip+i];
}
int removedValue = arr[skip-1];
arr = temp;
size = tempSize;
return removedValue;
}

public int peek() {
return arr[0];
}

public int peekAt(int index) {
return arr[index];
}

public void clear() {
arr = new int[0];
size = 0;
}

public boolean contains(int data) {
for(int i = 0; i < size; i++) {
if(arr[i] == data) {
return true;
}
break;
}
return false;
}

public int dequeueAt(int index) {
int tempSize = size-1;
int temp[] = new int[tempSize];
int removedValue = arr[size-1];
boolean isZero = false;
isZero = (index == 0);
for(int i = 0; i < size-1; i++) {
if(i != index) {
if(!isZero) {
temp[i] = arr[i];
}
else {
temp[i] = arr[i+1];
}
}
else {
temp[i] = arr[i+1];
removedValue = arr[i];
}
}
arr = temp;
size = tempSize;
return removedValue;
}

public int[] copy(int[] data) {
int temp[] = new int[data.length];
for(int i = 0; i < data.length; i++) {
temp[i] = data[i];
}
return temp;
}

public boolean isEmpty() {
return size == 0;
}

public int size() {
return size;
}


public void show() {
for(int i = 0; i < size; i++) {
System.out.println(arr[i]);
}
}
}
34 changes: 34 additions & 0 deletions Java Collections Dynamic Programming/search/MySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package search;
import sort.*;

public class MySearch
{
public int linearSearch(int[] data, int target) {
for(int i = 0; i < data.length; i++) {
if(data[i] == target) {
return i;
}
}
return -1;
}

//always pass in a sorted array
public int binarySearch(int[] arr, int target) {
int left = 0;
int right = arr.length - 1;

while(left <= right) {
int mid = (left + right) / 2;
if(arr[mid] == target) {
return mid;
}
else if(arr[mid] < target) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return -1;
}
}
46 changes: 46 additions & 0 deletions Java Collections Dynamic Programming/sort/MySort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package sort;

public class MySort
{
private SortType type = SortType.ASC;


public int[] bubbleSort(int[] data, SortType mytype) {
for(int i = 0; i < data.length; i++) {
for(int j = 0; j < data.length - i - 1; j++) {
if(data[j] > data[j+1]) {
if(type.toString().equals(mytype.toString()))
swap(data, j, j+1);
}
else {
if(!type.toString().equals(mytype.toString()))
swap(data,j,j+1);
}
}
}

return data;
}

public void swap(int[] data, int index1, int index2) {
int temp = data[index1];
data[index1] = data[index2];
data[index2] = temp;
}

public int[] selectionSort(int[] data) {
for(int i = 0; i < data.length; i++) {
int minIndex = i;
for(int j = i+1; j < data.length; j++) {
if(data[j] < data[minIndex]) {
minIndex = j;
}
}
int temp = data[i];
data[i] = data[minIndex];
data[minIndex] = temp;
}
return data;
}

}
8 changes: 8 additions & 0 deletions Java Collections Dynamic Programming/sort/SortType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package sort;

public enum SortType
{
ASC,
DESC
}

Loading