Skip to content
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

Changes Done - (Sorting Data and Adding a Program) #14

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
142 changes: 71 additions & 71 deletions Dijkastra.java → Algorithm/Dijkastra.java
Original file line number Diff line number Diff line change
@@ -1,71 +1,71 @@
import java.util.*;
class Dijkstra {
private static final int V = 9; // Number of vertices in the graph
// A utility function to find the vertex with minimum distance value
int minDistance(int dist[], boolean sptSet[]) {
int min = Integer.MAX_VALUE, min_index = -1;
for (int v = 0; v < V; v++)
if (!sptSet[v] && dist[v] <= min) {
min = dist[v];
min_index = v;
}
return min_index;
}
// Function that implements Dijkstra's single source shortest path algorithm
void dijkstra(int graph[][], int src) {
int dist[] = new int[V]; // The output array. dist[i] will hold the shortest distance from src to i
boolean sptSet[] = new boolean[V]; // sptSet[i] will be true if vertex i is included in shortest path tree
// Initialize all distances as INFINITE and stpSet[] as false
for (int i = 0; i < V; i++) {
dist[i] = Integer.MAX_VALUE;
sptSet[i] = false;
}
// Distance of source vertex from itself is always 0
dist[src] = 0;
// Find shortest path for all vertices
for (int count = 0; count < V - 1; count++) {
// Pick the minimum distance vertex from the set of vertices not yet processed
int u = minDistance(dist, sptSet);
// Mark the picked vertex as processed
sptSet[u] = true;
// Update dist value of the adjacent vertices of the picked vertex
for (int v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v] != 0 && dist[u] != Integer.MAX_VALUE && dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
// Print the constructed distance array
printSolution(dist);
}
// A utility function to print the constructed distance array
void printSolution(int dist[]) {
System.out.println("Vertex \t Distance from Source");
for (int i = 0; i < V; i++)
System.out.println(i + " \t\t " + dist[i]);
}
public static void main(String[] args) {
int graph[][] = new int[][] { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
Dijkstra t = new Dijkstra();
t.dijkstra(graph, 0);
}
}
import java.util.*;

class Dijkstra {
private static final int V = 9; // Number of vertices in the graph

// A utility function to find the vertex with minimum distance value
int minDistance(int dist[], boolean sptSet[]) {
int min = Integer.MAX_VALUE, min_index = -1;

for (int v = 0; v < V; v++)
if (!sptSet[v] && dist[v] <= min) {
min = dist[v];
min_index = v;
}

return min_index;
}

// Function that implements Dijkstra's single source shortest path algorithm
void dijkstra(int graph[][], int src) {
int dist[] = new int[V]; // The output array. dist[i] will hold the shortest distance from src to i
boolean sptSet[] = new boolean[V]; // sptSet[i] will be true if vertex i is included in shortest path tree

// Initialize all distances as INFINITE and stpSet[] as false
for (int i = 0; i < V; i++) {
dist[i] = Integer.MAX_VALUE;
sptSet[i] = false;
}

// Distance of source vertex from itself is always 0
dist[src] = 0;

// Find shortest path for all vertices
for (int count = 0; count < V - 1; count++) {
// Pick the minimum distance vertex from the set of vertices not yet processed
int u = minDistance(dist, sptSet);

// Mark the picked vertex as processed
sptSet[u] = true;

// Update dist value of the adjacent vertices of the picked vertex
for (int v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v] != 0 && dist[u] != Integer.MAX_VALUE && dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}

// Print the constructed distance array
printSolution(dist);
}

// A utility function to print the constructed distance array
void printSolution(int dist[]) {
System.out.println("Vertex \t Distance from Source");
for (int i = 0; i < V; i++)
System.out.println(i + " \t\t " + dist[i]);
}

public static void main(String[] args) {
int graph[][] = new int[][] { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
Dijkstra t = new Dijkstra();
t.dijkstra(graph, 0);
}
}
File renamed without changes.
File renamed without changes.
52 changes: 52 additions & 0 deletions Binary Tree/Level_Order_Traversal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/

class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> ans=new ArrayList<>();

if(root==null)
return ans;

Queue<TreeNode> x=new LinkedList<>();
x.add(root);

while(x.size()!=0){
int i=x.size();
System.out.println(i);

List<Integer> temp=new ArrayList<>();

while(i!=0){
TreeNode ptr=x.peek();

if(ptr.left!=null)
x.add(ptr.left);
if(ptr.right!=null)
x.add(ptr.right);

x.remove();
temp.add(ptr.val);

i-=1;
}

ans.add(temp);
}

return ans;
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
58 changes: 29 additions & 29 deletions linkedlistcycle.java → LinkedList/linkedlistcycle.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
public class linkedlistcycle {
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public boolean hasCycle(ListNode head) {
if(head==null) return false;
ListNode fast = head;
ListNode slow = head;
while(fast.next!=null && fast.next.next!=null)
{
fast=fast.next.next;
slow=slow.next;
if(fast==slow) return true;
}
return false;
}
}
public class linkedlistcycle {
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/


public boolean hasCycle(ListNode head) {
if(head==null) return false;
ListNode fast = head;
ListNode slow = head;

while(fast.next!=null && fast.next.next!=null)
{
fast=fast.next.next;
slow=slow.next;
if(fast==slow) return true;
}
return false;
}
}

84 changes: 42 additions & 42 deletions sortLinkedList.java → LinkedList/sortLinkedList.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
//this is a brute force solution
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
public class sortLinkedList {
public ListNode sortList(ListNode head) {
int count =0;
ListNode temp = new ListNode();
temp = head;
while(temp!=null){
count++;
temp = temp.next;
}
int arr[] = new int[count];
temp = head;
int k =0;
while(temp!=null){
arr[k]=temp.val;
k++;
temp=temp.next;
}
Arrays.sort(arr);
temp = head;
k=0;
while(temp!=null){
temp.val=arr[k];
k++;
temp=temp.next;
}
return head;
}
}

//this is a brute force solution
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
public class sortLinkedList {
public ListNode sortList(ListNode head) {
int count =0;
ListNode temp = new ListNode();
temp = head;
while(temp!=null){
count++;
temp = temp.next;
}
int arr[] = new int[count];
temp = head;
int k =0;
while(temp!=null){
arr[k]=temp.val;
k++;
temp=temp.next;
}
Arrays.sort(arr);
temp = head;
k=0;
while(temp!=null){
temp.val=arr[k];
k++;
temp=temp.next;

}
return head;
}
}

57 changes: 29 additions & 28 deletions matrixbinarysearch.java → Searching/matrixbinarysearch.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
public class matrixbinarysearch {
public boolean searchMatrix(int[][] matrix, int target) {
int k = 0;

while (k <= matrix.length - 1) {

int start = 0;
int end = matrix[k].length - 1;
while (start <= end) {
int mid = (start + end ) / 2;
if (matrix[k][mid] > target) {
end = mid - 1;
} else if (matrix[k][mid] < target) {
start = mid + 1;
} else {
return true;

}
}
k++;
}


return false;
}
}


package Searching;
public class matrixbinarysearch {
public boolean searchMatrix(int[][] matrix, int target) {
int k = 0;

while (k <= matrix.length - 1) {

int start = 0;
int end = matrix[k].length - 1;
while (start <= end) {
int mid = (start + end ) / 2;
if (matrix[k][mid] > target) {
end = mid - 1;
} else if (matrix[k][mid] < target) {
start = mid + 1;
} else {
return true;

}
}
k++;
}


return false;
}
}


Loading