Sorting in Data Structure
Sorting in Data Structure
The arrangement of data in a preferred order is called sorting in the data structure. By sorting data,
it is easier to search through it quickly and easily. The simplest example of sorting is a dictionary. Before
the era of the Internet, when you wanted to look up a word in a dictionary, you would do so in alphabetical
order. This made it easy.
Imagine the panic if you had to go through a big book with all the English words from the world in a
jumbled order! It is the same panic an engineer will go through if their data is not sorted and structured. So,
in short, sorting makes our lives easier.
In this lesson you will take through the different data structures & sorting algorithms. But first, let’s
understand what a sorting algorithm is.
A sorting algorithm is just a series of orders or instructions. In this, an array is an input, on which
the sorting algorithm performs operations to give out a sorted array.
Here’s an example of what sorting does.
Let’s suppose you have an array of strings: [h,j,k,i,n,m,o,l] [2, 5, 1, 20, 10]
Now, sorting would yield an output array in alphabetical order.
Output: [h,i,j,k,l,m,n,o] [1, 2, 5, 10, 20]
Sorting Categories
There are two different categories in sorting:
Internal sorting: If the input data is such that it can be adjusted in the main memory at once, it is
called internal sorting.
External sorting: If the input data is such that it cannot be adjusted in the memory entirely at once,
it needs to be stored in a hard disk, or any other storage device. This is called external sorting.
MergeSort(arr[], l, r)
If r > l
1. Divide the array into two equal halves by locating the middle point:
middle m = (l+r)/2
2. Use the mergeSort function to call for the first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for the second half:
Call mergeSort(arr, m+1, r)
4. Use the merge () function to merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)
Check out the image below to get a clear picture of how this works.
2. Selection Sort
In this, at first, the smallest element is sent to the first position. Then, the next smallest element is
searched in the remaining array and is placed at the second position. This goes on until the algorithm
reaches the final element and places it in the right position.
Example:
Input: 637124
First pass
637124 -> 367124 : Bubble sort compares 6 and 3 and swaps them because 3<6.
367124 -> 367124 : Since 6<7, no swapping
367124 -> 361724 : Swapped 7and 1, as 7>1
361724 -> 361274 : Swapped 2 and 7, as 2<7
361274 -> 361247 : Swapped 4 and 7, as 4<7
Second pass
361247 -> 361247
361274 -> 316274 Swapped 6 and 1 as 6>1
316274 -> 312674
312674 -> 312674
312674 -> 312647
Third pass
312647 -> 132647
132647 -> 123647
123647 -> 123467
123467 -> 123467
As you can see, we get the ascending order result after three passes.
Conclusion
That wraps up sorting in data structure and the most common sorting algorithms. You can choose
any of the different types of sorting algorithms. However, remember that some of these can be a little
tedious to write the program for. But then, they might come in handy for quick results. On the other hand, if
you want to sort large datasets, you must choose the bubble sort. Not only does it yield accurate results,
but is also easy to implement. Then again, it is slower than the other types.