Tuesday, April 4, 2017

Sorting Algorithms

Hi Guys,

This blog is about the various algorithm used to sort a collection .
Let's have a look into each of these available algorithms.

1. Selection Sort
This is the easiest logic to sort a group of objects .You need to pick the smallest element and swap it with the element at first position. Now you have the first element sorted . Just repeat the same step for the remaining elements and your objects will be sorted . Easy.

2.Bubble Sort
Logic  In an array of N number , start with comparing the first two numbers . Move the lowest element up. Now take the next two numbers , compare and move the lowest of them up. Similarly proceed till the last element . Iteration one is over and we have the largest element at the bottom. Now, make N-1 iterations each time reducing the array by excluding the last element.

3.Insertion Sort
Logic  In an array of N numbers take first two numbers and sort it. This will be the sorted subset . Now, take the next number and compare it with the sorted subset and position it so that..

4.Heap Sort
Logic : Take the array of N numbers and arrange it in form of Complete Binary Tree .After that take the first element and swap it with the element at last index. Form completely Binary tree with the elements excluding the one at the first index. Continue this process until all elements are sorted .

ec2-user@ec2 Permission denied