DSA - Sorting
DSA - Sorting
Introduction
algorithm specifies the way to arrange data in a particular order.
Most common orders are in numerical or lexicographical order.
Sorting is also used to represent data in more readable formats.
Following are some of the examples of sorting in real-life
scenarios:
Telephone Directory − The telephone directory stores the
School of Computer Engineering
Sorting Algorithm
telephone numbers of people sorted by their names, so that the
names
Sr# can be
Traditional searched
Sorting easily.
Sorting based on Divide & Conquer Sorting based on Trees
1Dictionary
Bubble Sort Merge sortstores wordsHeap
− The dictionary in an Sort alphabetical
2order so that searching
Insertion sort Quickof any word becomes easy.
sort
3 Selection sort
4 Radix Sort
4
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison based algorithm in which
Bubble Sort
each pair of adjacent elements is compared and elements are swapped if they are not in order. This
algorithm is not suitable for large data sets as its average and worst case complexity is of O(n2) where n
are no. of items.
Bubble sort starts with very first two elements, comparing them to check which
one is greater.
In this case, 33 is greater than 14, so it is already in sorted locations. Next, it
compares 27 with 33.
We find that 27 is smaller than 33 and these two values must be swapped.
We swap these values. We find that we reach at the end of the array. After one
School of Computer Engineering
To be precise, we are now showing that how array should look like after each
iteration. After second iteration, it should look like this
Notice that after each iteration, at least one value moves at the end
When there's no swap required, bubble sorts learns that array is completely sorted.
Bubble Sort cont...
6
Animation
https://cathyatseneca.github.io/DSAnim/web/bubble.html
8
Insertion sort is in-place comparison based sorting algorithm. A sub-list is maintained which is always
Insertion Sort
sorted. For example, the lower part of an array is maintained to be sorted. A element which is to be
inserted in the sorted sub-list, has to find its appropriate place and insert it there. Hence the name
insertion sort.
The array is searched sequentially and unsorted items are moved and inserted into sorted sub-list (in the
same array). This algorithm is not suitable for large data sets as its average and worst case complexity
are of Ο(n2) where n are no. of items.
School of Computer Engineering
swapping.
So swap them
Insertion Sort cont…
10
6. Set j = i - 1
7. While j >= 0 AND key < LA[j] repeat 8 and 9 j = i-1;
8. LA[j + 1] = LA [j] while(j>=0 && key < a[j])
9. Set j = j – 1
10. LA[j + 1] = key {
11. Stop a[j+1] = a[j];
j--;
}
Insertion Sort cont…
12
Time Complexity
Time complexity of Insertion Sort is
O(n2) in worst case
θ(n2) in average case
Ω(n) in best case
School of Computer Engineering
Space Complexity
Space complexity is O(1)
Animation
https://courses.cs.vt.edu/csonline/Algorithms/Lessons/InsertionCardSort/insertioncardsort.swf
13
Selection sort is a in-place comparison based algorithm in which the list is divided into two parts, sorted
Selection Sort
part at left end and unsorted part at right end. Initially sorted part is empty and unsorted part is entire list.
Smallest element is selected from the unsorted array and swapped with the leftmost element and that
element becomes part of sorted array. This process continues moving unsorted array boundary by one
element to the right. This algorithm is not suitable for large data sets as its average and worst case
complexity are of O(n2) where n are no. of items.
School of Computer Engineering
For the first position in the sorted list, the whole list
is scanned sequentially. The first position where 14
is stored presently, we search the whole list and
find that 10 is the lowest value.
Selection Sort cont…
14
The same process is applied on the rest of the items in the array and pictorial depiction of entire
sorting process is
C Code Pseudo
Snippet
int a[6] = {5, 1, 6, 2, 4, 3}; procedure selection sort
code
list : array of items
int i, j, min, temp;
n : size of list
for(i=0; i<6; i++)
{ for i = 1 to n - 1
min= i; min = i /* set current element as minimum*/
for(j=i+1; j<6; j++) for j = i+1 to n /* check the element to be minimum */
School of Computer Engineering
Time Complexity
Time complexity of Selection Sort is
O(n2) in worst case
θ(n2) in average case
Ω(n2) in best case
School of Computer Engineering
Space
Complexity
Space complexity is O(1)
Animation
http://www.algostructure.com/sorting/selectionsort.php
18
Radix Sort is a clever and intuitive little sorting algorithm. The idea
of Radix Sort is to do digit by digit sort starting from least
Radix Sort
significant digit to most significant digit.
Example:
Original, unsorted list: 170, 45, 75, 90, 802, 24, 2, 66
School of Computer Engineering
In divide and conquer approach, the problem in hand, is divided into smaller sub-problems and then each
problem is solved independently. When we keep on dividing the sub-problems into even smaller sub-
problems, we may eventually reach at a stage where no more dividation is possible. Those "atomic" smallest
possible sub-problem (fractions) are solved. The solution of all sub-problems is finally merged in order to
obtain the solution of original problem.
This algorithmic approach works recursively and conquer & merge steps works so close that they
appear as one.
22
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
Merge sort first divides the array into equal halves and then combines them in a sorted manner.
Quick Sort
holds values smaller than specified value say pivot based on
which the partition is made and another array holds values
How Quick
greater thanSort works?
pivot value. The quick sort partitions an array and
Quickcalls
then Sortitself
Algorithm
recursively twiceQuick
to sortSortthePseudo code two sub-
resulting
School of Computer Engineering
Quick sort is a divide and conquer algorithm. Its divided large list in mainly three parts:
//do nothing }
} }
if(leftPointer >= rightPointer) void swap(int num1, int num2)
{ {
break; int temp = a[num1];
} a[num1] = a[num2];
else a[num2] = temp;
{ }
swap(leftPointer, rightPointer); int main()
} {
} quickSort(0,5);
swap(leftPointer, right); return 0;
return leftPointer; }
}
29
Heap sort is a comparison based sorting technique based on Binary Heap data structure.
Complete Binary Tree: A binary tree T with n levels is complete if all levels except possibly the last
are completely full, and the last level has all its nodes to the left side.
Heap Sort
A Binary Heap: It is a Complete Binary Tree where items are stored in a special order such that value
in a parent node is greater(or smaller) than the values in its two children nodes. The former is called
as max heap and the latter is called min heap.
Since a Binary Heap is a Complete Binary Tree, it can be easily represented as array and array based
representation is space efficient. If the parent node is stored at index I, the left child can be calculated
by 2 * I + 1 and right child by 2 * I + 2 (assuming the indexing starts at 0).
How Heap Sort Wo
30
Transform
2. and put thethe lastheap
nodeinto Transform
Minroot2.position
in the the heap
and repeat theinto Max
process
Heap
till we have covered all the elements Heap
3. Delete the root node 3. Delete the root node
4. Put the last node of the heap 4. Put the last node of the heap
in root position in root position
5. Repeat from step 2 till all 5. Repeat from step 2 till all
31
Class Work
# Best Average Worst Comple
xity
2 Insertion Sort
3 Selection Sort
4 Quick Sort
5 Merge Sort
6 Heap Sort
7 Radix Sort
34
Assignments
1. Let A[1], A[2], . . . , A[n] be an array containing very large positive integers.
Describe an efficient algorithm to find the minimum positive difference
between any two integers in the array. What is the complexity of your
algorithm? Explain.
inversion is a pair of indices i and j such that i < j but A[i] > A [j]. For
example in the array [30000, 80000, 20000, 40000, 10000], the pair i = 1
and j = 3 is an inversion because A [1] = 30000 is greater than A[3] =
20000. On the other hand, the pair i = 1 and j = 2 is not an inversion
because A [1] = 30000 is smaller than A [2] = 80000. In this array there are
7 inversions and 3 non-inversions. Describe an efficient algorithm that
counts the number of inversions in any array. What is the running
time of your algorithm?
35
Home Work
Bubble Sort
Insertion Sort
Selection Sort
Quick Sort
Merge Sort
Heap Sort
School of Computer Engineering
Radix Sort
Supplementary Rea
37
http://www.geeksforgeeks.org/sorting-algorithms/
https://www.tutorialspoint.com/data_structures_algorithms/sorting_algorithms.htm
http://www.studytonight.com/data-structures/introduction-to-sorting
http://nptel.ac.in/courses/Webcourse-contents/IIT-%20Guwahati/data_str_algo/Module_5/binder1.pdf
http://nptel.ac.in/courses/106105164/
Bubble Sort : Exchange two adjacent elements if they are out of order. Repeat until array
is sorted.
Insertion sort : Scan successive elements for an out-of-order item, then insert the item in
the proper place.
Selection sort : Find the smallest element in the array, and put it in the proper place.
Swap it with the value in the first position. Repeat until array is sorted.
Quick sort : Partition the array into two segments. In the first segment, all elements are
less than or equal to the pivot value. In the second segment, all elements are greater than
FAQ
School of Computer Engineering
or equal to the pivot value. Finally, sort the two segments recursively.
Merge sort : Divide the list of elements in two parts, sort the two parts individually and
then merge it.
Heap sort : The first maximum element is searched and place it at the end and the
process is repeated for remaining elements.
Radix sort : Sorts data with integer keys by grouping keys by the individual digits which
share the same significant position and value.