# Algorithms

## Java Program To validate the Bracket Expression

This program checks for the valid bracket string expression and returns true/false. Basically we need to use stack effectively to check the valid brackets.   Algorithm/Idea is: Keep the list of allowed brackets (predefined) in some collections, here we are using the map<character, character> to keep the open and closed brackets of different types. Iterate the string expression character by character and check whether the character is of only allowed brackets (either open / close) If the bracket is open type then just add it to the stack using stack.push(). If the bracket is close type then it just performs stack.pop(). If any other character is encountered in between just perform the iterations for the next available brackets/till the end accordingly return ...

## Selection Sort Implementation in Java Example

Selection Sort Implementation in Java Example:   Psudeocode for selection sort: Take an array with N elements (say new long[]{3,3009, 76,172,271,2}) Outer for loop to iterate all the elements in the array. (index starts from 0) Create a currentMinimum variable with index value(initially 0) Create a nested for loop to iterate from innerIndex(index+1) (because till index it is already sorted, initially it is 0, means not sorted) 2.2.1 if array[currentMinimum] > array[innerIndex] then assign currentMinimum = innerIndex; 2.3. Outside of nested for loop, Perform swapping, here with temp variable for easiness, Create a temp variable and assign the value of array[currentMinimum] Now Move the array[innerIndex] to array[currentMinimum] And assign a...

## Binary Search Implementation in Java Example

Binary search is one of the most important searching algorithm to know and understand for search functionality. In the below we are going to return the array position based on the searching number existence in the array, if it does not exist then we will return -1.   Binary search basically works this way, 1. Numbers should be sorted in the array before beginning with binary search. 2. Check the middle number with the number which need to be searched and finds whether the number matches or greater or lesser. 3. Based on this, again it takes only the particular side (from middle right or left based on point(2) and performs the same steps again until the number matches, if not matched then it returns -1).   Where as linear search works directly from beginning all t...

## Singly Linked List Algorithm Implementation Java Example

Linked list is nothing but having the link of the next node with the current node, this gives lot of advantages like directly doing any manipulation on particular node instead of iterating everything for all the operations like we do in arraylist.   Why we need Linked List ?   Array does not help us for these things, so forced to go for linked list.   1. No dynamic size: All the time we can't mention the correct required size, as well as can't add more size and waste the memory as well, where as in linked lists it can be dynamically managed.   Java Stack Implementation Simple Example   2. Remove performance inefficiency: Just to move one value to left/right also needs one more array and this existing array need to be completely iterated ...

## Reversing a string using Stack Implementation

In our previous post, we saw how to create a custom stack implementation in java. Today we are going to extend the same implementation to perform additional operations like reversing, checking empty or full etc. Approach 1: [Simple & Best Approach]   We have used the same Stack class from this post [Java Stack Implementation Simple Example] with small modification like array type changed from int to char.   Modified Stack Class: [Stack.java] [java] package in.javadomain; public class Stack { char item[]; int lastIndex; int maxArraySize; public Stack(int arraySize) { item = new char[arraySize]; maxArraySize = arraySize; lastIndex--; } public char popOut() { int temp = lastIndex; // after the value returned need to decrement, but after return we

## Java Stack Implementation Simple Example

Recently I started refreshing the backbone of CS, data structure and algorithms. As part of that I wrote a core stack implementation functionalities like Push and Pop in java. I also wrote isEmpty() method just to make use in loops and stuffs like that.   I want you to try writing the stack(LIFO - Last In First Out behaviour) implementation program yourself with these key points, Create a class named Stack Have an array to push and pop (store and retrieve). Have a index variable to keep track on the position. Create pushIn, PopOut and isEmpty methods. If you need, you can set the array size dynamically from constructor. Then from your another main program you can call the constructor with the size or only constructor as per your conclusion on point 5 above. ...