Thursday 30 June 2016

Sorting : Arranging the data

Formally ,
Sorting is arranging the elements of data/list/arrays in increasing or decreasing order of desired property .
In general maximum sorting orders are numerical or lexicographical .

Now , the question arise : Why sorting ?

Consider an array of data having 2^100 elements in it .
Now if we need to pick a particular element from it , we need to perform a search operation . If data is sorted , we'll perform binary search and if data is not sorted we'll perform linear search .
Lets check the difference between binary search and linear search !

1. Linear search 

A linear search is a simple searching algorithm. It searches the desired element by comparing the each element with the given element in the sequence . So , in the worst case , it will make n number of comparisons . Particularly for this example , it will perform 2^100 comparisons. And if one comparison is done in microsecond , still it will take years to complete the process .

2. Binary Search

Unlike linear search binary search can be performed on sorted data only . It follows the logic as follows :
  • It compares the middle element first 
  • If the required element is greater or smaller than that it skips the other half 
  • In every step it checks with this "middle element" logic.
So , number of searches reduces to log 2 n for total n elements !

  • So , for the required case it will make log 2 n = log 2 2^100 = 100 comparisons only!
So , for same case , i.e. microsecond for comparison the whole procedure will be completed in less than a millisecond !
So , clearly we can conclude that sorting make this much difference .

Now , the main question : How to sort ?
There are many sorting algorithms like :
We , in our future posts will discuss few of them in detail .


Thank you !

4 comments :

  1. Such a detailing!
    Are you planning to write a textbook.

    ReplyDelete
  2. Very precise.
    Short and clear.

    ReplyDelete
  3. Very precise.
    Short and clear.

    ReplyDelete
  4. Your comments are contradicting each other , I am confused !

    ReplyDelete