Browsed byCategory: Algorithms

Java Program To validate the Bracket Expression

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…

537 total views, no views today

Selection Sort Implementation in Java Example

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…

1,973 total views, 4 views today

Binary Search Implementation in Java Example

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…

1,978 total views, 6 views today

Singly Linked List Algorithm Implementation Java Example

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,…

1,188 total views, 2 views today

Reversing a string using Stack Implementation

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 </a> with small modification like array type changed from int to char.   <h4><span style="text-decoration: underline;">Modified Stack Class: [Stack.java]</span></h4> [java] package in.javadomain; public class Stack { char item[]; int lastIndex;…

1,045 total views, no views today

Java Stack Implementation Simple Example

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…

1,007 total views, 1 views today