Sorting, Bubble, Selection, Insertion
Sorting, Bubble, Selection, Insertion
• Being unique, phone number can work as a key to locate any record
in the list.
Introduction
• Sorting is among the most basic problems in algorithm design.
• External Sorting
When the data to be sorted can’t be accommodated in the memory at the
same time and some has to be kept in auxiliary memory, then external sorting
methods are used.
• The choice of sorting method depends on efficiency considerations for different problems.
7
Efficiency of Sorting Algorithm
• Various sorting methods are analyzed in the cases like – best case,
worst case or average case.
8
Efficiency of Sorting Algorithm
• Determining the time requirement of sorting technique is to
actually run the program and measure its efficiency.
9
Efficiency of Sorting Algorithm
• Space constraints are usually less important than time
considerations.
• The reason for this can be, as for most sorting programs, the
amount of space needed is closer to 0(n) than to 0(n2)
10
Things to remember
• An ideal sort is an in-place sort where the algorithm uses no additional array
storage, and hence it is possible to sort very large lists without the need to allocate
additional working storage.
• A sorting algorithm is said to be stable if two objects with equal keys appear in the
same order in sorted output as they appear in the input unsorted array.
• Some sorting algorithms are stable by nature like Insertion sort, Merge Sort, Bubble
Sort, etc. And some sorting algorithms are not, like Heap Sort, Quick Sort, etc.
11
Things to remember
• Sorting can be performed in many ways.
• Over a time several methods (or algorithms) are being developed to sort
data(s).
• Bubble sort, Selection sort, Quick sort, Merge sort, Insertion sort are the few
sorting techniques.
12
BUBBLE SORT
• In bubble sort, each element is compared with its adjacent element.
• We begin with the 0th element and compare it with the 1st element.
• In this way all the elements are compared (excluding last) with their
next element and are interchanged if required
13
• On completing the first iteration, largest element gets placed at the
last position. Similarly in second iteration second largest element gets
placed at the second last position and so on.
14
14
Algorithm
15
TIME COMPLEXITY
• The time complexity for bubble sort is calculated in terms of the number of
comparisons f(n) (or of number of loops)
• Here two loops(outer loop and inner loop) iterates(or repeated) the
comparison.
• The inner loop is iterated one less than the number of elements in the list (i.e.,
n-1 times) and is reiterated upon every iteration of the outer loop
f (n) = (n – 1) + (n – 2) + ...... + 2 + 1 = n (n – 1) = O(n2).
16
TIME COMPLEXITY
• Best Case
• sorting a sorted array by bubble sort algorithm
• In best case outer loop will terminate after one iteration, i.e it involves performing one pass which
requires n-1 comparison
f (n) = O(n)
• Worst Case
• Suppose an array [5,4,3,2,1], we need to move first element to end of an array
• n-1 times the swapping procedure is to be called f (n) = O(n2)
• Average Case
• Difficult to analyse than the other cases
• Random inputs, so in general f (n) = O(n2)
• Space Complexity O(n)
17
SELECTION SORT
• Find the least (or greatest) value in the array, swap it into the leftmost(or rightmost)
component, and then forget the leftmost component, Do this repeatedly.
• Let a[n] be a linear array of n elements. The selection sort works as follows:
• Pass 1: Find the location loc of the smallest element in the list of n elements a[0], a[1], a[2],
a[3], .........,a[n-1] and then interchange a[loc] and a[0].
• Pass 2: Find the location loc of the smallest element int the sub-list of n-1 elements a[1],
a[2], a[3], .........,a[n-1] and then interchange a[loc] and a[1] such that a[0], a[1] are sorted.
Space Complexity
• Since no extra space beside n variables is needed for sorting so
• O(n)
21
Insertion Sort
• Like sorting a hand of playing cards start with an empty hand and the cards
facing down the table.
• Pick one card at a time from the table, and insert it into the correct position in
the left hand.
• Compare it with each of the cards already in the hand, from right to left
25
7 2 4 5 1 3
2 7 4 5 1 3
2 4 7 5 1 3
2 4 5 7 1 3
1 2 4 5 7 3
1 2 3 4 5 7
26
Algorithm
7 2 4 1 5 3
InsertionSort(){
for (i=1;i<n;i++){
7 2 4 1 5 3
value=C[ i ];
hole= i ;
2 7 4 1 5 3 while(hole>0 && C[hole-1]>value){
1st Pass
C[hole]=C[hole -1];
hole=hole-1;
}
C[hole]=value;
}
i value hole }
1 2 1
1 2 0
27
Time Complexity
• Best Case:
• If the array is all but sorted then
• Inner Loop wont execute so only some constant time the statements will run
• So Time complexity= O(n)
• Worst Case:
• Array element in reverse sorted order
• Time complexity=O(n2)
• Space Complexity
• Since no extra space beside n variables is needed for sorting so • Space Complexity = O(n)
28
Divide and conquer algorithms
• The sorting algorithms we’ve seen so far have worst-case running times of
O(n2)
• When the size of the input array is large, these algorithms can take a long time
to run.
• Now we will discuss two sorting algorithms whose running times are better
• Merge Sort
• Quick Sort
29
30
Divide-and-conquer
• Divide-and-conquer, breaks a problem into sub problems that are similar to the
original problem, recursively solves the sub problems, and finally combines the
solutions to the sub problems to solve the original problem.
31
Divide-and-conquer
32
Merge Sort
• Merge sort is a sorting technique based on divide and conquer technique.
• Merge sort first divides the array into equal halves and then combines them in
a sorted manner.
• With worst-case time complexity being Ο(n log n), it is one of the most
respected algorithms.
33
Merge Sort
• Because we're using divide-and-conquer to sort, we need to decide what our
sub problems are going to be.
34
35
Merge Sort
• Here’s how merge sort uses divide and conquer
1. Divide by finding the number q of the position midway between p and r. Do
this step the same way we found the midpoint in binary search: add p and r,
divide by 2, and round down.
2. Conquer by recursively sorting the subarrays in each of the two sub problems
created by the divide step. That is, recursively sort the subarray array[p..q]
and recursively sort the subarray array[q+1..r].
3. Combine by merging the two sorted subarrays back into the single sorted
subarray array[p..r].
36
Merge Sort
• Let’s start with array holding [14,7,3,12,9,11,6,2]
• We can say that array[0..7] where p=0 and r=7
• In the divide step we compute q=3
• The conquer step has us sort the two subarrays
• array[0..3] = [14,7,3,12] • array[4..7]= [9,11,6,2]
• When we comeback from the conquer step, each of the two subarrays is sorted i.e.
• array[0..3] = [3,7,12,14]
• array[4..7]= [2,6,9,11]
• Finally, the combine step merges the two sorted subarrays in first half and the second half, producing the final
sorted array [2,3 , 6,7,9, 11, 12,14]
37
How did the subarray array[0..3] become sorted?
• It has more than two element so it’s not a base case.
• So with p=0 and r=3, compute q=1, recursively sort array[0..1] and array[2..3],
resulting in array[0..3] containing [7,14,3,12] and merge the fist half with the
second half, producing [3,7,12,14]
40
Analysis of merge Sort
• Divide and conquer
• Recursive
• Stable
• Not In-place
• 0( n) space complexity
• 0(nlogn) time complexity
Recursive function calls and finally merge Merge function merges the whole block of array
41
void MergeSort(int *A,int n) { void Merge(int *A,int *L,int leftCount,int *R,int
int mid,i, *L, *R; rightCount) {
if(n < 2) return; int i,j,k; i = 0; j
mid = n/2; = 0; k =0;
L = (int*)malloc(mid*sizeof(int)); R = while(i<leftCount && j< rightCount) {
(int*)malloc((n- mid)*sizeof(int)); if(L[i] < R[j]) A[k++] = L[i++] ;
else A[k++] = R[j++] ;
for(i = 0;i<mid;i++) }
L[i] = A[i]; // creating left subarray while(i < leftCount)
for(i = mid;i<n;i++) A[k++] = L[i++] ;
R[i-mid] = A[i]; // creating right subarray while(j < rightCount)
A[k++] = R[j++] ;
MergeSort(L,mid);
}
MergeSort(R,n-mid);
Merge(A,L,mid,R,n-mid); free(L);
free(R);
}
42
Analysis of merge Sort
• The time to merge sort n numbers is equal to the time to do two
recursive merge sorts of size n/2 plus the time to merge, which is linear.
• We can express the number of operations involved using the following
recurrence relations
T(1)=1
T(n) =2T(n/2)+n
• Going further down using the same logic
T(n/2)=2T(n/2)+n/2
• Continuing in this manner, we can write
43
T(n)=nT(1)+nlogn
=n+nlogn
T(n)=0(nlogn)
• Although merge sort’s running time is very attractive it is not preferred
for sorting data in main memory.
• Main problem arises when the it uses linear extra memory as we need
to copy the original array into two arrays of half the size and the
additional work spent on copying to the temporary array and back
44
• However merge sort can work very well with linked list as it doesn’t
require additional space. Since we only need to change pointer links
rather than shifting the element.
45
Quick Sort
• Quick sort is one of the most popular sorting techniques.
• As the name suggests the quick sort is the fastest known sorting algorithm in
practice.
• It works by partitioning the array to be sorted and each partition in turn sorted
recursively. Hence also called partition exchange sort.
46
Quick Sort
• In partition one of the array elements is choses as a pivot element
• The pivot remains at the ith position when the array is completely sorted.
Continuously repeating this process will eventually sort an array.
47
pivot
wall current
6 5 1 3 8 4 7 9 2
current pivot
wall
6 5 1 3 8 4 7 9 2
pivot
wall current
6 5 1 3 8 4 7 9 2
pivot
current
wall
1 5 6 3 8 4 7 9 2
pivot
current
48
wall
1 2 6 3 8 4 7 9 5
Algorithm
• Choosing a pivot
• To partition the list we first choose a pivot element
• Partitioning
• Then we partition the elements so that all those with values less than pivot
are placed on the left side and the higher vale on the right
49
• Check if the current element is less than the pivot.
• If lesser replace it with the current element and move the wall up one position
• else move the pivot element to current element and vice versa
• Recur
• Repeat the same partitioning step unless all elements are sorted
50
Analysis of Quick Sort
• Best case
• The best case analysis assumes that the pivot is always in the middle
• To simplify the math, we assume that the two sublists are each exactly half the size of the
original T(N)=T(N/2)+T(N/2)….+1 leads to T(N)=O(nlogn)
• Average case
• T(N)=O(nlogn)
51
• Worst case
• When we pick minimum or maximum as pivot then we have to go through each and every
element so
• T(N) = O(n2)
52
Heap
• A heap is defined as an almost complete binary tree o nodes n
such that value of each node is less than or equal to the value of
the father OR greater than or equal to the value of the father.
• Ascending heap is an almost complete binary tree in which the value of each
node is greater or equal to the value of its father
53
Heap
• Heap is a special case of balanced binary tree data structure
where root-node key is compared with its children and arranged
accordingly.
54
Heap
• As the value of the parent is greater than that of child, this
property generates MAX heap. Based on this criteria a heap can
be of two types
• Min - Heap
• Max - Heap
55
Heap
• Where the value of the root node is less than or equal to either of its
children • For input 35 33 42 10 14 19 27 44 26 31
• Max-Heap −
56
Heap
• where the value of root node is greater than or equal to either of its
children.
• For input 35 33 42 10 14 19 27 44 26 31
57
Max Heap Construction Algorithm
Step 1 − Create a new node at the end of heap.
Step 2 − Assign new value to the node.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.
58
Max Heap Deletion Algorithm
Step 1 − Remove root node.
Step 2 − Move the last element of last level to root.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.
59
Analysis of Heap Sort
60
• The worse case complexity of the heap sort is O(nlogn), therefore,
Heap sort is superior to quick sort in the worst case
• Heap sort is not very efficient for small array because of the overhead
of initial heap creation and computation.
• The space requirement for the hap sort is only one additional record
to hold the temporary value.
61
Radix Sort
• The idea behind radix sort is slightly more complex than that of
bucket sort.
• Algorithm:
• Take the least significant digit of each element in the unsorted array
• Perform a stable sort based on that key
• Repeat the process sequentially with each more significant digit
62
Radix Sort
• For example suppose we want to sort the list
849,770,67 ,347,201,618,66,495,13, 45
63
• We now consider the hundreds digit to be zero 12 ,45,66,
67,201,347,495,618,770,849
64