Professional Documents
Culture Documents
Unit 4
Unit 4
Sorting is the process of arranging the elements of an array so that they can be placed either in
ascending or descending order. For example, consider an array A = {A1, A2, A3, A4, ?? An },
the array is called to be in ascending order if element of A are arranged like A1 > A2 > A3 > A4
> A5 > ? > An .
Consider an array;
int A[10] = { 5, 4, 10, 2, 30, 45, 34, 14, 18, 9 )
The Array sorted in ascending order will be given as;
A[] = { 2, 4, 5, 9, 10, 14, 18, 30, 34, 45 }
There are many techniques by which sorting can be performed. In this section of the tutorial, we
will discuss each method in detail.
SN Sorting Description
Algorith
ms
2 Bucket Bucket sort is also known as bin sort. It works by distributing the
Sort element into the array also called buckets. In this sorting algorithms,
Buckets are sorted individually by using different sorting algorithm.
3 Comb Sort Comb Sort is the advanced form of Bubble Sort. Bubble Sort compares
all the adjacent values while comb sort removes all the turtle values or
small values near the end of the list.
4 Counting It is a sorting technique based on the keys i.e. objects are collected
Sort according to keys which are small integers. Counting sort calculates the
number of occurrences of objects and stores its key values. New array is
formed by adding previous key elements and assigning them to objects.
5 Heap Sort In the heap sort, Min heap or max heap is maintained from the array
elements depending upon the choice and the elements are sorted by
deleting the root element of the heap.
6 Insertion As the name suggests, insertion sort inserts each element of the array to
Sort its proper place. It is a very simple sort method which is used to arrange
the deck of cards while playing bridge.
7 Merge Sort Merge sort follows the divide and conquer approach in which the list is
first divided into the sets of equal elements and then each half of the list
is sorted by using merge sort. The sorted list is combined again to form
an elementary sorted array.
8 Quick Sort Quick sort is the most optimized sort algorithm which performs sorting
in O(n log n) comparisons. Like Merge sort, quick sort also works by
using divide and conquer approach.
9 Radix Sort In Radix sort, the sorting is done as we do sort the names according to
their alphabetical order. It is the linear sorting algorithm used for
Integers.
10 Selection Selection sort finds the smallest element in the array and places it on the
Sort first place on the list, then it finds the second smallest element in the
array and places it on the second place. This process continues until all
the elements are moved to their correct ordering. It carries running time
O(n2) which is worse than insertion sort.
11 Shell Sort Shell sort is the generalization of insertion sort which overcomes the
drawbacks of insertion sort by comparing elements separated by a gap of
several positions.
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.
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 the 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:
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 gets placed at the last position in the
array The resulting 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.
First Pass:
Bubble sort starts with the 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 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), the algorithm
does not swap them.
Second Pass:
Now, during second iteration it should look like this:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
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:
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
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
5 11 12 13 6
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
5 11 6 12 13
5 6 11 12 13
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.
3. Combine: This step makes use of the merge( ) function to combine the sub-arrays into the
final sorted array.
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.
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.
Illustration of Quicksort:
As the partition process is done recursively, it keeps on putting the pivot in its actual
position in the sorted array. Repeatedly putting pivots in their actual position makes the
array sorted.
Follow the below images to understand how the recursive implementation of the
partition algorithm helps to sort the array.
Initial partition on the main array:
#include <iostream>
using namespace std;
int main() {
int arr[] = {23, 8, 28, 13, 18, 26};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
return 0;
}
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.
● 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
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:
Remove 5 and perform heapify
● Now remove the root (i.e. 3) again and perform heapify.
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.
● 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.
● 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: 0 1 2 3 4 5 6 7 8 9
● Count: 0 0 2 4 4 5 6 6 7 7
● 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.
Searching
Searching Algorithms are designed to check for an element or retrieve an element from any data
structure where it is stored.
Linear Search is defined as a sequential search algorithm that starts at one end and goes through
each element of a list until the desired element is found, otherwise the search continues till the
end of the data set.
For example: Consider the array arr[] = {10, 50, 30, 70, 80, 20, 90, 40} and key = 30
Step 1: Start from the first element (index 0) and compare key with each element (arr[i]).
● Comparing key with first element arr[0]. SInce not equal, the iterator moves to the next
element as a potential match.
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
int result = search(arr, N, x);
(result == -1)
? cout << "Element is not present in array"
: cout << "Element is present at index " << result;
return 0;
}
Output
Element is present at index 3
Binary Search
Binary Search is defined as a searching algorithm used in a sorted array by repeatedly dividing the search
interval in half. The idea of binary search is to use the information that the array is sorted and reduce the
time complexity to O(log N).
// Driver code
int main()
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
(result == -1)
? cout << "Element is not present in array"
: cout << "Element is present at index " << result;
return 0;
}
Output
Element is present at index 3
Hashing
Hashing refers to the process of generating a fixed-size output from an input of variable size
using the mathematical formulas known as hash functions. This technique determines an index or
location for the storage of an item in a data structure.
Need for Hash data structure
The amount of data on the internet is growing exponentially every day, making it difficult to
store it all effectively. In day-to-day programming, this amount of data might not be that big, but
still, it needs to be stored, accessed, and processed easily and efficiently. A very common data
structure that is used for such a purpose is the Array data structure.
Now the question arises if Array was already there, what was the need for a new data structure!
The answer to this is in the word “efficiency“. Though storing in Array takes O(1) time,
searching in it takes at least O(log n) time. This time appears to be small, but for a large data set,
it can cause a lot of problems and this, in turn, makes the Array data structure inefficient.
So now we are looking for a data structure that can store the data and search in it in constant
time, i.e. in O(1) time. This is how Hashing data structure came into play. With the introduction
of the Hash data structure, it is now possible to easily store data in constant time and retrieve it in
constant time as well.
Components of Hashing
There are majorly three components of hashing:
1. Key: A Key can be anything string or integer which is fed as input in the hash
function, the technique that determines an index or location for storage of an item in a
data structure.
2. Hash Function: The hash function receives the input key and returns the index of an
element in an array called a hash table. The index is known as the hash index.
3. Hash Table: Hash table is a data structure that maps keys to values using a special
function called a hash function. Hash stores the data in an associative manner in an
array where each data value has its own unique index.
Components of Hashing
What is Collision?
The hashing process generates a small number for a big key, so there is a possibility that two
keys could produce the same value. The situation where the newly inserted key maps to an
already occupied, and it must be handled using some collision handling technology.
Collision in Hashing