Skip to content

Commit 0647c7b

Browse files
committed
Java Programs
1 parent 3e2a62b commit 0647c7b

File tree

61 files changed

+2175
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2175
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package misc;
2+
3+
/**
4+
* Algorithm:
5+
* 1. Create two linked list which will represent two numbers.
6+
* 2. Reverse both linked list.
7+
* 3. Add two node values (Each node is being represented as single digit) starting from heads of two linkedlist.
8+
* 4. If sum is of above two node values is more than 10, then forward the carry.
9+
*
10+
*/
11+
public class AddTwoNumbersRepresentedByLinkedList {
12+
13+
private static Node head;
14+
15+
private static class Node {
16+
private int value;
17+
private Node next;
18+
19+
Node(int value) {
20+
this.value = value;
21+
}
22+
}
23+
24+
public void addToTheLast(Node node) {
25+
if (head == null) {
26+
head = node;
27+
} else {
28+
Node temp = head;
29+
while (temp.next != null)
30+
temp = temp.next;
31+
32+
temp.next = node;
33+
}
34+
}
35+
36+
public void printList(Node printNode) {
37+
Node temp = printNode;
38+
while (temp != null) {
39+
System.out.format("%d ", temp.value);
40+
temp = temp.next;
41+
}
42+
System.out.println();
43+
}
44+
45+
public static Node reverseLinkedList(Node node) {
46+
if (node == null || node.next == null) {
47+
return node;
48+
}
49+
Node remaining = reverseLinkedList(node.next);
50+
node.next.next = node;
51+
node.next = null;
52+
return remaining;
53+
}
54+
55+
// This function will do sum of numbers represented by linked list
56+
public Node findSumOfNumbers(Node l1, Node l2) {
57+
int carry = 0;
58+
Node newHead = null;
59+
Node tempNodeForIteration = null;
60+
int sum = 0;
61+
int firstIter = 0;
62+
while (l1 != null || l2 != null) {
63+
firstIter++;
64+
sum = carry;
65+
if (l1 != null) {
66+
sum = sum + l1.value;
67+
l1 = l1.next;
68+
}
69+
if (l2 != null) {
70+
sum = sum + l2.value;
71+
l2 = l2.next;
72+
}
73+
carry = sum / 10;
74+
sum = sum % 10;
75+
// Check if it first node for the result
76+
if (firstIter == 1) {
77+
tempNodeForIteration = new Node(sum);
78+
newHead = tempNodeForIteration;
79+
} else {
80+
Node tempSumNode = new Node(sum);
81+
tempNodeForIteration.next = tempSumNode;
82+
tempNodeForIteration = tempNodeForIteration.next;
83+
}
84+
}
85+
if (carry != 0) {
86+
Node tempNode = new Node(carry);
87+
tempNodeForIteration.next = tempNode;
88+
}
89+
return newHead;
90+
}
91+
92+
public static void main(String[] args) {
93+
AddTwoNumbersRepresentedByLinkedList list = new AddTwoNumbersRepresentedByLinkedList();
94+
95+
Node head1 = new Node(5);
96+
list.addToTheLast(head1);
97+
list.addToTheLast(new Node(6));
98+
list.addToTheLast(new Node(7));
99+
list.addToTheLast(new Node(1));
100+
list.addToTheLast(new Node(2));
101+
System.out.print("Number 1: ");
102+
list.printList(head1);
103+
head = null;
104+
Node head2 = new Node(6);
105+
list.addToTheLast(head2);
106+
list.addToTheLast(new Node(3));
107+
list.addToTheLast(new Node(5));
108+
list.addToTheLast(new Node(9));
109+
System.out.print("Number 2: ");
110+
list.printList(head2);
111+
head1 = reverseLinkedList(head1);
112+
head2 = reverseLinkedList(head2);
113+
114+
// function to find sum of two linkedlist represent by number
115+
Node result = list.findSumOfNumbers(head1, head2);
116+
result = reverseLinkedList(result);
117+
System.out.print("Sum: ");
118+
list.printList(result);
119+
}
120+
}

java-programs/ArrayListToArray.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package misc;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class ArrayListToArray {
8+
9+
public static void main(String[] args) {
10+
List<String> list = new ArrayList<>();
11+
list.add("Apple");
12+
list.add("Banana");
13+
list.add("Mango");
14+
System.out.println("Converting ArrayList to Array: ");
15+
String[] item = list.toArray(new String[list.size()]);
16+
for (String s : item) {
17+
System.out.println(s);
18+
}
19+
System.out.println("Converting Array to ArrayList: ");
20+
List<String> list2 = new ArrayList<>();
21+
list2 = Arrays.asList(item);
22+
System.out.println(list2);
23+
}
24+
}

java-programs/ArraySort.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package misc;
2+
import java.util.Arrays;
3+
/**
4+
* Note:-
5+
* java.util.Arrays uses quicksort for primitive types such as int and
6+
* mergesort for objects that implement Comparable or use a Comparator.
7+
*/
8+
public class ArraySort {
9+
10+
public static void main(String[] args) {
11+
String[] countries = {"India","United States","Malaysia","Australia","Lundon"};
12+
Arrays.sort(countries);
13+
for(int i = 0; i < countries.length; i++) {
14+
System.out.println(countries[i]);
15+
}
16+
}
17+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package misc;
2+
/**
3+
* Note:- In BST all the nodes in the left sub-tree of the root node should be
4+
* less than or equals to the data of the root. The data of all the nodes in the
5+
* right subtree of the root node should be greater than the data of the root.
6+
*
7+
*/
8+
public class BinarySearchTreeImpl {
9+
10+
public class BSTNode {
11+
12+
private BSTNode left;
13+
private BSTNode right;
14+
private Integer data;
15+
16+
public BSTNode(Integer data) {
17+
this.data = data;
18+
}
19+
20+
public BSTNode getLeft() {
21+
return left;
22+
}
23+
24+
public void setLeft(BSTNode left) {
25+
this.left = left;
26+
}
27+
28+
public BSTNode getRight() {
29+
return right;
30+
}
31+
32+
public void setRight(BSTNode right) {
33+
this.right = right;
34+
}
35+
36+
public Integer getData() {
37+
return data;
38+
}
39+
}
40+
41+
private BSTNode root;
42+
43+
public boolean isEmpty() {
44+
return (this.root == null);
45+
}
46+
47+
public void insert(Integer data) {
48+
System.out.print("[input: " + data + "]");
49+
if (root == null) {
50+
this.root = new BSTNode(data);
51+
System.out.println(" -> inserted: " + data);
52+
return;
53+
}
54+
insertNode(this.root, data);
55+
System.out.println(" -> inserted: " + data);
56+
System.out.println();
57+
}
58+
59+
private BSTNode insertNode(BSTNode root, Integer data) {
60+
BSTNode tmpNode = null;
61+
System.out.print(" -> " + root.getData());
62+
if (root.getData() >= data) {
63+
System.out.print(" [L]");
64+
if (root.getLeft() == null) {
65+
root.setLeft(new BSTNode(data));
66+
return root.getLeft();
67+
} else {
68+
System.out.println(" [R]");
69+
}
70+
71+
} else {
72+
System.out.print(" [R]");
73+
if (root.getRight() == null) {
74+
root.setRight(new BSTNode(data));
75+
return root.getRight();
76+
} else {
77+
tmpNode = root.getRight();
78+
}
79+
}
80+
return insertNode(tmpNode, data);
81+
}
82+
83+
public static void main(String[] args) {
84+
BinarySearchTreeImpl bst = new BinarySearchTreeImpl();
85+
bst.insert(50);
86+
bst.insert(30);
87+
bst.insert(20);
88+
bst.insert(40);
89+
bst.insert(70);
90+
bst.insert(60);
91+
bst.insert(80);
92+
}
93+
}

java-programs/BubbleSort.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package misc;
2+
/**
3+
* Steps:-
4+
* Bubble Sort algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.
5+
*
6+
*/
7+
public class BubbleSort {
8+
9+
public void bubbleSort(int[] arr) {
10+
int n = arr.length;
11+
int tmp = 0;
12+
for (int i = 0; i < n ; i++) {
13+
for (int j = 1; j < n-1; j++) {
14+
if (arr[j-1] > arr[j]) {
15+
tmp = arr[j - 1];
16+
arr[j-1] = arr[j];
17+
arr[j] = tmp;
18+
}
19+
}
20+
}
21+
}
22+
23+
void printArray(int arr[]) {
24+
for (int i = 0; i < arr.length; i++) {
25+
System.out.print(arr[i] + " ");
26+
}
27+
System.out.println("");
28+
}
29+
30+
public static void main(String[] args) {
31+
BubbleSort obj = new BubbleSort();
32+
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
33+
34+
obj.bubbleSort(arr);
35+
System.out.println("Sorted Array");
36+
obj.printArray(arr);
37+
}
38+
}

java-programs/BucketSort.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package misc;
2+
import java.util.Arrays;
3+
/**
4+
* Bucket Sort is also known as bin sort.
5+
* It works by distributing the element into the array also called buckets.
6+
*
7+
*/
8+
public class BucketSort {
9+
10+
public static int[] bucketSort(int[] arr) {
11+
int i, j;
12+
int[] bucket = new int[arr.length + 1];
13+
Arrays.fill(bucket, 0);
14+
15+
for(i = 0; i < arr.length; i++) {
16+
bucket[arr[i]]++;
17+
}
18+
int k = 0;
19+
for(i = 0; i <= arr.length; i++) {
20+
for(j = 0; j < bucket[i]; j++) {
21+
arr[k++] = i;
22+
}
23+
}
24+
return arr;
25+
}
26+
public static void main(String[] args) {
27+
int[] num = {3, 6, 1, 7, 2, 8, 10, 4, 9, 5};
28+
bucketSort(num);
29+
for(int i = 0; i < num.length; i++) {
30+
System.out.print(num[i] + " ");
31+
}
32+
}
33+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package misc;
2+
3+
public class CheckNumberInString {
4+
5+
public static void main(String[] args) {
6+
String str = "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.";
7+
Boolean flag = false;
8+
for(int i = 0; i < str.length(); i++) {
9+
flag = Character.isDigit(str.charAt(i));
10+
if(flag) {
11+
System.out.println(str + "\n ==> contains number"); break;
12+
}
13+
}
14+
}
15+
}

java-programs/CollectionSort.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package misc;
2+
import java.util.ArrayList;
3+
import java.util.Collections;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
/**
7+
* Java Program to Sort a List using Collections.Sort methods
8+
* This implementation uses merge sort.
9+
*
10+
*/
11+
public class CollectionSort {
12+
13+
public static void main(String[] args) {
14+
15+
List<String> list = new ArrayList<>();
16+
list.add("India");
17+
list.add("India");
18+
list.add("United States");
19+
list.add("Malaysia");
20+
list.add("Australia");
21+
list.add("Lundon");
22+
23+
Collections.sort(list);
24+
Iterator<String> itr = list.iterator();
25+
while(itr.hasNext()) {
26+
System.out.println(itr.next());
27+
}
28+
}
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package misc;
2+
import java.util.ArrayList;
3+
import java.util.List;
4+
5+
public class ConvertArrayListToStringArray {
6+
public static void main(String args[]) {
7+
List<String> list = new ArrayList<String>();
8+
9+
list.add("One");
10+
list.add("Two");
11+
list.add("Three");
12+
list.add("Four");
13+
list.add("Five");
14+
15+
String[] stringArrayObject = new String[list.size()];
16+
list.toArray(stringArrayObject);
17+
18+
for(String str: stringArrayObject) {
19+
System.out.print(str + ", ");
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)