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

Sorting Algorithm

Uploaded by

devanidarshan333
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Sorting Algorithm

Uploaded by

devanidarshan333
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Data Structure and Algorithms

Topperworld.in

Sorting Algorithm

Sorting refers to rearrangement of a given array or list of elements


according to a comparison operator on the elements. The comparison
operator is used to decide the new order of elements in the respective
data structure.
When we have a large amount of data, it can be difficult to deal with it,
especially when it is arranged randomly. When this happens, sorting that
data becomes crucial. It is necessary to sort data in order to make
searching easier.

Types of Sorting Techniques

There are various sorting algorithms are used in data structures. The
following two types of sorting algorithms can be broadly classified:
1. Comparison-based: We compare the elements in a
comparison-based sorting algorithm)
2. Non-comparison-based: We do not compare the elements in
a non-comparison-based sorting algorithm)

Sorting algorithm
Data Structure and Algorithms

Why Sorting Algorithms are Important

The sorting algorithm is important in Computer Science because it reduces


the complexity of a problem. There is a wide range of applications for
these algorithms, including searching algorithms, database algorithms,
divide and conquer methods, and data structure algorithms.
In the following sections, we list some important scientific applications
where sorting algorithms are used
•When you have hundreds of datasets you want to print, you
might want to arrange them in some way.
• Sorting algorithm is used to arrange the elements of a list in a
certain order (either ascending or descending).
• Searching any element in a huge data set becomes easy. We can
use Binary search method for search if we have sorted data. So,
Sorting become important here.
• They can be used in software and in conceptual problems to solve
more advanced problems.
Some of the most common sorting algorithms are:

Below are some of the most common sorting algorithms:

1. Selection Sort

Selection sort is another sorting technique in which we find the minimum


element in every iteration and place it in the array beginning from the first
index. Thus, a selection sort also gets divided into a sorted and unsorted
subarray.
Advantages and Disadvantages of Selection Sort
Advantages Disadvantages
The main advantage of the The primary disadvantage of the
selection sort is that it performs selection sort is its poor efficiency
well on a small list. when dealing with a huge list of
items.
Because it is an in-place sorting The selection sort requires n-
algorithm, no additional temporary squared number of steps for
storage is required beyond what is sorting n elements.
needed to hold the original list.
Its performance is easily influenced Quick Sort is much more efficient
by the initial ordering of the items than selection sort
before the sorting process.
Data Structure and Algorithms

Working of Selection Sort algorithm:

Lets consider the following array as an example: arr[] = {64, 25, 12, 22,
11}
First pass:
For the first position in the sorted array, the whole array is traversed
from index 0 to 4 sequentially. The first position where 64 is stored
presently, after traversing whole array it is clear that 11 is the lowest
value.

64 25 12 22 11

Thus, replace 64 with 11. After one iteration 11, which happens to be
the least value in the array, tends to appear in the first position of the
sorted list.

11 25 12 22 64

Second Pass:
For the second position, where 25 is present, again traverse the rest of
the array in a sequential manner.

11 25 12 22 64

After traversing, we found that 12 is the second lowest value in the


array and it should appear at the second place in the array, thus swap
these values.

11 12 25 22 64

Third Pass:
Now, for third place, where 25 is present again traverse the rest of the
array and find the third least value present in the array.

11 12 25 22 64

While traversing, 22 came out to be the third least value and it should
appear at the third place in the array, thus swap 22 with element
present at third position.

11 12 22 25 64

Fourth pass:
Data Structure and Algorithms

Similarly, for fourth position traverse the rest of the array and find the
fourth least element in the array
As 25 is the 4th lowest value hence, it will place at the fourth position.

11 12 22 25 64

Fifth Pass:
At last the largest value present in the array automatically get placed at
the last position in the array
The resulted array is the sorted array.

11 12 22 25 64

2. Bubble Sort

Bubble Sort is the simplest sorting algorithm that works by repeatedly


swapping the adjacent elements if they are in the wrong order. This
algorithm is not suitable for large data sets as its average and worst-case
time complexity is quite high.

Advantages and Disadvantages of Bubble Sort


Advantages Disadvantages
The primary advantage of the The main disadvantage of the
bubble sort is that it is popular and bubble sort is the fact that it does
easy to implement. not deal well with a list containing
a huge number of items.
In the bubble sort, elements are The bubble sort requires n-squared
swapped in place without using processing steps for every n
additional temporary storage. number of elements to be sorted.
The space requirement is at a The bubble sort is mostly suitable
minimum for academic teaching but not for
real-life applications.

Working of Bubble Sort algorithm:

Lets consider the following array as an example: arr[] = {5, 1, 4, 2, 8}


First Pass:

Bubble sort starts with very first two elements, comparing them to check
which one is greater.
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two
Data Structure and Algorithms

elements, and swaps since 5 > 1.


( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in
order (8 > 5), algorithm does not swap them.

Second Pass:

Now, during second iteration it should look like this:


( 1 4 2 5 8 ) –> (14258)
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> (12458)
( 1 2 4 5 8 ) –> (12458)

Third Pass:

Now, the array is already sorted, but our algorithm does not know if it is
completed.
The algorithm needs one whole pass without any swap to know it is
sorted.
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

Illustration:

Illustration of Bubble Sort


Data Structure and Algorithms

3. Insertion Sort

Insertion sort is a simple sorting algorithm that works similarly to the


way you sort playing cards in your hands. The array is virtually split into
a sorted and an unsorted part. Values from the unsorted part are picked
and placed at the correct position in the sorted part.

Advantages and Disadvantages of Insertion Sort


Advantages Disadvantages

The main advantage of the The disadvantage of the insertion


insertion sort is its simplicity. sort is that it does not perform as
well as other, better sorting
algorithms
It also exhibits a good performance With n-squared steps required for
when dealing with a small list. every n element to be sorted, the
insertion sort does not deal well
with a huge list.
The insertion sort is an in-place The insertion sort is particularly
sorting algorithm so the space useful only when sorting a list of
requirement is minimal. few items.

Working of Insertion Sort algorithm:

Consider an example: arr[]: {12, 11, 13, 5, 6}

12 11 13 5 6

First Pass:

• Initially, the first two elements of the array are compared in


insertion sort.

12 11 13 5 6

• Here, 12 is greater than 11 hence they are not in the ascending


order and 12 is not at its correct position. Thus, swap 11 and 12.
• So, for now 11 is stored in a sorted sub-array.

11 12 13 5 6
Data Structure and Algorithms

Second Pass:

• Now, move to the next two elements and compare them

11 12 13 5 6

• Here, 13 is greater than 12, thus both elements seems to be in


ascending order, hence, no swapping will occur. 12 also stored in
a sorted sub-array along with 11

Third Pass:

• Now, two elements are present in the sorted sub-array which


are 11 and 12
• Moving forward to the next two elements which are 13 and 5

11 12 13 5 6

• Both 5 and 13 are not present at their correct place so swap them

11 12 5 13 6

• After swapping, elements 12 and 5 are not sorted, thus swap


again

11 5 12 13 6

• Here, again 11 and 5 are not sorted, hence swap again

5 11 12 13 6

• here, it is at its correct position

Fourth Pass:

• Now, the elements which are present in the sorted sub-array


are 5, 11 and 12
• Moving to the next two elements 13 and 6

5 11 12 13 6

• Clearly, they are not sorted, thus perform swap between both

5 11 12 6 13

• Now, 6 is smaller than 12, hence, swap again


Data Structure and Algorithms

5 11 6 12 13

• Here, also swapping makes 11 and 6 unsorted hence, swap again

5 6 11 12 13

• Finally, the array is completely sorted.

Illustrations:

4. Merge Sort

The Merge Sort algorithm is a sorting algorithm that is based on


the Divide and Conquers paradigm. In this algorithm, the array is initially
divided into two equal halves and then they are combined in a sorted
manner.

Let’s see how Merge Sort uses Divide and Conquer:

The merge sort algorithm is an implementation of the divide and conquers


technique. Thus, it gets completed in three steps:
1. Divide: In this step, the array/list divides itself recursively into
sub-arrays until the base case is reached.
2. Conquer: Here, the sub-arrays are sorted using recursion.
Data Structure and Algorithms

3. Combine: This step makes use of the merge( ) function to


combine the sub-arrays into the final sorted array.

Advantages and Disadvantages of Merge Sort

Advantages Disadvantages
It can be applied to files of any Requires extra space »N
size.
Reading of the input during the Merge Sort requires more space
run-creation step is sequential than other sort.
==> Not much seeking.
If heap sort is used for the in- Merge sort is less efficient than
memory part of the merge, its other sort
operation can be overlapped with
I/O

Working of Merge Sort algorithm:

To know the functioning of merge sort, lets consider an array arr[] = {38,
27, 43, 3, 9, 82, 10}
At first, check if the left index of array is less than the right index, if yes
then calculate its mid point
Data Structure and Algorithms

Now, as we already know that merge sort first divides the whole array
iteratively into equal halves, unless the atomic values are achieved.
Here, we see that an array of 7 items is divided into two arrays of
size 4 and 3 respectively.

Now, again find that is left index is less than the right index for both
arrays, if found yes, then again calculate mid points for both the arrays.

Now, further divide these two arrays into further halves, until the atomic
units of the array is reached and further division is not possible.
Data Structure and Algorithms

After dividing the array into smallest units, start merging the elements
again based on comparison of size of elements Firstly, compare the
element for each list and then combine them into another list in a sorted
manner.

After the final merging, the list looks like this:


Data Structure and Algorithms

5 Quick Sort

Quicksort is a sorting algorithm based on the divide and conquer approach


where an array is divided into subarrays by selecting a pivot element
(element selected from the array).
1. While dividing the array, the pivot element should be positioned
in such a way that elements less than the pivot are kept on the
left side, and elements greater than the pivot is on the right side
of the pivot.
2. The left and right subarrays are also divided using the same
approach. This process continues until each subarray contains a
single element.
3. At this point, elements are already sorted. Finally, elements are
combined to form a sorted array.

Advantages and Disadvantages of Quick Sort


Advantages Disadvantages
The quick sort is regarded as the The slight disadvantage of quick
best sorting algorithm. sort is that its worst-case
performance is similar to average
performances of the bubble,
insertion or selections sorts.
It is able to deal well with a huge If the list is already sorted than
list of items. bubble sort is much more efficient
than quick sort
Because it sorts in place, no If the sorting element is integers
additional storage is required as than radix sort is more efficient
well than quick sort.
Data Structure and Algorithms

Working of Quick Sort algorithm:

To know the functioning of Quick sort, let’s consider an array arr[] = {10,
80, 30, 90, 40, 50, 70}
• Indexes: 0 1 2 3 4 5 6
• low = 0, high = 6, pivot = arr[h] = 70
• Initialize index of smaller element, i = -1

Step1

• Traverse elements from j = low to high-1


• j = 0: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
• i=0
• arr[] = {10, 80, 30, 90, 40, 50, 70} // No change as i and j are
same
• j = 1: Since arr[j] > pivot, do nothing
Data Structure and Algorithms

Step2

• j = 2 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])


• i=1
• arr[] = {10, 30, 80, 90, 40, 50, 70} // We swap 80 and 30

Step3

• j = 3 : Since arr[j] > pivot, do nothing // No change in i and arr[]


• j = 4 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
• i=2
• arr[] = {10, 30, 40, 90, 80, 50, 70} // 80 and 40 Swapped

Step 4

• j = 5 : Since arr[j] <= pivot, do i++ and swap arr[i] with arr[j]
• i=3
Data Structure and Algorithms

• arr[] = {10, 30, 40, 50, 80, 90, 70} // 90 and 50 Swapped

Step 5

• We come out of loop because j is now equal to high-1.


• Finally we place pivot at correct position by swapping arr[i+1]
and arr[high] (or pivot)
• arr[] = {10, 30, 40, 50, 70, 90, 80} // 80 and 70 Swapped

Step 6

• Now 70 is at its correct place. All elements smaller than 70 are


before it and all elements greater than 70 are after it.
• Since quick sort is a recursive function, we call the partition
function again at left and right partitions
Data Structure and Algorithms

Step 7

• Again call function at right part and swap 80 and 90

Step 8
Data Structure and Algorithms

6. Heap Sort

Heap sort is a comparison-based sorting technique based on Binary Heap


data structure. It is similar to the selection sort where we first find the
minimum element and place the minimum element at the beginning.
Repeat the same process for the remaining elements.

Advantages and Disadvantages of Heap Sort


Advantages Disadvantages
The Heap sort algorithm is widely Heap sort requires more space for
used because of its efficiency. sorting
The Heap sort algorithm can be Quick sort is much more efficient
implemented as an in-place sorting than Heap in many cases
algorithm
its memory usage is minimal Heap sort make a tree of sorting
elements.

Working of Heap Sort algorithm:

To understand heap sort more clearly, let’s take an unsorted array and try
to sort it using heap sort. Consider the array: arr[] = {4, 10, 3, 5, 1}.
Build Complete Binary Tree: Build a complete binary tree from the array.

Build complete binary tree from the array

Transform into max heap: After that, the task is to construct a tree from
that unsorted array and try to convert it into max heap.
Data Structure and Algorithms

• To transform a heap into a max-heap, the parent node should


always be greater than or equal to the child nodes
• Here, in this example, as the parent node 4 is smaller
than the child node 10, thus, swap them to build a max-
heap.
• Transform it into a max heap

• Now, as seen, 4 as a parent is smaller than the child 5, thus swap


both of these again and the resulted heap and array should be
like this:

Make the tree a max heap


Data Structure and Algorithms

Perform heap sort: Remove the maximum element in each step (i.e.,
move it to the end position and remove that) and then consider the
remaining elements and transform it into a max heap.
• Delete the root element (10) from the max heap. In order to
delete this node, try to swap it with the last node, i.e. (1). After
removing the root element, again heapify it to convert it into max
heap.
• Resulted heap and array should look like this:

Remove 10 and perform heapify

• Repeat the above steps and it will look like the following:
Data Structure and Algorithms

Remove 5 and perform heapify

• Now remove the root (i.e. 3) again and perform heapify.


Data Structure and Algorithms

Remove 4 and perform heapify

• Now when the root is removed once again it is sorted. and the
sorted array will be like arr[] = {1, 3, 4, 5, 10}.

The sorted array


Data Structure and Algorithms

7. Counting Sort

Counting sort is a sorting technique based on keys between a specific


range. It works by counting the number of objects having distinct key
values (kind of hashing). Then do some arithmetic to calculate the position
of each object in the output sequence.

Advantages and Disadvantages of Counting Sort


Advantages Disadvantages
Best-case and average-case: O(n
+ k) Worst-case: O(n + k)
O(k) High memory usage for large k
Limited to sorting integers/small
Stable (preserves relative order) integer keys
Efficient for small key ranges Inefficient for large key ranges
Can outperform comparison-
based sorts Slower for general-purpose sorting

Working of Counting Sort algorithm:

Consider the array: arr[] = {1, 4, 1, 2, 7, 5, 2}.


• Input data: {1, 4, 1, 2, 7, 5, 2}
• Take a count array to store the count of each unique object.
Data Structure and Algorithms

• Now, store the count of each unique element in the count array
• If any element repeats itself, simply increase its count.

• Here, the count of each unique element in the count array is as


shown below:
• Index: 0 1 2 3 4 5 6 7 8 9
• Count: 0 2 2 0 1 1 0 1 0 0

• Modify the count array such that each element at each index
stores the sum of previous counts.
• Index: 0 1 2 3 4 5 6 7 8 9
• Count: 0 2 4 4 5 6 6 7 7 7
• The modified count array indicates the position of each object in
the output sequence.
Data Structure and Algorithms

• Find the index of each element of the original array in the count
array. This gives the cumulative count.

• Rotate the array clockwise for one time.


• Index: 0123456789
• Count: 0024456677
Data Structure and Algorithms

• Output each object from the input sequence followed by


increasing its count by 1.
• Process the input data: 1, 4, 1, 2, 7, 5, 2. Position of 1 is 0.
• Put data 1 at index 0 in output. Increase count by 1 to place
next data 1 at an index 1 greater than this index.

• After placing each element at its correct position, decrease its


count by one.

You might also like