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 :
- Bubble sort
- Selection sort
- Quick sort
- Insertion sort
- Merge sort .
- Radix sort etc.
We , in our future posts will discuss few of them in detail .
Thank you !
Such a detailing!
ReplyDeleteAre you planning to write a textbook.
Very precise.
ReplyDeleteShort and clear.
Very precise.
ReplyDeleteShort and clear.
Your comments are contradicting each other , I am confused !
ReplyDelete