Sorting
Sorting
7 Sorting 4
Bubble Sort, Insertion Sort, Selection Sort, Quick
Sort, Merge Sort, Heap Sort, Radix Sort
Sorting
Algorithm
Sr# Traditional Sorting Sorting based on Divide & Conquer Sorting based on Trees
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.
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.
Algorith C Code
m
Suppose LA is an array with n integer elements and Snippet
int a[6] = {5, 1, 6, 2, 4, 3};
swap function is to exchange the values for the int i, j, temp;
array elements. Below is the Bubble Sort algorithm
for(i=0; i<6; i++)
to arrange the elements in ascending order
{
1. Start
for(j=0; j<6-i-1; j++)
2. Set i=0
{
3. Set j = 0
4. Repeat steps 5 till 9 while i < n if( a[j] > a[j+1])
5. Set j = 0 {
6. Repeat steps 7 till 8 while j < n – i - 1 temp = a[j]; a[j] = a[j+1]; a[j+1] = temp;
7. If (LA[j] > LA[j+1]) then swap (LA[j], LA[j+1]) }
8. Set j = j + 1 }
9. Set i = i + 1 }
10. Stop
Time Space
Complexity
n-1 comparisons will be done in 1st pass, n-2 in Complexity
Auxiliary Space is the extra space or temporary
2nd pass, n-3 in 3rd pass and so on. So the total space used by an algorithm. if we want to
number of comparisons will be compare standard sorting algorithms on the basis
of space, then auxiliary space would be a better
(n-1)+(n-2)+(n-3)+.....+3+2+1
criteria than space complexity. Space complexity
Sum = n * (n-1)/2
is O(1) since only single additional memory space
i.e O(n2)
is required for temp variable
Animatio
n
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
Algorith C Code
m
Suppose Snippet
LA is an array with n integer elements and int a[6] = {5, 1, 6, 2, 4, 3};
below is the Insertion Sort algorithm to arrange the
int i, j, key;
elements in ascending order
for(i=1; i<6; i++)
1. Start {
2. Set i=1, j = 0, key = 0
key = a[i];
4. Repeat steps 5 till 10 while i < n
j = i-1;
5. Set key = LA[i]
while(j>=0 && key < a[j])
6. Set j = i - 1
7. While j >= 0 AND key < LA[j] repeat 8 and 9 {
8. LA[j + 1] = LA [j] a[j+1] = a[j];
9. Set j = j – 1 j--;
10. LA[j + 1] = key }
11. Stop a[j+1] = key;
}
Time
Complexity
Time complexity of Insertion Sort is
❑ O(n2) in worst case
❑ θ(n2) in average case
❑ Ω(n) in best case
Space
Complexity
Space complexity is O(1)
Animatio
n
https://courses.cs.vt.edu/csonline/Algorithms/Lessons/InsertionCardSort/insertioncardsort.swf
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.
The same process is applied on the rest of the items in the array and pictorial depiction of entire
sorting process is
Time
Complexity
Time complexity of Selection Sort is
❑ O(n2) in worst case
❑ θ(n2) in average case
❑ Ω(n2) in best case
Space Complexity
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 average case
❑ Ω(w*n) in best case
Note: w is the word size and n is the number of elements
❑ Divide/Break: This step involves breaking the problem into smaller sub-problems. Sub-problems
should represent as a part of original problem. This step generally takes recursive approach to
divide the problem until no sub-problem is further dividable. At this stage, sub-problems become
atomic in nature but still represents some part of actual problem.
❑ Conquer/Solve: This step receives lot of smaller sub-problem to be solved. Generally at this level,
problems are considered 'solved' on their own.
❑ Merge/Combine: When the smaller sub-problems are solved, this stage recursively combines
them until they formulate solution of the original problem.
This algorithmic approach works recursively and conquer & merge steps works so close that they
appear as one.
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.
Quick sort is a divide and conquer algorithm. Its divided large list in mainly three parts:
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).
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
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.
❑ 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/