diff --git a/Data-Structures/Trees/AVLTree.java b/Data-Structures/Trees/AVLTree.java index e284ece..5a1c796 100644 --- a/Data-Structures/Trees/AVLTree.java +++ b/Data-Structures/Trees/AVLTree.java @@ -1,239 +1,239 @@ -import java.util.*; - -public class Main { - - static class Node { - int data; - int height; - Node left, right; - - Node(int data) { - this.data = data; - this.height = 1; - } - } - - static class AVL { - Node root; - - // -------- UTILITIES -------- - int height(Node n) { - return n == null ? 0 : n.height; - } - - int getBalance(Node n) { - return n == null ? 0 : height(n.left) - height(n.right); - } - - void updateHeight(Node n) { - n.height = 1 + Math.max(height(n.left), height(n.right)); - } - - // -------- ROTATIONS -------- - Node rightRotate(Node y) { - Node x = y.left; - Node t2 = x.right; - - x.right = y; - y.left = t2; - - updateHeight(y); - updateHeight(x); - - return x; - } - - Node leftRotate(Node x) { - Node y = x.right; - Node t2 = y.left; - - y.left = x; - x.right = t2; - - updateHeight(x); - updateHeight(y); - - return y; - } - - // -------- INSERT (ITERATIVE) -------- - public void insert(int key) { - if (root == null) { - root = new Node(key); - return; - } - - Deque stack = new ArrayDeque<>(); - Node cur = root; - - while (cur != null) { - stack.push(cur); - - if (key < cur.data) { - if (cur.left == null) { - cur.left = new Node(key); - stack.push(cur.left); - break; - } - cur = cur.left; - } else if (key > cur.data) { - if (cur.right == null) { - cur.right = new Node(key); - stack.push(cur.right); - break; - } - cur = cur.right; - } else { - return; // no duplicates - } - } - - rebalance(stack); - } - - // -------- DELETE (ITERATIVE) -------- - public void delete(int key) { - if (root == null) return; - - Deque stack = new ArrayDeque<>(); - Node cur = root, parent = null; - - // Find node to delete - while (cur != null && cur.data != key) { - stack.push(cur); - parent = cur; - cur = key < cur.data ? cur.left : cur.right; - } - - if (cur == null) return; // not found - - // If node has two children, replace with inorder successor - if (cur.left != null && cur.right != null) { - stack.push(cur); - - Node succParent = cur; - Node succ = cur.right; - - while (succ.left != null) { - stack.push(succ); - succParent = succ; - succ = succ.left; - } - - cur.data = succ.data; // copy successor value - parent = succParent; - cur = succ; // now delete successor node - } - - // cur has at most one child - Node child = (cur.left != null) ? cur.left : cur.right; - - if (parent == null) { - root = child; - } else if (parent.left == cur) { - parent.left = child; - } else { - parent.right = child; - } - - rebalance(stack); - } - - // -------- REBALANCE -------- - void rebalance(Deque stack) { - while (!stack.isEmpty()) { - Node node = stack.pop(); - updateHeight(node); - - int balance = getBalance(node); - - if (balance > 1) { - if (getBalance(node.left) < 0) { - node.left = leftRotate(node.left); - } - node = rightRotate(node); - } else if (balance < -1) { - if (getBalance(node.right) > 0) { - node.right = rightRotate(node.right); - } - node = leftRotate(node); - } - - if (stack.isEmpty()) { - root = node; - } else { - Node parent = stack.peek(); - if (node.data < parent.data) { - parent.left = node; - } else { - parent.right = node; - } - } - } - } - - // -------- TRAVERSALS -------- - public void inorder() { - inorder(root); - System.out.println(); - } - - void inorder(Node n) { - if (n == null) return; - inorder(n.left); - System.out.print(n.data + " "); - inorder(n.right); - } - - public void levelOrder() { - if (root == null) return; - - Queue q = new ArrayDeque<>(); - q.add(root); - - while (!q.isEmpty()) { - Node n = q.poll(); - System.out.print(n.data + " "); - - if (n.left != null) q.add(n.left); - if (n.right != null) q.add(n.right); - } - System.out.println(); - } - } - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - AVL tree = new AVL(); - - int ch; - do { - System.out.println("\n--- AVL Tree Operations Menu ---"); - System.out.println("1. Insert"); - System.out.println("2. Delete"); - System.out.println("3. Inorder"); - System.out.println("4. LevelOrder"); - System.out.println("5. Exit"); - System.out.print("Choice: "); - - ch = sc.nextInt(); - - switch (ch) { - case 1 -> { - System.out.print("Enter value to Insert: "); - tree.insert(sc.nextInt()); - } - case 2 -> { - System.out.print("Enter value to Delete: "); - tree.delete(sc.nextInt()); - } - case 3 -> tree.inorder(); - case 4 -> tree.levelOrder(); - case 5 -> System.out.println("Exiting..."); - default -> System.out.println("Invalid choice!"); - } - } while (ch != 5); - - sc.close(); - } -} +import java.util.*; + +public class AVLTree { + + static class Node { + int data; + int height; + Node left, right; + + Node(int data) { + this.data = data; + this.height = 1; + } + } + + static class AVL { + Node root; + + // -------- UTILITIES -------- + int height(Node n) { + return n == null ? 0 : n.height; + } + + int getBalance(Node n) { + return n == null ? 0 : height(n.left) - height(n.right); + } + + void updateHeight(Node n) { + n.height = 1 + Math.max(height(n.left), height(n.right)); + } + + // -------- ROTATIONS -------- + Node rightRotate(Node y) { + Node x = y.left; + Node t2 = x.right; + + x.right = y; + y.left = t2; + + updateHeight(y); + updateHeight(x); + + return x; + } + + Node leftRotate(Node x) { + Node y = x.right; + Node t2 = y.left; + + y.left = x; + x.right = t2; + + updateHeight(x); + updateHeight(y); + + return y; + } + + // -------- INSERT (ITERATIVE) -------- + public void insert(int key) { + if (root == null) { + root = new Node(key); + return; + } + + Deque stack = new ArrayDeque<>(); + Node cur = root; + + while (cur != null) { + stack.push(cur); + + if (key < cur.data) { + if (cur.left == null) { + cur.left = new Node(key); + stack.push(cur.left); + break; + } + cur = cur.left; + } else if (key > cur.data) { + if (cur.right == null) { + cur.right = new Node(key); + stack.push(cur.right); + break; + } + cur = cur.right; + } else { + return; // no duplicates + } + } + + rebalance(stack); + } + + // -------- DELETE (ITERATIVE) -------- + public void delete(int key) { + if (root == null) return; + + Deque stack = new ArrayDeque<>(); + Node cur = root, parent = null; + + // Find node to delete + while (cur != null && cur.data != key) { + stack.push(cur); + parent = cur; + cur = key < cur.data ? cur.left : cur.right; + } + + if (cur == null) return; // not found + + // If node has two children, replace with inorder successor + if (cur.left != null && cur.right != null) { + stack.push(cur); + + Node succParent = cur; + Node succ = cur.right; + + while (succ.left != null) { + stack.push(succ); + succParent = succ; + succ = succ.left; + } + + cur.data = succ.data; // copy successor value + parent = succParent; + cur = succ; // now delete successor node + } + + // cur has at most one child + Node child = (cur.left != null) ? cur.left : cur.right; + + if (parent == null) { + root = child; + } else if (parent.left == cur) { + parent.left = child; + } else { + parent.right = child; + } + + rebalance(stack); + } + + // -------- REBALANCE -------- + void rebalance(Deque stack) { + while (!stack.isEmpty()) { + Node node = stack.pop(); + updateHeight(node); + + int balance = getBalance(node); + + if (balance > 1) { + if (getBalance(node.left) < 0) { + node.left = leftRotate(node.left); + } + node = rightRotate(node); + } else if (balance < -1) { + if (getBalance(node.right) > 0) { + node.right = rightRotate(node.right); + } + node = leftRotate(node); + } + + if (stack.isEmpty()) { + root = node; + } else { + Node parent = stack.peek(); + if (node.data < parent.data) { + parent.left = node; + } else { + parent.right = node; + } + } + } + } + + // -------- TRAVERSALS -------- + public void inorder() { + inorder(root); + System.out.println(); + } + + void inorder(Node n) { + if (n == null) return; + inorder(n.left); + System.out.print(n.data + " "); + inorder(n.right); + } + + public void levelOrder() { + if (root == null) return; + + Queue q = new ArrayDeque<>(); + q.add(root); + + while (!q.isEmpty()) { + Node n = q.poll(); + System.out.print(n.data + " "); + + if (n.left != null) q.add(n.left); + if (n.right != null) q.add(n.right); + } + System.out.println(); + } + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + AVL tree = new AVL(); + + int ch; + do { + System.out.println("\n--- AVL Tree Operations Menu ---"); + System.out.println("1. Insert"); + System.out.println("2. Delete"); + System.out.println("3. Inorder"); + System.out.println("4. LevelOrder"); + System.out.println("5. Exit"); + System.out.print("Choice: "); + + ch = sc.nextInt(); + + switch (ch) { + case 1 -> { + System.out.print("Enter value to Insert: "); + tree.insert(sc.nextInt()); + } + case 2 -> { + System.out.print("Enter value to Delete: "); + tree.delete(sc.nextInt()); + } + case 3 -> tree.inorder(); + case 4 -> tree.levelOrder(); + case 5 -> System.out.println("Exiting..."); + default -> System.out.println("Invalid choice!"); + } + } while (ch != 5); + + sc.close(); + } +} diff --git a/Data-Structures/Trees/BinarySearchTree.java b/Data-Structures/Trees/BinarySearchTree.java index 1d90720..bbac4a7 100644 --- a/Data-Structures/Trees/BinarySearchTree.java +++ b/Data-Structures/Trees/BinarySearchTree.java @@ -1,6 +1,6 @@ import java.util.*; -public class Main { +public class BinarySearchTree { static class Node { int data; Node left, right; diff --git a/Data-Structures/Trees/BinaryTree.java b/Data-Structures/Trees/BinaryTree.java index bd281ab..c7b9e73 100644 --- a/Data-Structures/Trees/BinaryTree.java +++ b/Data-Structures/Trees/BinaryTree.java @@ -1,59 +1,59 @@ -class Node { - int data; - Node left; - Node right; - - public Node(int key) { - data = key; - left = right = null; - } -} - -class BinaryTree { - Node root = null; - - void inorder_traversal(Node node) { - if (node != null) { - inorder_traversal(node.left); - System.out.print(node.data + " "); - inorder_traversal(node.right); - } - } - - void pre_order_traversal(Node node) { - if (node != null) { - System.out.print(node.data + " "); - pre_order_traversal(node.left); - pre_order_traversal(node.right); - } - } - - void post_order_traversal(Node node) { - if (node != null) { - post_order_traversal(node.left); - post_order_traversal(node.right); - System.out.print(node.data + " "); - } - } -} - -public class Main { - public static void main(String[] args) { - BinaryTree tree = new BinaryTree(); - tree.root = new Node(27); - tree.root.left = new Node(12); - tree.root.right = new Node(3); - tree.root.left.left = new Node(44); - tree.root.left.right = new Node(17); - tree.root.right.left = new Node(56); - - System.out.println("Inorder traversal:"); - tree.inorder_traversal(tree.root); - - System.out.println("\nPreorder traversal:"); - tree.pre_order_traversal(tree.root); - - System.out.println("\nPostorder traversal:"); - tree.post_order_traversal(tree.root); - } -} +class Node { + int data; + Node left; + Node right; + + public Node(int key) { + data = key; + left = right = null; + } +} + +class BinaryTreeStructure { + Node root = null; + + void inorder_traversal(Node node) { + if (node != null) { + inorder_traversal(node.left); + System.out.print(node.data + " "); + inorder_traversal(node.right); + } + } + + void pre_order_traversal(Node node) { + if (node != null) { + System.out.print(node.data + " "); + pre_order_traversal(node.left); + pre_order_traversal(node.right); + } + } + + void post_order_traversal(Node node) { + if (node != null) { + post_order_traversal(node.left); + post_order_traversal(node.right); + System.out.print(node.data + " "); + } + } +} + +public class BinaryTree { + public static void main(String[] args) { + BinaryTreeStructure tree = new BinaryTreeStructure(); + tree.root = new Node(27); + tree.root.left = new Node(12); + tree.root.right = new Node(3); + tree.root.left.left = new Node(44); + tree.root.left.right = new Node(17); + tree.root.right.left = new Node(56); + + System.out.println("Inorder traversal:"); + tree.inorder_traversal(tree.root); + + System.out.println("\nPreorder traversal:"); + tree.pre_order_traversal(tree.root); + + System.out.println("\nPostorder traversal:"); + tree.post_order_traversal(tree.root); + } +} diff --git a/Searching-algorithms/Binarysearch.java b/Searching-algorithms/Binarysearch.java index 86795a9..327eee0 100644 --- a/Searching-algorithms/Binarysearch.java +++ b/Searching-algorithms/Binarysearch.java @@ -1,38 +1,38 @@ -import java.util.Scanner; -import java.util.Arrays; - -public class DS_Binarysearch { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("Enter the length of the Array: "); - int size = sc.nextInt(); - System.out.println("Enter Array elements: "); - int arr[] = new int[size]; - for (int i = 0; i < size; i++) { - arr[i] = sc.nextInt(); - } - System.out.println("Entered Array: " + Arrays.toString(arr)); - System.out.println("Enter the key element to be Found: "); - int key = sc.nextInt(); - int result = binarysearch(arr, key); - if (result != -1) { - System.out.println("Element " + key + " Found at Index: " + result); - } else { - System.out.println("Element " + key + " was NOT Found in the entered Array."); - } - } - public static int binarysearch(int[] arr, int key) { - int low = 0; - int high = arr.length - 1; - while (low <= high) { - int mid = (low + high) / 2; - if (arr[mid] == key) - return mid; - else if (key < arr[mid]) - high = mid - 1; - else - low = mid + 1; - } - return -1; - } -} +import java.util.Scanner; +import java.util.Arrays; + +public class Binarysearch { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("Enter the length of the Array: "); + int size = sc.nextInt(); + System.out.println("Enter Array elements: "); + int arr[] = new int[size]; + for (int i = 0; i < size; i++) { + arr[i] = sc.nextInt(); + } + System.out.println("Entered Array: " + Arrays.toString(arr)); + System.out.println("Enter the key element to be Found: "); + int key = sc.nextInt(); + int result = binarysearch(arr, key); + if (result != -1) { + System.out.println("Element " + key + " Found at Index: " + result); + } else { + System.out.println("Element " + key + " was NOT Found in the entered Array."); + } + } + public static int binarysearch(int[] arr, int key) { + int low = 0; + int high = arr.length - 1; + while (low <= high) { + int mid = (low + high) / 2; + if (arr[mid] == key) + return mid; + else if (key < arr[mid]) + high = mid - 1; + else + low = mid + 1; + } + return -1; + } +} diff --git a/Searching-algorithms/Interpolationsearch.java b/Searching-algorithms/Interpolationsearch.java index 44b738f..41104e6 100644 --- a/Searching-algorithms/Interpolationsearch.java +++ b/Searching-algorithms/Interpolationsearch.java @@ -1,25 +1,25 @@ -import java.util.Scanner; -import java.util.Arrays; - -public class DS_Interpolationsearch { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("Enter the length of the Array: "); - int n = sc.nextInt(); - System.out.println("Enter Array elements: "); - int arr[] = new int[n]; - for (int i = 0; i < n; i++) { - arr[i] = sc.nextInt(); - } - System.out.println("Entered Array: " + Arrays.toString(arr)); - System.out.println("Enter the key element to be Found: "); - int key = sc.nextInt(); - int result = interpolationSearch(arr, 0, arr.length - 1, key); - if (result != -1) { - System.out.println("Element " + key + " Found at Index: " + result); - } else { - System.out.println("Element " + key + " was NOT Found in the entered Array."); - } +import java.util.Scanner; +import java.util.Arrays; + +public class Interpolationsearch { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("Enter the length of the Array: "); + int n = sc.nextInt(); + System.out.println("Enter Array elements: "); + int arr[] = new int[n]; + for (int i = 0; i < n; i++) { + arr[i] = sc.nextInt(); + } + System.out.println("Entered Array: " + Arrays.toString(arr)); + System.out.println("Enter the key element to be Found: "); + int key = sc.nextInt(); + int result = interpolationSearch(arr, 0, arr.length - 1, key); + if (result != -1) { + System.out.println("Element " + key + " Found at Index: " + result); + } else { + System.out.println("Element " + key + " was NOT Found in the entered Array."); + } } public static int interpolationSearch(int arr[], int lo, int hi, int x) { int pos; @@ -34,9 +34,9 @@ public static int interpolationSearch(int arr[], int lo, int hi, int x) { return pos; if (arr[pos] < x) return interpolationSearch(arr, pos + 1, hi, x); - if (arr[pos] > x) - return interpolationSearch(arr, lo, pos - 1, x); - } - return -1; - } -} + if (arr[pos] > x) + return interpolationSearch(arr, lo, pos - 1, x); + } + return -1; + } +} diff --git a/Searching-algorithms/Linearsearch.java b/Searching-algorithms/Linearsearch.java index cca4f3b..48b3846 100644 --- a/Searching-algorithms/Linearsearch.java +++ b/Searching-algorithms/Linearsearch.java @@ -1,32 +1,32 @@ -import java.util.Scanner; -import java.util.Arrays; - -public class DS_linearsearch { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("Enter the length of the Array: "); - int size = sc.nextInt(); - System.out.println("Enter Array elements: "); - int arr[] = new int[size]; - for (int i = 0; i < size; i++) { - arr[i] = sc.nextInt(); - } - System.out.println("Entered Array: " + Arrays.toString(arr)); - System.out.println("Enter the key element to be Found: "); - int key = sc.nextInt(); - int result = linearsearch(arr, key); - if (result != -1) { - System.out.println("Element " + key + " Found at Index: " + result); - } else { - System.out.println("Element " + key + " was NOT Found in the entered Array."); - } - } - public static int linearsearch(int[] arr, int key){ - for (int i = 0; i < arr.length; i++) { - if (arr[i] == key) { - return i; - } - } - return -1; - } -} +import java.util.Scanner; +import java.util.Arrays; + +public class Linearsearch { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("Enter the length of the Array: "); + int size = sc.nextInt(); + System.out.println("Enter Array elements: "); + int arr[] = new int[size]; + for (int i = 0; i < size; i++) { + arr[i] = sc.nextInt(); + } + System.out.println("Entered Array: " + Arrays.toString(arr)); + System.out.println("Enter the key element to be Found: "); + int key = sc.nextInt(); + int result = linearsearch(arr, key); + if (result != -1) { + System.out.println("Element " + key + " Found at Index: " + result); + } else { + System.out.println("Element " + key + " was NOT Found in the entered Array."); + } + } + public static int linearsearch(int[] arr, int key){ + for (int i = 0; i < arr.length; i++) { + if (arr[i] == key) { + return i; + } + } + return -1; + } +} diff --git a/Searching-algorithms/Uniform_binarysearch.java b/Searching-algorithms/Uniform_binarysearch.java index 186b8d5..7b2fbb7 100644 --- a/Searching-algorithms/Uniform_binarysearch.java +++ b/Searching-algorithms/Uniform_binarysearch.java @@ -1,45 +1,45 @@ -import java.util.Scanner; -import java.util.Arrays; - -public class DS_Uniform_binarysearch { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("Enter the length of the Array: "); - int size = sc.nextInt(); - System.out.println("Enter Array elements: "); - int arr[] = new int[size]; - for (int i = 0; i < size; i++) { - arr[i] = sc.nextInt(); - } - System.out.println("Entered Array: " + Arrays.toString(arr)); - System.out.println("Enter the key element to be Found: "); - int key = sc.nextInt(); - int result = binunisearch(arr, key); - if (result != -1) { - System.out.println("Element " + key + " Found at Index: " + result); - } else { - System.out.println("Element " + key + " was NOT Found in the entered Array."); - } - } - public static int binunisearch(int[] arr, int key) { - int n = arr.length; - int k = (int)(Math.log(n) / Math.log(2)); - System.out.println("n = " + n); - System.out.println("k = " + k); - int[] offset = new int[k + 1]; - offset[0] = 1 << k; - for (int i = 1; i <= k; i++) { - offset[i] = offset[i-1] / 2; - } - int index = -1; - for (int i = 0; i <= k; i++) { - int next = index + offset[i]; - if (next < n && arr[next] <= key) { - index = next; - } - } - if (index >= 0 && arr[index] == key) - return index; - return -1; - } -} +import java.util.Scanner; +import java.util.Arrays; + +public class Uniform_binarysearch { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("Enter the length of the Array: "); + int size = sc.nextInt(); + System.out.println("Enter Array elements: "); + int arr[] = new int[size]; + for (int i = 0; i < size; i++) { + arr[i] = sc.nextInt(); + } + System.out.println("Entered Array: " + Arrays.toString(arr)); + System.out.println("Enter the key element to be Found: "); + int key = sc.nextInt(); + int result = binunisearch(arr, key); + if (result != -1) { + System.out.println("Element " + key + " Found at Index: " + result); + } else { + System.out.println("Element " + key + " was NOT Found in the entered Array."); + } + } + public static int binunisearch(int[] arr, int key) { + int n = arr.length; + int k = (int)(Math.log(n) / Math.log(2)); + System.out.println("n = " + n); + System.out.println("k = " + k); + int[] offset = new int[k + 1]; + offset[0] = 1 << k; + for (int i = 1; i <= k; i++) { + offset[i] = offset[i-1] / 2; + } + int index = -1; + for (int i = 0; i <= k; i++) { + int next = index + offset[i]; + if (next < n && arr[next] <= key) { + index = next; + } + } + if (index >= 0 && arr[index] == key) + return index; + return -1; + } +} diff --git a/Sorting-algorithms/Bubblesort.java b/Sorting-algorithms/Bubblesort.java index 64439e1..58a42a3 100644 --- a/Sorting-algorithms/Bubblesort.java +++ b/Sorting-algorithms/Bubblesort.java @@ -1,29 +1,29 @@ -// Bubble Sort : -import java.util.Scanner; -import java.util.Arrays; -public class DS_Bubblesort{ - public static void main(String[] args){ - Scanner sc = new Scanner(System.in); - System.out.println("Enter the array size : "); - int n = sc.nextInt(); - int[] arr = new int[n]; - for(int i = 0; i < n; i ++){ - arr[i] = sc.nextInt(); - } - System.out.println("Entered array : "+ Arrays.toString(arr)); - bubblesort(arr); - System.out.println("Sorted array : "+ Arrays.toString(arr)); - } - public static void bubblesort(int[] arr){ - int n = arr.length; - for(int i = 0; i < n - 1; i++){ - for(int j = 0; j < n - 1 - i; j++){ - if(arr[j] > arr[j + 1]){ - int temp = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = temp; - } - } - } - } -} +// Bubble Sort : +import java.util.Scanner; +import java.util.Arrays; +public class Bubblesort{ + public static void main(String[] args){ + Scanner sc = new Scanner(System.in); + System.out.println("Enter the array size : "); + int n = sc.nextInt(); + int[] arr = new int[n]; + for(int i = 0; i < n; i ++){ + arr[i] = sc.nextInt(); + } + System.out.println("Entered array : "+ Arrays.toString(arr)); + bubblesort(arr); + System.out.println("Sorted array : "+ Arrays.toString(arr)); + } + public static void bubblesort(int[] arr){ + int n = arr.length; + for(int i = 0; i < n - 1; i++){ + for(int j = 0; j < n - 1 - i; j++){ + if(arr[j] > arr[j + 1]){ + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + } +} diff --git a/Sorting-algorithms/Heapsort.java b/Sorting-algorithms/Heapsort.java index 12470ca..6f7d6b4 100644 --- a/Sorting-algorithms/Heapsort.java +++ b/Sorting-algorithms/Heapsort.java @@ -1,45 +1,45 @@ -import java.util.Scanner; -import java.util.Arrays; -public class DS_HeapSort { - public void sort(int arr[]) { - int n = arr.length; - for (int i = n / 2 - 1; i >= 0; i--) { - heapify(arr, n, i); - } - for (int i = n - 1; i >= 0; i--) { - int tmp = arr[0]; - arr[0] = arr[i]; - arr[i] = tmp; - heapify(arr, i, 0); - } - } - void heapify(int arr[], int n, int i) { - int max = i; - int leftChild = 2 * i + 1; - int rightChild = 2 * i + 2; - if (leftChild < n && arr[leftChild] > arr[max]) - max = leftChild; - if (rightChild < n && arr[rightChild] > arr[max]) - max = rightChild; - if (max != i) { - int swap = arr[i]; - arr[i] = arr[max]; - arr[max] = swap; - heapify(arr, n, max); - } - } - public static void main(String args[]) { - Scanner sc = new Scanner(System.in); - System.out.println("Enter the number of elements in the array : "); - int n = sc.nextInt(); - System.out.println("Enter array elements : "); - int[] arr = new int[n]; - for(int i = 0; i = 0; i--) { + heapify(arr, n, i); + } + for (int i = n - 1; i >= 0; i--) { + int tmp = arr[0]; + arr[0] = arr[i]; + arr[i] = tmp; + heapify(arr, i, 0); + } + } + void heapify(int arr[], int n, int i) { + int max = i; + int leftChild = 2 * i + 1; + int rightChild = 2 * i + 2; + if (leftChild < n && arr[leftChild] > arr[max]) + max = leftChild; + if (rightChild < n && arr[rightChild] > arr[max]) + max = rightChild; + if (max != i) { + int swap = arr[i]; + arr[i] = arr[max]; + arr[max] = swap; + heapify(arr, n, max); + } + } + public static void main(String args[]) { + Scanner sc = new Scanner(System.in); + System.out.println("Enter the number of elements in the array : "); + int n = sc.nextInt(); + System.out.println("Enter array elements : "); + int[] arr = new int[n]; + for(int i = 0; i = 0 && arr[j] > key) { - arr[j + 1] = arr[j]; - j--; - } - arr[j + 1] = key; - } - } -} +// Insertion sort : +import java.util.Scanner; +import java.util.Arrays; +public class Insertionsort{ + public static void main(String[] args){ + Scanner sc = new Scanner(System.in); + System.out.println("Enter the array size : "); + int n = sc.nextInt(); + int[] arr = new int[n]; + for(int i = 0; i < n; i ++){ + arr[i] = sc.nextInt(); + } + System.out.println("Entered array : "+ Arrays.toString(arr)); + insertionsort(arr); + System.out.println("Sorted array : "+ Arrays.toString(arr)); + } + public static void insertionsort(int[] arr){ + for (int i = 1; i < arr.length; i++) { + int key = arr[i]; + int j = i - 1; + while (j >= 0 && arr[j] > key) { + arr[j + 1] = arr[j]; + j--; + } + arr[j + 1] = key; + } + } +} diff --git a/Sorting-algorithms/Mergesort.java b/Sorting-algorithms/Mergesort.java index f44e0e5..855d7a6 100644 --- a/Sorting-algorithms/Mergesort.java +++ b/Sorting-algorithms/Mergesort.java @@ -1,57 +1,57 @@ -// Merge Sort : -import java.util.Scanner; -import java.util.Arrays; -public class DS_Mergesort{ - public static void main(String[] args){ - Scanner sc = new Scanner(System.in); - System.out.println("Enter the size of the array : "); - int n = sc.nextInt(); - System.out.println("Enter array elements : "); - int[] arr = new int[n]; - for(int i = 0; i < n; i ++){ - arr[i] = sc.nextInt(); - } - System.out.println("Entered array : "+ Arrays.toString(arr)); - mergeSort(arr, 0, arr.length - 1); - System.out.println("Sorted array : " + Arrays.toString(arr)); - } - public static void mergeSort(int[] arr, int left, int right) { - if (left < right) { - int mid = left + (right - left) / 2; - mergeSort(arr, left, mid); - mergeSort(arr, mid + 1, right); - merge(arr, left, mid, right); - } - } - public static void merge(int[] arr, int left, int mid, int right) { - int n1 = mid - left + 1; - int n2 = right - mid; - int[] L = new int[n1]; - int[] R = new int[n2]; - for (int i = 0; i < n1; i++) - L[i] = arr[left + i]; - for (int j = 0; j < n2; j++) - R[j] = arr[mid + 1 + j]; - int i = 0, j = 0, k = left; - while (i < n1 && j < n2) { - if (L[i] <= R[j]) { - arr[k] = L[i]; - i++; - } else { - arr[k] = R[j]; - j++; - } - k++; - } - while (i < n1) { - arr[k] = L[i]; - i++; - k++; } - - while (j < n2) { - arr[k] = R[j]; - j++; - k++; - } - } -} +// Merge Sort : +import java.util.Scanner; +import java.util.Arrays; +public class Mergesort{ + public static void main(String[] args){ + Scanner sc = new Scanner(System.in); + System.out.println("Enter the size of the array : "); + int n = sc.nextInt(); + System.out.println("Enter array elements : "); + int[] arr = new int[n]; + for(int i = 0; i < n; i ++){ + arr[i] = sc.nextInt(); + } + System.out.println("Entered array : "+ Arrays.toString(arr)); + mergeSort(arr, 0, arr.length - 1); + System.out.println("Sorted array : " + Arrays.toString(arr)); + } + public static void mergeSort(int[] arr, int left, int right) { + if (left < right) { + int mid = left + (right - left) / 2; + mergeSort(arr, left, mid); + mergeSort(arr, mid + 1, right); + merge(arr, left, mid, right); + } + } + public static void merge(int[] arr, int left, int mid, int right) { + int n1 = mid - left + 1; + int n2 = right - mid; + int[] L = new int[n1]; + int[] R = new int[n2]; + for (int i = 0; i < n1; i++) + L[i] = arr[left + i]; + for (int j = 0; j < n2; j++) + R[j] = arr[mid + 1 + j]; + int i = 0, j = 0, k = left; + while (i < n1 && j < n2) { + if (L[i] <= R[j]) { + arr[k] = L[i]; + i++; + } else { + arr[k] = R[j]; + j++; + } + k++; + } + while (i < n1) { + arr[k] = L[i]; + i++; + k++; } + + while (j < n2) { + arr[k] = R[j]; + j++; + k++; + } + } +} diff --git a/Sorting-algorithms/Quicksort_Hoare.java b/Sorting-algorithms/Quicksort_Hoare.java index 028762e..fa8f3e4 100644 --- a/Sorting-algorithms/Quicksort_Hoare.java +++ b/Sorting-algorithms/Quicksort_Hoare.java @@ -1,46 +1,46 @@ -// Quick Sort: -import java.util.Scanner; -import java.util.Arrays; -public class DS_Quicksort{ - public static void main(String[] args){ - Scanner sc = new Scanner(System.in); - System.out.println("Enter the size of the array : "); - int n = sc.nextInt(); - System.out.println("Enter array elements : "); - int[] arr = new int[n]; - for(int i = 0; i < n; i ++){ - arr[i] = sc.nextInt(); - } - System.out.println("Entered array : "+ Arrays.toString(arr)); - quicksortrecursion(arr,0,n-1); - System.out.println("Sorted Array : "+Arrays.toString(arr)); - } - public static int partition(int[] arr, int low,int high){ - int pivot = arr[(low + high)/2]; - while(low <= high){ - while(arr[low] < pivot){ - low++; - } - while(arr[high] > pivot){ - high--; - } - if(low <= high){ - int temp = arr[low]; - arr[low] = arr[high]; - arr[high] = temp; - low++; - high--; - } - } - return low; - } - public static void quicksortrecursion(int[] arr, int low, int high){ - int pi = partition(arr,low,high); - if(low < pi-1){ - quicksortrecursion(arr,low,pi-1); - } - if(high > pi){ - quicksortrecursion(arr,pi,high); - } - } -} +// Quick Sort: +import java.util.Scanner; +import java.util.Arrays; +public class Quicksort_Hoare{ + public static void main(String[] args){ + Scanner sc = new Scanner(System.in); + System.out.println("Enter the size of the array : "); + int n = sc.nextInt(); + System.out.println("Enter array elements : "); + int[] arr = new int[n]; + for(int i = 0; i < n; i ++){ + arr[i] = sc.nextInt(); + } + System.out.println("Entered array : "+ Arrays.toString(arr)); + quicksortrecursion(arr,0,n-1); + System.out.println("Sorted Array : "+Arrays.toString(arr)); + } + public static int partition(int[] arr, int low,int high){ + int pivot = arr[(low + high)/2]; + while(low <= high){ + while(arr[low] < pivot){ + low++; + } + while(arr[high] > pivot){ + high--; + } + if(low <= high){ + int temp = arr[low]; + arr[low] = arr[high]; + arr[high] = temp; + low++; + high--; + } + } + return low; + } + public static void quicksortrecursion(int[] arr, int low, int high){ + int pi = partition(arr,low,high); + if(low < pi-1){ + quicksortrecursion(arr,low,pi-1); + } + if(high > pi){ + quicksortrecursion(arr,pi,high); + } + } +} diff --git a/Sorting-algorithms/Quicksort_Lomuto.java b/Sorting-algorithms/Quicksort_Lomuto.java index 00cccc4..dcfd5fd 100644 --- a/Sorting-algorithms/Quicksort_Lomuto.java +++ b/Sorting-algorithms/Quicksort_Lomuto.java @@ -1,39 +1,39 @@ -import java.util.Scanner; -import java.util.Arrays; -public class QuickSort { - private static int partition(int[] array, int low, int high) { - int pivot = array[high]; - int i = low - 1; - for (int j = low; j < high; j++) { - if (array[j] <= pivot) { - i++; - int temp = array[i]; - array[i] = array[j]; - array[j] = temp; - } - } - int temp = array[i + 1]; - array[i + 1] = array[high]; - array[high] = temp; - return i + 1; - } - private static void quickSort(int[] array, int low, int high) { - if (low < high) { - int pi = partition(array, low, high); - quickSort(array, low, pi - 1); - quickSort(array, pi + 1, high); - } - } - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - System.out.print("Enter the number of elements: "); - int n = scanner.nextInt(); - int[] array = new int[n]; - System.out.println("Enter the elements:"); - for (int i = 0; i < n; i++) { - array[i] = scanner.nextInt(); - } - quickSort(array, 0, n - 1); - System.out.println("Sorted array in ascending order:"+ Arrays.toString(array)); - } -} +import java.util.Scanner; +import java.util.Arrays; +public class Quicksort_Lomuto { + private static int partition(int[] array, int low, int high) { + int pivot = array[high]; + int i = low - 1; + for (int j = low; j < high; j++) { + if (array[j] <= pivot) { + i++; + int temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } + } + int temp = array[i + 1]; + array[i + 1] = array[high]; + array[high] = temp; + return i + 1; + } + private static void quickSort(int[] array, int low, int high) { + if (low < high) { + int pi = partition(array, low, high); + quickSort(array, low, pi - 1); + quickSort(array, pi + 1, high); + } + } + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.print("Enter the number of elements: "); + int n = scanner.nextInt(); + int[] array = new int[n]; + System.out.println("Enter the elements:"); + for (int i = 0; i < n; i++) { + array[i] = scanner.nextInt(); + } + quickSort(array, 0, n - 1); + System.out.println("Sorted array in ascending order:"+ Arrays.toString(array)); + } +} diff --git a/Sorting-algorithms/Radixsort.java b/Sorting-algorithms/Radixsort.java index 8376f5a..5f43b62 100644 --- a/Sorting-algorithms/Radixsort.java +++ b/Sorting-algorithms/Radixsort.java @@ -1,54 +1,54 @@ -//Radix sort: -import java.util.Scanner; -import java.util.Arrays; -public class DS_Radixsort { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("Enter array size : "); - int n = sc.nextInt(); - System.out.println("Enter array elements : "); - int[] arr = new int[n]; - for (int i = 0; i < n; i++) { - arr[i] = sc.nextInt(); - } - System.out.println("Entered array : " + Arrays.toString(arr)); - radixsort(arr); - System.out.println("Sorted array : " + Arrays.toString(arr)); - } - public static void radixsort(int[] arr) { - int max = getMax(arr); - for (int exp = 1; max / exp > 0; exp *= 10) { - countSort(arr, exp); - } - } - public static int getMax(int[] arr) { - int max = arr[0]; - for (int i = 1; i < arr.length; i++) { - if (arr[i] > max) { - max = arr[i]; - } - } - return max; - } - public static void countSort(int[] arr, int exp) { - int n = arr.length; - int[] output = new int[n]; - int[] count = new int[10]; - Arrays.fill(count, 0); - for (int i = 0; i < n; i++) { - int digit = (arr[i] / exp) % 10; - count[digit]++; - } - for (int i = 1; i < 10; i++) { - count[i] += count[i - 1]; - } - for (int i = n - 1; i >= 0; i--) { - int digit = (arr[i] / exp) % 10; - output[count[digit] - 1] = arr[i]; - count[digit]--; - } - for (int i = 0; i < n; i++) { - arr[i] = output[i]; - } - } -} +//Radix sort: +import java.util.Scanner; +import java.util.Arrays; +public class Radixsort { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("Enter array size : "); + int n = sc.nextInt(); + System.out.println("Enter array elements : "); + int[] arr = new int[n]; + for (int i = 0; i < n; i++) { + arr[i] = sc.nextInt(); + } + System.out.println("Entered array : " + Arrays.toString(arr)); + radixsort(arr); + System.out.println("Sorted array : " + Arrays.toString(arr)); + } + public static void radixsort(int[] arr) { + int max = getMax(arr); + for (int exp = 1; max / exp > 0; exp *= 10) { + countSort(arr, exp); + } + } + public static int getMax(int[] arr) { + int max = arr[0]; + for (int i = 1; i < arr.length; i++) { + if (arr[i] > max) { + max = arr[i]; + } + } + return max; + } + public static void countSort(int[] arr, int exp) { + int n = arr.length; + int[] output = new int[n]; + int[] count = new int[10]; + Arrays.fill(count, 0); + for (int i = 0; i < n; i++) { + int digit = (arr[i] / exp) % 10; + count[digit]++; + } + for (int i = 1; i < 10; i++) { + count[i] += count[i - 1]; + } + for (int i = n - 1; i >= 0; i--) { + int digit = (arr[i] / exp) % 10; + output[count[digit] - 1] = arr[i]; + count[digit]--; + } + for (int i = 0; i < n; i++) { + arr[i] = output[i]; + } + } +} diff --git a/Sorting-algorithms/Selectionsort.java b/Sorting-algorithms/Selectionsort.java index 07d563d..0ba2db3 100644 --- a/Sorting-algorithms/Selectionsort.java +++ b/Sorting-algorithms/Selectionsort.java @@ -1,31 +1,31 @@ -// Selection sort: -import java.util.Scanner; -import java.util.Arrays; -public class DS_Selectionsort{ - public static void main(String[] args){ - Scanner sc = new Scanner(System.in); - System.out.println("Enter the array size : "); - int n = sc.nextInt(); - int[] arr = new int[n]; - for(int i = 0; i < n; i ++){ - arr[i] = sc.nextInt(); - } - System.out.println("Entered array : "+ Arrays.toString(arr)); - selectionsort(arr); - System.out.println("Sorted array : "+ Arrays.toString(arr)); - } - public static void selectionsort(int[] arr){ - int n = arr.length; - for(int i = 0; i < n-1;i++){ - int min_index = i; - for(int j = i + 1;j < n;j++){ - if(arr[j] < arr[min_index]){ - min_index = j; - } - } - int temp = arr[i]; - arr[i] = arr[min_index]; - arr[min_index] = temp; - } - } -} +// Selection sort: +import java.util.Scanner; +import java.util.Arrays; +public class Selectionsort{ + public static void main(String[] args){ + Scanner sc = new Scanner(System.in); + System.out.println("Enter the array size : "); + int n = sc.nextInt(); + int[] arr = new int[n]; + for(int i = 0; i < n; i ++){ + arr[i] = sc.nextInt(); + } + System.out.println("Entered array : "+ Arrays.toString(arr)); + selectionsort(arr); + System.out.println("Sorted array : "+ Arrays.toString(arr)); + } + public static void selectionsort(int[] arr){ + int n = arr.length; + for(int i = 0; i < n-1;i++){ + int min_index = i; + for(int j = i + 1;j < n;j++){ + if(arr[j] < arr[min_index]){ + min_index = j; + } + } + int temp = arr[i]; + arr[i] = arr[min_index]; + arr[min_index] = temp; + } + } +} diff --git a/Sorting-algorithms/Shellsort.java b/Sorting-algorithms/Shellsort.java index b03877b..c3408d1 100644 --- a/Sorting-algorithms/Shellsort.java +++ b/Sorting-algorithms/Shellsort.java @@ -1,31 +1,31 @@ -//Shell sort: -import java.util.Scanner; -import java.util.Arrays; -public class DS_shellsort { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("Enter array size : "); - int n = sc.nextInt(); - System.out.println("Enter array elements : "); - int[] arr = new int[n]; - for (int i = 0; i < n; i++) { - arr[i] = sc.nextInt(); - } - System.out.println("Entered array : " + Arrays.toString(arr)); - shellSort(arr); - System.out.println("Sorted array : " + Arrays.toString(arr)); - } - public static void shellSort(int[] arr) { - int n = arr.length; - for (int gap = n / 2; gap > 0; gap /= 2) { - for (int i = gap; i < n; i++) { - int temp = arr[i]; - int j; - for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) { - arr[j] = arr[j - gap]; - } - arr[j] = temp; - } - } - } -} +//Shell sort: +import java.util.Scanner; +import java.util.Arrays; +public class Shellsort { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("Enter array size : "); + int n = sc.nextInt(); + System.out.println("Enter array elements : "); + int[] arr = new int[n]; + for (int i = 0; i < n; i++) { + arr[i] = sc.nextInt(); + } + System.out.println("Entered array : " + Arrays.toString(arr)); + shellSort(arr); + System.out.println("Sorted array : " + Arrays.toString(arr)); + } + public static void shellSort(int[] arr) { + int n = arr.length; + for (int gap = n / 2; gap > 0; gap /= 2) { + for (int i = gap; i < n; i++) { + int temp = arr[i]; + int j; + for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) { + arr[j] = arr[j - gap]; + } + arr[j] = temp; + } + } + } +}