DSA - Sorting
DSA - Sorting
7 Sorting 4
Bubble Sort, Insertion Sort, Selection Sort,
Quick Sort, Merge Sort, Heap Sort, Radix
Sort
Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to arrange data in a particular
order. Most common orders are in numerical or lexicographical order. The importance of sorting lies in the fact that data
searching can be optimized to a very high level, if data is stored in a sorted manner. 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 telephone numbers of people sorted by their names, so
that the names can be searched easily.
Dictionary − The dictionary stores words in an alphabetical order so that searching of any word becomes easy.
Important Terms
Increasing order − A sequence of values is said to be in increasing order, if the successive element is greater than the
previous one. For example, 1, 3, 4, 6, 8, 9 are in increasing order, as every next element is greater than the previous
element.
Decreasing Order − A sequence of values is said to be in decreasing order, if the successive element is less than the
current one. For example, 9, 8, 6, 4, 3, 1 are in decreasing order, as every next element is less than the previous element.
Non-increasing order − A sequence of values is said to be in non-increasing order, if the successive element is less
than or equal to its previous element in the sequence. This order occurs when the sequence contains duplicate values.
For example, 9, 8, 6, 3, 3, 1 are in non-increasing order, as every next element is less than or equal to (in case of 3) but
not greater than any previous element.
Non-decreasing Order − A sequence of values is said to be in non-decreasing order, if the successive element is
greater than or equal to its previous element in the sequence. This order occurs when the sequence contains duplicate
values. For example, 1, 3, 3, 6, 8, 9 are in non-decreasing order, as every next element is greater than or equal to (in case
of 3) but not less than the previous one.
Sr# Traditional Sorting Sorting based on Divide & Sorting based on Trees
Conquer
1 Bubble Sort Merge sort Heap Sort
2 Insertion sort Quick sort
3 Selection sort
4 Radix Sort
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison based algorithm in which
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
iteration the array should look like this
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.
(n-1)+(n-2)+(n-3)+.....+3+2+1
Sum = n * (n-1)/2
i.e O(n2)
Animation
https://cathyatseneca.github.io/DSAnim/web/bubble.html
Insertion sort is in-place comparison based sorting algorithm. A sub-list is maintained which is always
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.
So swap them
Time Complexity
Space Complexity
Selection sort is a in-place comparison based algorithm in which the list is divided into two parts, sorted
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.
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.
The same process is applied on the rest of the items in the array and pictorial depiction of entire
sorting process is
Time Complexity
Space Complexity
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.
Quick sort is based on partitioning of array of data into smaller arrays. A large array is
partitioned into two arrays one of which holds values smaller than specified value say pivot
based on which the partition is made and another array holds values greater than pivot
value. The quick sort partitions an array and then calls itself recursively twice to sort the
resulting two sub-array.
How Quick Sort works?
Quick Sort Algorithm Quick Sort Pseudo code
Step 1 − Start procedure quickSort(left, right)
Step 2 − Make the right-most index value pivot if right-left <= 0
Step 3 − partition the array using pivot value return
Step 4 − quicksort left partition recursively else
Step 5 − quicksort right partition recursively pivot = A[right]
Step 6 − Stop partition = partitionFunc(left, right, pivot)
quickSort(left,partition-1)
quickSort(partition+1,right)
end if
end procedure
Quick sort is a divide and conquer algorithm. Its divided large list in mainly three parts:
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.
return c
Space complexity is O(n) end procedure
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. 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).
the root position and repeat the process till we have covered all the elements
Pointes to Remember for ascending order Pointes to Remember for descending order
sorting sorting
1. Build Heap 1. Build Heap
2. Transform the heap into Min Heap 2. Transform the heap into Max Heap
3. Delete the root node 3. Delete the root node
4. Put the last node of the heap in root 4. Put the last node of the heap in root
position position
5. Repeat from step 2 till all nodes are 5. Repeat from step 2 till all nodes are
covered covered
C Code
Max Heap
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 significant digit to most significant digit.
Example:
Original, unsorted list: 170, 45, 75, 90, 802, 24, 2, 66
Sorting by least significant digit (1s place) gives: 170, 90, 802, 2, 24, 45, 75, 66
Sorting by next digit (10s place) gives: 802, 2, 24, 45, 66, 170, 75, 90
Sorting by most significant digit (100s place) gives: 2, 24, 45, 66, 75, 90, 170, 802
Time Complexity Space Complexity
Time complexity of Radix Sort is Space complexity is O(w+n)
O(w*n) in worst case
Ω(w*n) in best case
θ(w*n) in average case
Note: w is the word size and n is the number of elements
Radix Sort
1. Let A[1], A[2], . . . , A[n] be an array containing n 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.
Bubble Sort
Insertion Sort
Selection Sort
Quick Sort
Merge Sort
Heap Sort
Radix Sort
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 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.