Java generic stack implementation

Examples package com.w3schools;   public class Test<T extends Object> { private int stackSize; private T[] stackArray; private int top;   /** * constructor to create stack with size * @param size */ public Test(int size) { this.stackSize = size; this.stackArray = (T[]) new Object[stackSize]; this.top = -1; }   /** * Adds new entry to … Read more

Java dynamic stack implementation

Dynamic Stack As we discussed in previous example that a stack throws an exception if enough space is not available to accept an entity to be pushed. To overcome this situation we can create dynamic stack whose capacity will keep increases as it reaches to max capacity. Examples package com.w3schools;   public class Test { … Read more

Java stack implementation

Examples package com.w3schools;   public class Test { private int stackSize; private int[] stackArr; private int top;   /** * constructor to create stack with size * @param size */ public Test(int size) { this.stackSize = size; this.stackArr = new int[stackSize]; this.top = -1; }   /** * Adds new entry to the top of … Read more

Java Stack tutorial

A stack is an ADT – Abstract Data Type or a linear data structure. It is a LIFO data structure because it allows all data operations at one end only i.e. elements can be added and removed from the stack only at the top. LIFO stands for Last-in-first-out. The element which is inserted last, is … Read more

Java merge sort algorithm example

Sorting algorithm A sorting algorithm is a way to put elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (like search and merge algorithms) which require input data to be in sorted lists. It is also … Read more

Java quick sort algorithm example

Quicksort sort Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays. Quicksort steps: Pick an element, called a pivot, from the array. Partitioning: reorder the array so that all elements with values less … Read more

Java insertion sort algorithm example

Insertion sort Insertion sort is a simple sorting algorithm that builds the final sorted array or list one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. It iterates, take one input element each repetition, and growing a sorted output list. … Read more

Java selection sort algorithm example

Selection sort The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and … Read more

Java bubble sort algorithm example

Bubble sort Bubble sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, … Read more

java sorting algorithms tutorial

Sorting algorithm A sorting algorithm is a way to put elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (like search and merge algorithms) which require input data to be in sorted lists. It is also … Read more