├── BubbleSort.java ├── CircularQueue.java ├── InsertionCode.java ├── LinearBinarySearch.java ├── LinkedListCode.java ├── MergeSort.java ├── QueueCode.java ├── QuickSort.java ├── Recursion.java ├── SelectionSort.java ├── StackCode.java └── TreeCode.java /BubbleSort.java: -------------------------------------------------------------------------------- 1 | 2 | public class BubbleSort { 3 | 4 | public static void main(String[] args) { 5 | 6 | int nums[]= {6,5,2,8,9,4}; 7 | int size = nums.length; 8 | int temp=0; 9 | 10 | System.out.println("Before Sorting"); 11 | for(int num : nums) { 12 | System.out.print(num + " "); 13 | } 14 | 15 | for(int i=0;i nums[j+1]) 20 | { 21 | temp = nums[j]; 22 | nums[j] = nums[j+1]; 23 | nums[j+1] = temp; 24 | } 25 | } 26 | // System.out.println(); 27 | // for(int num : nums) { 28 | // System.out.print(num +" "); 29 | // } 30 | } 31 | 32 | 33 | System.out.println(); 34 | System.out.println("After Sorting"); 35 | for(int num : nums) { 36 | System.out.print(num +" "); 37 | } 38 | 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /CircularQueue.java: -------------------------------------------------------------------------------- 1 | 2 | public class CircularQueue { 3 | 4 | public static void main(String[] args) { 5 | 6 | Queue queue = new Queue(); 7 | 8 | queue.enqueue(10); 9 | queue.enqueue(20); 10 | queue.enqueue(5); 11 | queue.enqueue(99); 12 | 13 | System.out.println(queue.dequeue()); 14 | queue.enqueue(12); 15 | queue.enqueue(32); 16 | 17 | System.out.println("Peek "+queue.peek()); 18 | 19 | System.out.println(queue.dequeue()); 20 | System.out.println(queue.dequeue()); 21 | System.out.println(queue.dequeue()); 22 | System.out.println(queue.dequeue()); 23 | System.out.println(queue.dequeue()); 24 | 25 | // queue.show(); 26 | 27 | } 28 | } 29 | 30 | 31 | class Queue{ 32 | 33 | private int front = 0; 34 | private int rear = -1; 35 | private int size = 0; 36 | private int[] arr = new int[4]; 37 | 38 | public void enqueue(int data) 39 | { 40 | if(!isFull()) 41 | { 42 | rear = (rear + 1) % 4; 43 | arr[rear] = data; 44 | size++; 45 | } 46 | else { 47 | System.out.println("Queue if full"); 48 | } 49 | } 50 | 51 | 52 | public int dequeue() 53 | { 54 | if(isEmpty()) 55 | { 56 | throw new RuntimeException("Queue is Empty"); 57 | } 58 | 59 | int data = arr[front]; 60 | front = (front+ + 1) % 4; 61 | size--; 62 | return data; 63 | } 64 | 65 | 66 | public int peek() 67 | { 68 | if(isEmpty()) 69 | 70 | throw new RuntimeException("Queue is Empty"); 71 | return arr[front]; 72 | } 73 | 74 | 75 | public boolean isFull() 76 | { 77 | return size ==4; 78 | } 79 | 80 | 81 | public boolean isEmpty() 82 | { 83 | return size ==0; 84 | } 85 | 86 | 87 | public void show() 88 | { 89 | for(int i=front; i=0 && arr[j] > key) 14 | { 15 | arr[j+1] = arr[j]; 16 | j--; 17 | } 18 | arr[j+1] = key; 19 | } 20 | 21 | for(int num : arr) 22 | { 23 | System.out.print(num +" "); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LinearBinarySearch.java: -------------------------------------------------------------------------------- 1 | public class LinearBinarySearch { 2 | 3 | public static void main(String[] args) { 4 | 5 | // int nums[]= {1,2,3,5,7,9,10,11,13}; 6 | int nums[]=new int[1000]; 7 | int target= 11; 8 | 9 | int result1 = linearSearch(nums, target); 10 | // int result2 = binarySearch(nums, target); 11 | int result2 = binarySearch(nums, target, 0, nums.length-1); 12 | 13 | if(result1 != -1) 14 | System.out.println("Element found at index: "+ result1); 15 | else 16 | System.out.println("Element not found"); 17 | } 18 | 19 | 20 | public static int linearSearch(int nums[], int target) { 21 | int steps=0; 22 | for(int i=0;i0) 13 | // f1(i-1); 14 | // } 15 | 16 | 17 | int result = fact(10); 18 | System.out.println(result); 19 | } 20 | 21 | public static int fact(int i) 22 | { 23 | if(i!=0) 24 | return i * fact(i-1); 25 | 26 | return 1; 27 | } 28 | 29 | // 5! = 5 * 4 * 3 * 2 * 1 30 | // 5! = 5 * 4! 31 | 32 | } 33 | -------------------------------------------------------------------------------- /SelectionSort.java: -------------------------------------------------------------------------------- 1 | 2 | public class SelectionSort { 3 | 4 | public static void main(String[] args) { 5 | 6 | int nums[]= {6,5,2,8,9,4}; 7 | int size = nums.length; 8 | int temp=0; 9 | int minIndex=-1; 10 | 11 | System.out.println("Before Sorting"); 12 | for(int num : nums) { 13 | System.out.print(num + " "); 14 | } 15 | 16 | for(int i=0; i nums[j]) { 22 | minIndex=j; 23 | } 24 | } 25 | 26 | temp = nums[minIndex]; 27 | nums[minIndex] = nums[i]; 28 | nums[i] = temp; 29 | 30 | System.out.println(); 31 | for(int num : nums) 32 | { 33 | System.out.print(num + " "); 34 | } 35 | } 36 | 37 | 38 | System.out.println(); 39 | System.out.println("After Sorting"); 40 | for(int num : nums) { 41 | System.out.print(num +" "); 42 | } 43 | 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /StackCode.java: -------------------------------------------------------------------------------- 1 | public class StackCode { 2 | 3 | public static void main(String[] args) { 4 | 5 | Stack nums = new Stack(); 6 | 7 | nums.pop(); 8 | 9 | // nums.push(10); 10 | // nums.push(30); 11 | // System.out.println(nums.pop()); 12 | // nums.push(70); 13 | // nums.push(20); 14 | // System.out.println(nums.peek()); 15 | // nums.push(50); 16 | // nums.push(90); 17 | 18 | nums.printStack(); 19 | } 20 | } 21 | 22 | 23 | class Stack{ 24 | 25 | private int[] arr = new int[5]; 26 | int top; 27 | int size; 28 | 29 | public Stack() 30 | { 31 | size = arr.length; 32 | top = -1; 33 | } 34 | 35 | 36 | public void push(int data) 37 | { 38 | // top++; 39 | if(top < size) 40 | arr[++top] = data; 41 | else 42 | System.out.println("Stack Overflow"); 43 | } 44 | 45 | 46 | public int pop() 47 | { 48 | if(top > -1) 49 | return arr[top--]; 50 | else { 51 | System.out.println("Stack Underflow"); 52 | } 53 | return 0; 54 | } 55 | 56 | 57 | public int peek() 58 | { 59 | return arr[top]; 60 | } 61 | 62 | 63 | public void printStack() 64 | { 65 | for(int n: arr) 66 | { 67 | System.out.print(n +" "); 68 | } 69 | System.out.println(); 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /TreeCode.java: -------------------------------------------------------------------------------- 1 | 2 | public class TreeCode { 3 | 4 | public static void main(String[] args) { 5 | 6 | BinaryTree tree = new BinaryTree(); 7 | tree.insert(8); 8 | tree.insert(7); 9 | tree.insert(12); 10 | tree.insert(15); 11 | tree.insert(2); 12 | tree.insert(5); 13 | 14 | // tree.inorder(); 15 | tree.preorder(); 16 | } 17 | 18 | } 19 | 20 | 21 | class Node 22 | { 23 | int data; 24 | Node left; 25 | Node right; 26 | 27 | public Node(int data) { 28 | this.data = data; 29 | } 30 | } 31 | 32 | 33 | class BinaryTree 34 | { 35 | Node root; 36 | 37 | public void insert(int data) 38 | { 39 | root = insertRec(root,data); 40 | // if(root == null) 41 | // root = new Node(data); 42 | // else if(data < root.data) 43 | // root.left.data = data; 44 | } 45 | 46 | 47 | public Node insertRec(Node root, int data) 48 | { 49 | if(root==null) 50 | root = new Node(data); 51 | else if(data < root.data) 52 | root.left = insertRec(root.left, data); 53 | else if(data > root.data) 54 | root.right = insertRec(root.right, data); 55 | 56 | return root; 57 | } 58 | 59 | 60 | public void inorder() 61 | { 62 | inorderRec(root); 63 | } 64 | 65 | 66 | public void inorderRec(Node root) 67 | { 68 | if(root != null) 69 | { 70 | inorderRec(root.left); 71 | System.out.print(root.data + " "); 72 | inorderRec(root.right); 73 | 74 | } 75 | } 76 | 77 | 78 | public void preorder() 79 | { 80 | preorderRec(root); 81 | } 82 | 83 | 84 | public void preorderRec(Node root) 85 | { 86 | if(root != null) 87 | { 88 | System.out.print(root.data + " "); 89 | inorderRec(root.left); 90 | inorderRec(root.right); 91 | 92 | } 93 | } 94 | 95 | } 96 | --------------------------------------------------------------------------------