0% found this document useful (0 votes)
134 views

Sorting in Data Structure

Sorting arranges data in a preferred order, making it easier to search. There are two sorting categories: internal sorting handles data that fits in memory, while external sorting handles data too large for memory. Common sorting algorithms include merge sort, selection sort, and bubble sort. Merge sort splits data into halves, sorts them, and merges the results. Selection sort finds the smallest element and places it in position 1, then finds the next smallest for position 2, and so on. Bubble sort repeatedly swaps adjacent elements that are out of order until the list is fully sorted.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views

Sorting in Data Structure

Sorting arranges data in a preferred order, making it easier to search. There are two sorting categories: internal sorting handles data that fits in memory, while external sorting handles data too large for memory. Common sorting algorithms include merge sort, selection sort, and bubble sort. Merge sort splits data into halves, sorts them, and merges the results. Selection sort finds the smallest element and places it in position 1, then finds the next smallest for position 2, and so on. Bubble sort repeatedly swaps adjacent elements that are out of order until the list is fully sorted.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Sorting in Data Structure: Categories & Types

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.

What is a Sorting Algorithm?


Sorting Categories
Types of Sorting in Data Structure
1. Merge Sort
2. Selection Sort
3. Bubble Sort

What is a Sorting Algorithm?

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.

Types of Sorting in Data Structure


Here are a few of the most common types of sorting algorithms.
1. Merge Sort
This algorithm works on splitting an array into two halves of comparable sizes. Each half is then
sorted and merged back together by using the merge () function.

Here’s how the algorithm works:

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.

Look at the picture below to understand it better.


3. Bubble Sort
It is the easiest and simplest of all the sorting algorithms. It works on the principle of repeatedly
swapping adjacent elements in case they are not in the right order.
In simpler terms, if the input is to be sorted in ascending order, the bubble sort will first compare
the first two elements in the array. In case the second one is smaller than the first, it will swap the two, and
move on to the next element, and so on.

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.

You might also like