Skip to main content

Posts

Insertion Sort

Insertion sort is a very simple sorting algorithm. Insertion sort is an in-place and comparison based algorithm in which an element is compared with its previous adjacent element and sorted. In this algorithm after each iteration we get a partially sorted algorithm. Insertion sort works well with small datasets but its efficiency decreases when the size of the dataset increases. It is less efficient that quick sort, merge sort or heap sort but is better that selection and bubble sort. It has a worst case time complexity of o( n 2 ), hence is less efficient for large datasets. To know more about insertion sort and its uses and advantages click Here . C Program C++ Program Java program Python Program C Program C++ Program Java Program Python Program Sample input and output to check the program You might also be interested in  Bubble sort Algorithm Selection Sort Algorithm Hashing with Quadratic Probing Hashing wi...

Selection Sort

Selection sort is one of the most simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list or the array used is divided into two parts, the left part is the sorted part of the list and the right part consists of unsorted element of the array. Initially, the sorted part is empty and the unsorted part is the entire list. In Selection sort in each iteration the smallest element in the unsorted part of the array is brought to the to its correct position.Selection almost always outperforms bubble sort and gnome sort.  This algorithm is not suitable for large data sets as its average and worst case complexities are of Ο(n 2 ), where  n  is the number of items. See the following example for better understanding. To know more about selection sort click Here . C Program C++ Program Java program Python Program C Program C++ Program Ja...

Hashing with Quadratic Probing

Hashing is a technique used for storing , searching and removing elements in almost constant time. Hashing is done with help of a hash function that generates index for a given input, then this index can be used to search the elements, store an element, or remove that element from that index. A hash function is a function that is used to map the data elements to their position in the data structure used. For example if we use an array to store the integer elements then the hash function will generate position for each element so that searching, storing and removing operation on the array can be done in constant time that is independent of the number of elements in the array. For better look at the example below. now we face a problem if for 2 numbers same position is generated example consider elements 1 and 14 1 % 13 = 1 14 % 13 = 1 so when we get 1 we store it at the first position, but when we get 14 we see that the position 1 is already taken, this is a case of colli...

Hashing with Linear Probing

Hashing is a technique used for storing , searching and removing elements in almost constant time. Hashing is done with help of a hash function that generates index for a given input, then this index can be used to search the elements, store an element, or remove that element from that index. A hash function is a function that is used to map the data elements to their position in the data structure used. For example if we use an array to store the integer elements then the hash function will generate position for each element so that searching, storing and removing operation on the array can be done in constant time that is independent of the number of elements in the array. For better look at the example below. now we face a problem if for 2 numbers same position is generated example consider elements 1 and 14 1 % 13 = 1 14 % 13 = 1 so when we get 1 we store it at the first position, but when we get 14 we see that the position 1 is already taken, this is a case of colli...

Trailing zeros in factorial of a number

Trailing zeros is a sequence of 0s in a number. In order to find trailing zeros in factorial of a number we divide the number by 5 and its powers till 5 power k is smaller than the number. To know more about trailing zeros and its uses click Here Program : C++ Program Java program Python Program C++ Program Java Program Python Program Sample input and output to check the program You might also be interested in Check if a number is a Fibonacci number or not Find Factorial of a number Find Square root using Babylonian method Bubble sort Algorithm Stack implementation using linked list Queue implementation using linked list

Bubble sort Algorithm

Bubble sort is the most simple sorting technique used to sort arrays , linked list etc. It is an algorithm that keeps traversing the list until the list is completly sorted.  Bubble sort has been occasionally referred to as a "sinking sort". The idea being that the larger elements are heavier hence they sink to the bottom of the list and smaller elements being light bubble to the top of the list. Algorithm In bubble we keep comparing 2 adjacent elements and swap their places is they are not in correct order. Performance Bubble sort being a simple algorithm works fine when the size of the data is small but its performance decreases as the size of the data increases, hence Bubble is used only when number of elements is small or only some elements are out of order. To know more about bubble sort, its performance, complexity and comparisons to other sorting methods click Here .    C Program C++ Program Java program Python Program C Program ...

Stack implementation using Linked List

Stack is a data structure it serves as a collection of elements. It has 2 principle operation push and pop.Stack works on the principle of  Last In First Out or LIFO i.e the element that is entered last is removed first. To do this is has 2 methods  push () :  push operation pushes the element on the top. pop () :  pop operation removes the element on the top. This can be seen in  the below image. Linked list  is a simple linear data structure formed by collection of data elements called nodes. Each node consists of a data element and link field.The data field consists of element and link field consists of address of next element. To know more about Stack Data Structure click  here To know more about Linked List click  here To view Simple Linked List implementation click  here      Now inorder to implement Stack using linked list we need to implement push and pop operation in Linked List. Push and...