Unit 5
Unit 5
Insertion Sort – Quick Sort – Heap Sort – Merge Sort –Linear Search – Binary Search
INTRODUCTION TO SORTING
Sorting is the process of arranging the elements of an array so that they can be
placed either in ascending or descending order. Efficient sorting is important to
optimizing the use of other algorithms that require sorted lists to work correctly
and for producing human – read able input
For example,
Sorting and Searching Techniques
There are many techniques by using which, sorting can be performed
Sorting
Sl. No. Description
Algorithms
Insertion sort inserts each element of the array to its
1 Insertion Sort proper place. It is a very simple sort method which is used
to arrange the deck of cards while playing bridge
Quick sort follows the divide and conquer approach in
which the algorithm is breaking down into sub problems,
2 Quick Sort
then solving the sub problems, and combining the results
back together to solve the original problem.
In the heap sort, Min heap or max heap is maintained
from the array elements depending upon the choice and
3 Heap Sort
the elements are sorted by deleting the root element of the
heap.
Merge sort follows divide and conquer approach in
which, the list is first divided into the sets of equal
4 Merge Sort 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
INSERTION SORT
Insertion sort is a simple sorting algorithm.
This sorting method sorts the array by shifting elements one by one.
It builds the final sorted array one item at a time.
Insertion sort has one of the simplest implementation.
This sort is efficient for smaller data sets but it is insufficient for larger lists.
It has less space complexity like bubble sort.
It requires single additional memory space.
Insertion sort does not change the relative order of elements with equal keys
because it is stable.
Algorithm:
Step 1 − If the element is the first one, it is already sorted.
Step 2 – Move to next element
Step 3 − Compare the current element with all elements in the sorted array
Step 4 – If the element in the sorted array is smaller than the current element, iterate to
the next element. Otherwise, shift all the greater element in the array by one
position towards right
Step 5 − Insert the value at the correct position
Step 6 − Repeat until the complete list is sorted
Working of Insertion sort Algorithm
Consider an unsorted array of elements 40, 10, 9, 20, 30, 50
The above steps represents how insertion sort works. Insertion sort works like the
way we sort playing cards in our hands. It always starts with the second element
as key. The key is compared with the elements ahead of it and is put it in the right
place.
At the first step, 40 has nothing before it. Element 10 is compared to 40 and is
inserted before 40. Element 9 is smaller than 40 and 10, so it is inserted before 10
and this operation continues until the array is sorted in ascending order.
Analysis of Insertion Sort:
Time Complexity
Best O(n)
Worst O(n2)
Average O(n2)
Space Complexity O(1)
Stability Yes
Applications
The insertion sort is used when:
The array is has a small number of elements
There are only a few elements left to be sorted
Example Program 5.1
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
{
scanf("%d", &array[c]);
}
for (c = 1 ; c <= n - 1; c++)
{
d = c;
while ( d > 0 && array[d] < array[d-1])
{
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++)
{
printf("%d\n", array[c]);
}
return 0;
}
Output
Enter the number of elements
5
Enter 5 integers
40
30
20
10
40
Sorted list in ascending order
10
20
30
40
40
QUICK SORT
Quick sort is also known as Partition-exchange sort based on the rule of Divide
and Conquer.
It is a highly efficient sorting algorithm.
Quick sort is the quickest comparison-based sorting algorithm.
It is very fast and requires less additional space, only O(n log n) space is required.
Quick sort picks an element as pivot and partitions the array around the picked
pivot.
What is a heap?
A heap is a complete binary tree, and the binary tree is a tree in which the node
can have the utmost two children. A complete binary tree is a binary tree in which
all the levels except the last level, i.e., leaf node, should be completely filled, and
all the nodes should be left-justified.
First, construct a heap from the given array and convert it into max heap
After converting the given heap into max heap, the array elements are
Next step is to delete the root element (89) from the max heap. To delete this node,
swap it with the last node, i.e. (11). After deleting the root element, again heapify
it to convert it into max heap.
After swapping the array element 89 with 11, and converting the heap into max-
heap, the elements of array are
After swapping the array element 81 with 54 and converting the heap into max-
heap, the elements of array are
In the next step, delete the root element (76) from the max heap again. To delete
this node, swap it with the last node, i.e. (9). After deleting the root element, again
heapify it to convert it into max heap.
After swapping the array element 76 with 9 and converting the heap into max-
heap, the elements of array are
In the next step, again delete the root element (54) from the max heap. To delete
this node, swap it with the last node, i.e. (14). After deleting the root element,
again heapify it to convert it into max heap.
After swapping the array element 54 with 14 and converting the heap into max-
heap, the elements of array are
In the next step, again delete the root element (22) from the max heap. To delete
this node, swap it with the last node, i.e. (11). After deleting the root element,
again heapify it to convert it into max heap.
After swapping the array element 22 with 11 and converting the heap into max-
heap, the elements of array are
In the next step, again delete the root element (14) from the max heap. To delete
this node, swap it with the last node, i.e. (9). After deleting the root element, again
heapify it to convert it into max heap.
After swapping the array element 14 with 9 and converting the heap into max-
heap, the elements of array are
In the next step, again delete the root element (11) from the max heap. To delete
this node, swap it with the last node, i.e. (9). After deleting the root element, again
heapify it to convert it into max heap.
After swapping the array element 11 with 9, the elements of array are
Now, heap has only one element left. After deleting it, heap will be empty.
heapify(a, n, largest);
}
}
/*Function to implement the heap sort*/
void heapSort(int a[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
heapify(a, n, i);
// One by one extract an element from heap
for (int i = n - 1; i >= 0; i--) {
/* Move current root element to end*/
// swap a[0] with a[i]
int temp = a[0];
a[0] = a[i];
a[i] = temp;
heapify(a, i, 0);
}
}
/* function to print the array elements */
void printArr(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf("%d", arr[i]);
printf(" ");
}
}
int main()
{
int a[] = {42, 8, 26, 39, 28, 23, 7};
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
heapSort(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}
Output
Before sorting array elements are
42, 8, 26, 39, 28, 23, 7
After sorting array elements are
7, 8, 23, 26, 28, 39, 42
MERGE SORT
Merge sort is a sorting technique based on divide and conquer technique.
With worst-case time complexity being Ο(n log n), it is one of the most respected
algorithms.
Merge sort first divides the array into equal halves and then combines them in a
sorted manner
Algorithm
Merge sort keeps on dividing the list into equal halves until it can no more be
divided. By definition, if it is only one element in the list, it is sorted. Then, merge
sort combines the smaller sorted lists keeping the new list sorted too
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.
First divide the given array into two equal halves. Merge sort keeps dividing the
list into equal parts until it cannot be further divided.
As there are eight elements in the given array, so it is divided into two arrays of
size 4.
Now, again divide these two arrays into halves. As they are of size 4, so divide
them into new arrays of size 2.
Now, again divide these arrays to get the atomic value that cannot be further
divided.
Now, combine them in the same manner they were broken. First compare the
element of each array and then combine them into another array in sorted order.
So, first compare 12 and 31, both are in sorted positions. Then compare 25 and 8,
and in the list of two values, put 8 first followed by 25. Then compare 32 and 17, sort
them and put 17 first followed by 32. After that, compare 40 and 42, and place them
sequentially.
In the next iteration of combining, now compare the arrays with two data values
and merge them into an array of found values in sorted order.
Now, there is a final merging of the arrays. After the final merging of above
arrays, the array will look like
INTRODUCTION TO SEARCHING
Searching in data structure refers to the process of finding the required
information from a collection of items stored as elements in the computer
memory.
These sets of items are in different forms, such as an array, linked list, graph, or
tree. Another way to define searching in the data structures is by locating the
desired element of specific characteristics in a collection of items
Searching Methods
Searching in the data structure can be done by applying searching algorithms to
check for or extract an element from any form of stored data structure. These
algorithms are classified according to the type of search operation they perform,
such as:
Sequential search - The list or array of elements is traversed sequentially while
checking every component of the set. For example – Linear Search.
Interval Search - The interval search includes algorithms that are explicitly
designed for searching in sorted data structures. In terms of efficiency, these
algorithms are far better than linear search algorithms. Example- Logarithmic
Search, Binary search.
These methods are evaluated based on the time taken by an algorithm to search
an element matching the search item in the data collections and are given by,
The best possible time
The average time
The worst-case time
The primary concerns are with worst-case times, which provide guaranteed
predictions of the algorithm’s performance and are also easier to calculate than average
times.
LINEAR SEARCH
Linear search is also called as sequential search algorithm.
It is the simplest searching algorithm.
In Linear search, we simply traverse the list completely and match each element
of the list with the item whose location is to be found.
If the match is found, then the location of the item is returned; otherwise, the
algorithm returns NULL.
It is widely used to search an element from the unordered list, i.e., the list in which
items are not sorted.
The worst-case time complexity of linear search is O (n).
Steps used in the implementation of Linear Search
First, we have to traverse the array elements using for loop.
In each iteration of for loop, compare the search element with the current array
element, and
If the element matches, then return the index of the corresponding array
element.
If the element does not match, then move to the next element.
If there is no match or the search element is not present in the given array, return
-1.
Algorithm
Linear_Search(a, n, val) // 'a' is the given array, 'n' is the size of given array, 'val'
is the value to search
Step 1: set pos = -1
Step 2: set i = 1
Step 3: repeat step 4 while i <= n
Step 4: if a[i] == val
set pos = i
print pos
go to step 6
[end of if]
set ii = i + 1
[end of loop]
Step 5: if pos = -1
print "value is not present in the array "
[end of if]
Step 6: exit
The value of K, i.e., 41, is not matched with the first element of the array. So, move to
the next element. And follow the same process until the respective element is found.
Now, the element to be searched is found. So algorithm will return the index of
the element matched.
BINARY SEARCH
Binary search is the search technique that works efficiently on sorted lists.
Hence, to search an element into some list using the binary search technique, we
must ensure that the list is sorted.
Binary search follows the divide and conquer approach in which the list is divided
into two halves, and the item is compared with the middle element of the list.
If the match is found then, the location of the middle element is returned.
Otherwise, we search into either of the halves depending upon the result produced
through the match.
Algorithm
Binary_Search(a, lower_bound, upper_bound, val) // 'a' is the given array,
'lower_bound' is the index of the first array element, 'upper_bound' is the index of the last
array element, 'val' is the value to search
Step 1: set beg = lower_bound, end = upper_bound, pos = - 1
Step 2: repeat steps 3 and 4 while beg <=end
Step 3: set mid = (beg + end)/2
Step 4: if a[mid] = val
set pos = mid
print pos
go to step 6
else if a[mid] > val
set end = mid - 1
else
set beg = mid + 1
[end of if]
[end of loop]
Step 5: if pos = -1
print "value is not present in the array"
[end of if]
Step 6: exit
12. Why quick sort is preferred for arrays and merge sort for linked lists?
Quick sort is an in-place sorting algorithm, i.e. which means it does not require
any additional space, whereas Merge sort does, which can be rather costly. In
merge sort, the allocation and deallocation of the excess space increase the
execution time of the algorithm.
Unlike arrays, in linked lists, we can insert elements in the middle in O(1) extra
space and O(1) time complexities if we are given a reference/pointer to the
previous node. As a result, we can implement the merge operation in the merge
sort without using any additional space.
13. In which case insertion sort is used?
Insertion sort has a fast best-case running time and is a good sorting algorithm
to use if the input list is already mostly sorted.
14. What is the advantage of using Quick sort algorithm?
Quick sort reduces unnecessary swaps and moves an item to a greater distance,
in one move.
15. Mention the various types of searching techniques in C.
Linear search
Binary search
16. Define Searching.
Searching in data structure refers to the process of finding the required
information from a collection of items stored as elements in the computer
memory.
These sets of items are in different forms, such as an array, linked list, graph, or
tree.
17. Compare Quick sort and Merge Sort.
Basis for comparison Quick Sort Merge Sort
Efficiency Inefficient for larger arrays More efficient
Sorting method Internal External
Stability Not Stable Stable
Preferred for for Arrays for Linked Lists