Skip to content
This repository was archived by the owner on May 29, 2024. It is now read-only.

Commit 3e4473f

Browse files
committed
Algorithms Documentation [Java]
1 parent 23dc7fc commit 3e4473f

35 files changed

+2820
-0
lines changed

algorithms/Java/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Python
2+
3+
## Arrays
4+
1. [Count Inversions](arrays/count-inversions.java)
5+
2. [Kadanes Algorithm](arrays/Kadanes_Algorithm.java)
6+
3. [Left Rotation](arrays/left_rotation.java)
7+
4. [Unique Digits of Large Number](arrays/unique-digits-of-large-number.java)
8+
9+
## Graphs
10+
1. [Dijkstras](graphs/Dijkstras.java)
11+
12+
## Linked Lists
13+
1. [Circular](linked-lists/circular.java)
14+
2. [Clone Linked List](linked-lists/clone-linkedlist-with-rnd-pointer.java)
15+
3. [Doubly](linked-lists/doubly.java)
16+
4. [Reverse](linked-lists/reverse.java)
17+
5. [Singly](linked-lists/singly.java)
18+
19+
## Queues
20+
1. [Circular Queue using Linked List](queues/circular-queue-linked-list.java)
21+
2. [Queue using Linked List](queues/queue-linked-list.java)
22+
23+
## Scheduling
24+
1. [Multi-Level Queue Scheduling](scheduling/multi-level-queue-scheduling.java)
25+
2. [Rund Robin](scheduling/round-robin.java)
26+
27+
## Searching
28+
1. [Binary Search](searching/binary-search.java)
29+
2. [Jump Search](searching/jump-search.java)
30+
3. [Linear Search](searching/linear-search.java)
31+
32+
## Sorting
33+
1. [Bubble Sort](sorting/bubble-sort.java)
34+
2. [Counting Sort](sorting/counting-sort.java)
35+
3. [Heap Sort](sorting/heap-sort.java)
36+
4. [Insertion Sort](sorting/insertion-sort.java)
37+
5. [Merge Sort](sorting/merge-sort.java)
38+
6. [Quick Sort](sorting/quick-sort.java)
39+
7. [Selection Sort](sorting/selection-sort.java)
40+
41+
## Stacks
42+
1. [Balanced Paranthesis](stacks/balanced-paranthesis.java)
43+
2. [Stack](stacks/stack.java)
44+
3. [The Stock Span Problem](stacks/the-stock-span-problem.java)
45+
46+
## Strings
47+
1. [KMP](strings/kmp.java)
48+
2. [Palindrome](strings/palindrome.java)
49+
3. [Rabin Krap](strings/rabin-karp.java)
50+
4. [Sequence](strings/sequence.java)
51+
5. [Split String](strings/SplitString.java)
52+
6. [Tokenizer](strings/tokenizer.java)
53+
54+
## Trees
55+
1. [Pre in Post Traversal](trees/pre_in_post_traversal.java)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Java program to print largest contiguous array sum
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
class Kadane
7+
{
8+
public static void main (String[] args)
9+
{
10+
int [] a = {-2, -3, 4, -1, -2, 1, 5, -3};
11+
System.out.println("Maximum contiguous sum is " +
12+
maxSubArraySum(a));
13+
}
14+
15+
static int maxSubArraySum(int a[])
16+
{
17+
int size = a.length;
18+
int max_so_far = Integer.MIN_VALUE, max_ending_here = 0;
19+
20+
for (int i = 0; i < size; i++)
21+
{
22+
max_ending_here = max_ending_here + a[i];
23+
if (max_so_far < max_ending_here)
24+
max_so_far = max_ending_here;
25+
if (max_ending_here < 0)
26+
max_ending_here = 0;
27+
}
28+
return max_so_far;
29+
}
30+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Algorithm Type: Divide & Conquer
2+
// Time Complexity: O(n*log(n))
3+
4+
import java.io.File;
5+
import java.lang.reflect.Array;
6+
import java.util.*;
7+
8+
public class inversions {
9+
private static long count_split_inv(int[] arr, int[] left, int[] right) {
10+
long split_inv = 0;
11+
int ridx = 0, lidx = 0;
12+
int size = arr.length;
13+
int rsize = right.length;
14+
int lsize = left.length;
15+
for (int i = 0; i < size; i++) {
16+
if (lidx != lsize && ridx != rsize) {
17+
if (right[ridx] <= left[lidx]) {
18+
arr[i] = right[ridx];
19+
ridx++;
20+
split_inv += lsize - lidx;
21+
} else {
22+
arr[i] = left[lidx];
23+
lidx++;
24+
}
25+
} else if (lidx == lsize) {
26+
arr[i] = right[ridx];
27+
ridx++;
28+
} else if (ridx == rsize) {
29+
arr[i] = left[lidx];
30+
lidx++;
31+
}
32+
}
33+
return split_inv;
34+
}
35+
36+
private static long count_inversions(int[] arr) {
37+
int size = arr.length;
38+
if (size == 1) {
39+
return 0;
40+
}
41+
int[] left = Arrays.copyOfRange(arr, 0, size / 2);
42+
int[] right = Arrays.copyOfRange(arr, size / 2, size);
43+
long left_inv = count_inversions(left);
44+
long right_inv = count_inversions(right);
45+
long split_inv = count_split_inv(arr, left, right);
46+
47+
return left_inv + right_inv + split_inv;
48+
}
49+
50+
public static void main(String[] args) {
51+
try {
52+
int[] arr = {8, 2, 1, 5, 7, 3, 9, 2, 0, 1};
53+
System.out.println(count_inversions(arr));
54+
} catch (Exception e) {
55+
System.out.println("Err... ");
56+
}
57+
58+
}
59+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
public class left_rotation{
2+
3+
public static void rotateToLeft(int [] arr){
4+
if(arr == null || arr.length == 0){
5+
System.out.println("no rotation possible");
6+
return;
7+
}
8+
//take the first element
9+
int firstElement = arr[0];
10+
//move everything in the left
11+
for(int i = 0; i < arr.length -1; i++){
12+
arr[i] = arr[i + 1];
13+
}
14+
//the first element become the last element
15+
arr[arr.length - 1] = firstElement;
16+
}
17+
18+
public static void print(int [] arr){
19+
System.out.print("[");
20+
for(int i = 0; i < arr.length; i++)
21+
System.out.print(arr[i] + ", ");
22+
System.out.println("]");
23+
}
24+
25+
public static void main(String [] args){
26+
int [] arr = {1,2,3,4,5,6,7,8,9};
27+
int n = 3; //number of times to rotate the array
28+
29+
System.out.print("before: ");
30+
print(arr);
31+
32+
System.out.println("rotating " + n + " times to left");
33+
for(int i = 0; i < n; i++)
34+
rotateToLeft(arr); //move to left n times
35+
36+
System.out.print("after: ");
37+
print(arr);
38+
}
39+
}
40+
41+
/*
42+
to run the file:
43+
javac left_rotation.java
44+
java left_rotation
45+
46+
result:
47+
before: [1, 2, 3, 4, 5, 6, 7, 8, 9, ]
48+
rotating 3 times to left
49+
after: [4, 5, 6, 7, 8, 9, 1, 2, 3, ]
50+
51+
*/
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.util.*;
2+
public class UniqueDigitsOfLargeNumber {
3+
4+
public static void main(String[] args) {
5+
// TODO Auto-generated method stub
6+
Scanner sc=new Scanner(System.in);
7+
int l,i,j,l1=0;
8+
String s,p="";
9+
char c,d;
10+
System.out.println("Enter a large number");
11+
s=sc.nextLine();
12+
l=s.length();
13+
for(i=0;i<l;i++)
14+
{
15+
c=s.charAt(i);
16+
for(j=0;j<l1;j++)
17+
{
18+
if(p.charAt(j)==c)
19+
break;
20+
}
21+
if(j==l1)
22+
{
23+
p=p+c;
24+
l1=p.length();
25+
}
26+
}
27+
System.out.print("The unique digits present in "+Long.parseLong(s)+" are ");
28+
if(l1>1)
29+
{
30+
for(i=0;i<l1-1;i++)
31+
{
32+
System.out.print(p.charAt(i)+", ");
33+
}
34+
System.out.println("and "+p.charAt(l1-1)+".");
35+
c=p.charAt(0);
36+
for(i=0;i<l1;i++)
37+
{
38+
if(p.charAt(i)>c)
39+
c=p.charAt(i);
40+
}
41+
s="";
42+
s=s+c;
43+
c--;
44+
for(d=c;d>='0';d--)
45+
{
46+
for(i=0;i<l1;i++)
47+
{
48+
if(p.charAt(i)==d)
49+
s=s+d;
50+
}
51+
}
52+
System.out.println("The largest number possible out of these unique digits is "+Long.parseLong(s));
53+
}
54+
else
55+
{
56+
System.out.println(p.charAt(0)+".");
57+
System.out.println("The largest number possible out of these unique digits is "+Integer.parseInt(p));
58+
}
59+
60+
}
61+
62+
}

algorithms/Java/graphs/Dijkstras.java

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import java.util.*;
2+
3+
class AdjListNode
4+
{
5+
int dest;
6+
int weight;
7+
AdjListNode(int dest,int weight)
8+
{
9+
this.dest=dest;
10+
this.weight=weight;
11+
}
12+
}
13+
14+
class NodeComparator implements Comparator<AdjListNode>
15+
{
16+
@Override
17+
public int compare(AdjListNode node1, AdjListNode node2)
18+
{
19+
if (node1.weight < node2.weight)
20+
return -1;
21+
if (node1.weight > node2.weight)
22+
return 1;
23+
return 0;
24+
}
25+
}
26+
27+
class Dijkstras
28+
{
29+
public int[] dijkstras(List<List<AdjListNode>> adj_list,int source)
30+
{
31+
int[] distance = new int[adj_list.size()];
32+
Set<Integer> completed = new HashSet<Integer>();
33+
for(int i=0;i<distance.length;++i)
34+
{
35+
distance[i] = Integer.MAX_VALUE;
36+
}
37+
PriorityQueue<AdjListNode> pq = new PriorityQueue<>(adj_list.size(),new NodeComparator());
38+
AdjListNode source_node = new AdjListNode(source,0);
39+
for(int i=0;i<distance.length;++i)
40+
{
41+
pq.add(new AdjListNode(i,Integer.MAX_VALUE));
42+
43+
}
44+
pq.add(source_node);
45+
distance[source]=0;
46+
while(!pq.isEmpty())
47+
{
48+
AdjListNode current = pq.remove();
49+
if(!completed.contains(current.dest))
50+
{
51+
completed.add(current.dest);
52+
for(AdjListNode n :adj_list.get(current.dest) )
53+
{
54+
if( distance[n.dest] > (distance[current.dest]+n.weight))
55+
{
56+
distance[n.dest] = distance[current.dest]+n.weight;
57+
pq.add(new AdjListNode(n.dest, distance[n.dest]));
58+
}
59+
}
60+
}
61+
}
62+
return distance;
63+
}
64+
65+
public static void main(String args[])
66+
{
67+
/*
68+
SAMPLE INPUT AND OUTPUT
69+
___________________________
70+
71+
Input:
72+
__________
73+
Please enter the number of nodes N. Vertices will be [0,N-1]
74+
6
75+
Please Enter the number of Edges
76+
9
77+
Please enter each edge in the sequence <starting node> <destination node> <weight>
78+
0 1 1
79+
0 2 5
80+
1 2 2
81+
1 4 1
82+
1 3 2
83+
2 4 2
84+
3 5 1
85+
3 4 3
86+
4 5 2
87+
Please enter source vertex
88+
0
89+
90+
Output:
91+
___________
92+
Distances from source 0
93+
Node0--->0
94+
Node1--->1
95+
Node2--->3
96+
Node3--->3
97+
Node4--->2
98+
Node5--->4
99+
*/
100+
101+
List<List<AdjListNode>> adj_list = new ArrayList<List<AdjListNode>>();
102+
System.out.println("Please enter the number of nodes N. Vertices will be [0,N-1]");
103+
Scanner sc = new Scanner(System.in);
104+
int v = sc.nextInt();
105+
for(int i=0;i<v;++i)
106+
adj_list.add(new ArrayList<AdjListNode>());
107+
108+
System.out.println("Please enter the number of edges");
109+
int e = sc.nextInt();
110+
System.out.println("Please enter each edge in the sequence <starting node> <destination node> <weight>");
111+
// Sample Data: 0 2 5 (edge from 0 to 2 with weight 5)
112+
for(int i=0;i<e;++i)
113+
{
114+
int startnode = sc.nextInt();
115+
int destnode = sc.nextInt();
116+
int weight = sc.nextInt();
117+
adj_list.get(startnode).add(new AdjListNode(destnode,weight));
118+
}
119+
int source;
120+
System.out.println("Please enter source vertex");
121+
source = sc.nextInt();
122+
Dijkstras d = new Dijkstras();
123+
int[] distances = d.dijkstras(adj_list, source); //source vertex is taken as 0
124+
System.out.println("Distances from source "+source);
125+
for(int i=0;i<distances.length;++i)
126+
{
127+
if(distances[i]==Integer.MAX_VALUE)
128+
System.out.println("Node"+i+"--->"+"infinity");
129+
else
130+
System.out.println("Node"+i+"--->"+distances[i]);
131+
}
132+
}
133+
}

0 commit comments

Comments
 (0)