JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label Data-Structure. Show all posts
Showing posts with label Data-Structure. Show all posts

Implement stack using Linked List in java data structure and algorithm in data structure and algorithm

1:35 AM
Implement stack using Linked List in java data structure and algorithm in data structure and algorithm The stack is an abstract data type that demonstrates Last in first out (LIFO) behavior. We will implement the same behavior using Linked List. There are two most important operations of Stack: Push: We will push elements to the beginning of the linked list to demonstrate the push behavior of the stack. Pop: We will remove the first element of linked list to demonstrate the pop behavior of Stack. Java Program: Let's create a java program to create a stack using Linked List. package org.arpit.java2blog; public class LinkedListStack { private Node head; // the first node // nest class to define linkedlist node private class Node { int value; Node next; } public LinkedListStack() { head = null; } // Remove value from the beginning of the list for demonstrating behaviour of stack public int pop() throws LinkedListEmptyException { if (head

Stack implementation in java data structure and algorithm

1:16 AM
Stack is abstract data type which demonstrates Last in first out (LIFO) behavior. We will implement same behavior using Array. Although java provides implementation for all abstract data types such as Stack,Queue and LinkedList but it is always good idea to understand basic data structures and implement them yourself. Java Program to implement Stack using Array: package org.arpit.java2blog; /**  * @author java Gian  */ public class StackCustom { int size; int arr[]; int top; StackCustom(int size) { this.size = size; this.arr = new int[size]; this.top = -1; } public void push(int pushedElement) { if (!isFull()) { top++; arr[top] = pushedElement; System.out.println("Pushed element:" + pushedElement); } else { System.out.println("Stack is full !"); } } public int pop() { if (!isEmpty()) { int returnedTop = top; top--; System.out.println("Popped element :" + arr[returnedTop]);

what is Tim-sort?

11:27 PM
Tim-sort Tim-sort is a sorting algorithm derived from insertion sort and merge sort. It was designed to perform in an optimal way on different kind of real world data. It is an adaptive sorting algorithm which needs O(n log n) comparisons to sort an array of n elements. It was designed and implemented by Tim Peters in 2002 in python programming language. It has been python's standard sorting algorithm since version 2.3. Technique Consider an array of n elements which needs to be sorted. In Tim sort, the array is divided into several parts where each of them is called a Run. These Runs are sorted by using insertion sort one by one and then the sorted runs get combined using a combine function. The idea of Tim sort is originated from the fact that, insertion sort works more optimally on the short lists rather than working on the larger lists. Complexity Complexity Best Case Average Case Worst Case Time Complexity O(n) O(n log n) O(n log n) Space Complexity n Steps :

what is Cycle Sort?

11:25 PM
Cycle Sort Cycle sort is a comparison sorting algorithm which forces array to be factored into the number of cycles where each of them can be rotated to produce a sorted array. It is theoretically optimal in the sense that it reduces the number of writes to the original array. Algorithm Consider an array of n distinct elements. An element a is given, index of a can be calculated by counting the number of elements that are smaller than a. if the element is found to be at its correct position, simply leave it as it is. Otherwise, find the correct position of  a  by counting the total number of elements that are less than  a . where it must be present in the sorted array. The other element b which is replaced is to be moved to its correct position. This process continues until we got an element at the original position of  a . The illustrated process constitutes a cycle. Repeating this cycle for each element of the list. The resulting list will be sorted. C program

.