Skip to main content

Posts

Showing posts from January, 2017

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 with Linear

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 Java Program

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