SlideShare a Scribd company logo
1 of 75
Debasis Samanta
Computer Science & Engineering
Indian Institute of Technology Kharagpur
Spring-2017
Programming and Data Structures
Lecture #7
Sorting algorithms
Lecture #07: © DSamanta
CS 10001 : Programming and Data Structures 2
*Introduction
*Different sorting algorithms
*Sorting by distribution
*
Today’s Discussion…
CS 10001 : Programming and Data Structures 3 Lecture #07: © DSamanta
Introduction
CS 11001 : Programming and Data Structures 4 Lecture #11: © DSamanta
Sorting – The Task
CS 10001 : Programming and Data Structures 5 Lecture #07: © DSamanta
• Given an array
x[0], x[1], … , x[size-1]
reorder entries so that
x[0] <= x[1] <= . . . <= x[size-1]
Here, List is in non-decreasing order.
• We can also sort a list of elements in non-increasing order.
Sorting – Example
CS 10001 : Programming and Data Structures 6 Lecture #07: © DSamanta
• Original list:
– 10, 30, 20, 80, 70, 10, 60, 40, 70
• Sorted in non-decreasing order:
– 10, 10, 20, 30, 40, 60, 70, 70, 80
• Sorted in non-increasing order:
– 80, 70, 70, 60, 40, 30, 20, 10, 10
Sorting Problem
CS 10001 : Programming and Data Structures 7 Lecture #07: © DSamanta
Unsorted list
• What do we want :
- Data to be sorted in order
x:
0 size-1
Sorted list
Issues in Sorting
CS 10001 : Programming and Data Structures 8 Lecture #07: © DSamanta
Many issues are there in sorting techniques
• How to rearrange a given set of data?
• Which data structures are more suitable to store data prior
to their sorting?
• How fast the sorting can be achieved?
• How sorting can be done in a memory constraint situation?
• How to sort various types of data?
Sorting Algorithms
CS 11001 : Programming and Data Structures 9 Lecture #11: © DSamanta
Sorting by Comparison
CS 10001 : Programming and Data Structures 10 Lecture #07: © DSamanta
• Basic operation involved in this type of sorting
technique is comparison. A data item is compared
with other items in the list of items in order to find
its place in the sorted list.
• Insertion
• Selection
• Exchange
• Enumeration
Sorting by Comparison
CS 10001 : Programming and Data Structures 11 Lecture #07: © DSamanta
Sorting by comparison – Insertion:
• From a given list of items, one item is considered at a time.
The item chosen is then inserted into an appropriate position
relative to the previously sorted items. The item can be
inserted into the same list or to a different list.
e.g.: Insertion sort
Sorting by comparison – Selection:
• First the smallest (or largest) item is located and it is separated
from the rest; then the next smallest (or next largest) is
selected and so on until all item are separated.
e.g.: Selection sort, Heap sort
Sorting by Comparison
CS 10001 : Programming and Data Structures 12 Lecture #07: © DSamanta
Sorting by comparison – Exchange:
• If two items are found to be out of order, they are
interchanged. The process is repeated until no more exchange
is required.
e.g.: Bubble sort, Shell Sort, Quick Sort
Sorting by comparison – Enumeration:
• Two or more input lists are merged into an output list and
while merging the items, an input list is chosen following the
required sorting order.
e.g.: Merge sort
Sorting by Distribution
CS 10001 : Programming and Data Structures 13 Lecture #07: © DSamanta
• No key comparison takes place
• All items under sorting are distributed over an auxiliary storage
space based on the constituent element in each and then grouped
them together to get the sorted list.
• Distributions of items based on the following choices
 Radix - An item is placed in a space decided by the
bases (or radix) of its components with which it is
composed of.
 Counting - Items are sorted based on their relative counts.
 Hashing - Items are hashed, that is, dispersed into a list
based on a hash function.
Note: This lecture concentrates only on sorting by comparison.
Insertion Sort
CS 10001 : Programming and Data Structures 14 Lecture #07: © DSamanta
Insertion Sort
CS 10001 : Programming and Data Structures 15 Lecture #07: © DSamanta
General situation :
0 size-1
i
remainder, unsorted
smallest elements, sorted
0 size-1
i
x:
i
j
Compare and
Shift till x[i] is
larger.
Insertion Sort
CS 10001 : Programming and Data Structures 16 Lecture #07: © DSamanta
void insertionSort (int list[], int size)
{
int i,j,item;
for (i=1; i<size; i++)
{
item = list[i] ;
/* Move elements of list[0..i-1], that are greater than
item, to one position ahead of their current position */
for (j=i-1; (j>=0)&& (list[j] > item); j--)
list[j+1] = list[j];
list[j+1] = item ;
}
}
Insertion Sort
CS 10001 : Programming and Data Structures 17 Lecture #07: © DSamanta
int main()
{
int x[ ]={-45,89,-65,87,0,3,-23,19,56,21,76,-50};
int i;
for(i=0;i<12;i++)
printf("%d ",x[i]);
printf("n");
insertionSort(x,12);
for(i=0;i<12;i++)
printf("%d ",x[i]);
printf("n");
}
OUTPUT
-45 89 -65 87 0 3 -23 19 56 21 76 -50
-65 -50 -45 -23 0 3 19 21 56 76 87 89
Lecture #07: © DSamanta
CS 10001 : Programming and Data Structures 18
Insertion Sort - Example
Insertion Sort: Complexity Analysis
CS 10001 : Programming and Data Structures 19 Lecture #07: © DSamanta
Case 1: If the input list is already in sorted order
Number of comparisons: Number of comparison in each
iteration is 1.
Number of movement: No data movement takes place in any
iteration.
𝑀 𝑛 = 0
𝐶 𝑛 = 1 + 1 + 1 + ⋯ + 1 upto (n-1)th iteration.
Insertion Sort: Complexity analysis
CS 10001 : Programming and Data Structures 20 Lecture #07: © DSamanta
Case 2: If the input list is sorted but in reverse order
Number of comparisons: Number of comparison in each
iteration is 1.
Number of movement: Number of movements takes place in
any ith iteration is i.
𝑀 𝑛 = 1 + 2 + 3 + ⋯ + 𝑛 − 1 =
𝑛(𝑛 − 1)
2
𝐶 𝑛 = 1 + 2 + 3 + ⋯ + 𝑛 − 1 =
𝑛(𝑛 − 1)
2
Insertion Sort: Complexity analysis
CS 10001 : Programming and Data Structures 21 Lecture #07: © DSamanta
Case 3: If the input list is in random order
• Let 𝑝𝑗 be the probability that the key will go to the 𝑗𝑡ℎ location
(1 ≤ 𝑗 ≤ 𝑖 + 1). Then the number of comparisons will be 𝑗 ∙ 𝑝𝑗.
• The average number of comparisons in the (i + 1)𝑡ℎ iteration is
𝐴𝑖+1 =
𝑗=1
𝑖+1
𝑗 ∙ 𝑝𝑗
• Assume that all keys are distinct and all permutations of keys are
equally likely.
𝑝1 = 𝑝2 = 𝑝3 = ⋯ = 𝑝𝑖+1 =
1
𝑖 + 1
Insertion Sort: Complexity analysis
CS 10001 : Programming and Data Structures 22 Lecture #07: © DSamanta
Case 3: Number of comparisons
• Therefore, the average number of comparisons in the (i +
1)𝑡ℎ iteration
𝐴𝑖+1 =
1
𝑖 + 1
𝑗=1
𝑖+1
𝑗 =
1
𝑖 + 1
∙
𝑖 + 1 ∙ 𝑖 + 2
2
=
𝑖 + 2
2
• Total number of comparisons for all (𝑛 − 1) iterations is
𝐶 𝑛 =
𝑖=0
𝑛−1
𝐴𝑖+1 =
1
2
∙
𝑛 𝑛 − 1
2
+ (𝑛 − 1)
Insertion Sort: Complexity analysis
CS 10001 : Programming and Data Structures 23 Lecture #07: © DSamanta
Case 3: Number of Movements
• On the average, number of movements in the 𝑖𝑡ℎ iteration
𝑀𝑖 =
𝑖 + 𝑖 − 1 + 𝑖 − 2 + ⋯ + 2 + 1
𝑖
=
𝑖 + 1
2
• Total number of movements
𝑀 𝑛 =
𝑖=1
𝑛−1
𝑀𝑖 =
1
2
∙
𝑛 𝑛 − 1
2
+
𝑛 − 1
2
Insertion Sort: Summary of Complexity Analysis
CS 10001 : Programming and Data Structures 24 Lecture #07: © DSamanta
Case Comparisons Movement Memory Remarks
Case 1 C 𝑛 = (𝑛 − 1) 𝑀 𝑛 = 0 𝑆 𝑛 = 𝑛 Input list is in sorted
order
Case 2 C 𝑛 =
𝑛(𝑛 − 1)
2
𝑀 𝑛 =
𝑛(𝑛 − 1)
2
𝑆 𝑛 = 𝑛 Input list is sorted in
reverse order
Case 3 C 𝑛 =
(𝑛 − 1)(𝑛 + 4)
4
𝑀 𝑛 =
(𝑛 − 1)(𝑛 + 2)
4
𝑆 𝑛 = 𝑛 Input list is in random
order
Case Run time, 𝑻(𝒏) Complexity Remarks
Case 1 𝑇 𝑛 = 𝑐 (𝑛 − 1) 𝑇 𝑛 = 𝑂(𝑛)
Best case
Case 2 𝑇 𝑛 = 𝑐 𝑛(𝑛 − 1) 𝑇 𝑛 = 𝑂(𝑛2
) Worst case
Case 3 𝑇 𝑛 = 𝑐
(𝑛 − 1)(𝑛 + 3)
2
𝑇 𝑛 = 𝑂(𝑛2
)
Average case
Selection Sort
CS 10001 : Programming and Data Structures 25 Lecture #07: © DSamanta
Selection Sort
CS 10001 : Programming and Data Structures 26 Lecture #07: © DSamanta
General situation :
remainder, unsorted
smallest elements, sorted
0 size-1
k
x:
Steps :
• Find smallest element, mval, in x[k…size-1]
• Swap smallest element with x[k], then increase k.
0 k size-1
mval
swap
x:
Selection Sort
CS 10001 : Programming and Data Structures 27 Lecture #07: © DSamanta
/* Yield location of smallest element in
x[k .. size-1];*/
int findMinLloc (int x[ ], int k, int size)
{
int j, pos; /* x[pos] is the smallest
element found so far */
pos = k;
for (j=k+1; j<size; j++)
if (x[j] < x[pos])
pos = j;
return pos;
}
Selection Sort
CS 10001 : Programming and Data Structures 28 Lecture #07: © DSamanta
/* The main sorting function */
/* Sort x[0..size-1] in non-decreasing order */
int selectionSort (int x[], int size)
{ int k, m;
for (k=0; k<size-1; k++)
{
m = findMinLoc(x, k, size);
temp = a[k];
a[k] = a[m];
a[m] = temp;
}
}
Selection Sort - Example
CS 10001 : Programming and Data Structures 29 Lecture #07: © DSamanta
-17 12 -5 6 142 21 3 45
x:
3 12 -5 6 142 21 -17 45
x:
-17 -5 12 6 142 21 3 45
x:
-17 -5 3 6 142 21 12 45
x:
-17 -5 3 6 142 21 12 45
x:
-17 -5 3 6 12 21 142 45
x:
-17 -5 3 6 12 21 45 142
x:
-17 -5 3 6 21 142 45
x: 12
-17 -5 3 6 12 21 45 142
x:
Selection Sort: Complexity Analysis
CS 10001 : Programming and Data Structures 30 Lecture #07: © DSamanta
Case 1: If the input list is already in sorted order
Number of comparisons:
𝐶 𝑛 =
𝑖=1
𝑛−1
𝑛 − 𝑖 =
𝑛(𝑛 − 1)
2
Number of movement: no data movement takes place in any
iteration.
𝑀 𝑛 = 0
Selection Sort: Complexity Analysis
CS 10001 : Programming and Data Structures 31 Lecture #07: © DSamanta
Case 2: If the input list is sorted but in reverse order
Number of comparisons:
Number of movements:
𝑀 𝑛 =
3
2
(𝑛 − 1)
𝑐 𝑛 =
𝑖=1
𝑛−1
𝑛 − 𝑖 =
𝑛(𝑛 − 1)
2
Selection Sort: Complexity Analysis
CS 10001 : Programming and Data Structures 32 Lecture #07: © DSamanta
Case 3: If the input list is in random order
Number of comparisons:
𝐶 𝑛 =
𝑛(𝑛 − 1)
2
• Let 𝑝𝑖 be the probability that the 𝑖𝑡ℎ smallest element is in the 𝑖𝑡ℎ
position. Number of total swap operations = (1 − 𝑝𝑖) × (𝑛 − 1)
where 𝑝1 = 𝑝2 = 𝑝3 = ⋯ = 𝑝𝑛 =
1
𝑛
• Total number of movements
𝑀 𝑛 = 1 −
1
𝑛
× 𝑛 − 1 × 3 =
3(𝑛 − 1)(𝑛 − 1)
𝑛
Selection Sort: Summary of Complexity analysis
CS 10001 : Programming and Data Structures 33 Lecture #07: © DSamanta
Case Comparisons Movement Memory Remarks
Case 1 𝑐 𝑛 =
𝑛(𝑛 − 1)
2
𝑀 𝑛 = 0 𝑆 𝑛 = 0 Input list is in sorted
order
Case 2 𝑐 𝑛 =
𝑛(𝑛 − 1)
2
𝑀 𝑛 =
3(𝑛 − 1)
2
𝑆 𝑛 = 0 Input list is sorted in
reverse order
Case 3 𝑐 𝑛 =
𝑛(𝑛 − 1)
2
𝑀 𝑛 =
3 𝑛 − 1 2
𝑛
𝑆 𝑛 = 0 Input list is in random
order
Case Run time, 𝑻(𝒏) Complexity Remarks
Case 1 𝑇 𝑛 =
𝑛(𝑛 − 1)
2
𝑇 𝑛 = 𝑂(𝑛2
)
Best case
Case 2 𝑇 𝑛 =
(𝑛 − 1)(𝑛 + 3)
2
𝑇 𝑛 = 𝑂(𝑛2
)
Worst case
Case 3 𝑇 𝑛 ≈
(𝑛 − 1)(2𝑛 + 3)
2
(Taking 𝑛 − 1 ≈ 𝑛)
𝑇 𝑛 = 𝑂(𝑛2
)
Average case
Bubble Sort
CS 10001 : Programming and Data Structures 34 Lecture #07: © DSamanta
Bubble Sort
CS 10001 : Programming and Data Structures 35 Lecture #07: © DSamanta
The sorting process proceeds in
several passes.
• In every pass we go on
comparing neighbouring pairs,
and swap them if out of order.
• In every pass, the largest of the
elements under considering
will bubble to the top (i.e., the
right).
In every iteration
heaviest element drops
at the bottom.
The bottom
moves upward.
Bubble Sort
CS 10001 : Programming and Data Structures 36 Lecture #07: © DSamanta
How the passes proceed?
• In pass 1, we consider index 0 to n-1.
• In pass 2, we consider index 0 to n-2.
• In pass 3, we consider index 0 to n-3.
• ……
• ……
• In pass n-1, we consider index 0 to 1.
Bubble Sort - Example
CS 10001 : Programming and Data Structures 37 Lecture #07: © DSamanta
3 12 -5 6 72 21 -7 45
x:
Pass: 1
3 12 -5 6 72 21 -7 45
x:
3 -5 12 6 72 21 -7 45
x:
3 -5 6 12 72 21 -7 45
x:
3 -5 6 12 72 21 -7 45
x:
3 -5 6 12 21 72 -7 45
x:
3 -5 6 12 21 -7 72 45
x:
3 -5 6 12 21 -7 45 72
x:
Bubble Sort - Example
CS 10001 : Programming and Data Structures 38 Lecture #07: © DSamanta
3 -5 6 12 21 -7 45 72
x:
Pass: 2
-5 3 6 12 21 -7 45 72
x:
-5 3 6 12 21 -7 45 72
x:
-5 3 6 12 21 -7 45 72
x:
-5 3 6 12 21 -7 45 72
x:
-5 3 6 12 -7 21 45 72
x:
-5 3 6 12 -7 21 45 72
x:
Bubble Sort
CS 10001 : Programming and Data Structures 39 Lecture #07: © DSamanta
void swap(int *x, int *y)
{
int tmp = *x;
*x = *y;
*y = tmp;
}
void bubble_sort(int x[], int n)
{
int i,j;
for (i=n-1; i>0; i--)
for (j=0; j<i; j++)
if (x[j] > x[j+1])
swap(&x[j],&x[j+1]);
}
Bubble Sort
CS 10001 : Programming and Data Structures 40 Lecture #07: © DSamanta
int main()
{
int x[ ]={-45,89,-65,87,0,3,-23,19,56,21,76,-50};
int i;
for(i=0;i<12;i++)
printf("%d ",x[i]);
printf("n");
bubble_sort(x,12);
for(i=0;i<12;i++)
printf("%d ",x[i]);
printf("n");
}
OUTPUT
-45 89 -65 87 0 3 -23 19 56 21 76 -50
-65 -50 -45 -23 0 3 19 21 56 76 87 89
Bubble Sort: Complexity analysis
CS 10001 : Programming and Data Structures 41 Lecture #07: © DSamanta
Case 1: If the input list is already in sorted order
Number of comparisons:
𝐶 𝑛 =
𝑛(𝑛 − 1)
2
Number of movements:
𝑀 𝑛 = 0
Bubble Sort: Complexity analysis
CS 10001 : Programming and Data Structures 42 Lecture #07: © DSamanta
Case 2: If the input list is sorted but in reverse order
Number of comparisons:
Number of movements:
𝑀 𝑛 =
𝑛(𝑛 − 1)
2
𝑐 𝑛 =
𝑛(𝑛 − 1)
2
Bubble Sort: Complexity analysis
CS 10001 : Programming and Data Structures 43 Lecture #07: © DSamanta
Case 3: If the input list is in random order
Number of comparisons:
𝐶 𝑛 =
𝑛(𝑛 − 1)
2
Number of movements:
• Let 𝑝𝑗 be the probability that the largest element is in the unsorted
part is in 𝑗𝑡ℎ (1 ≤ 𝑗 ≤ 𝑛 − 𝑖 + 1) location.
• The average number of swaps in the 𝑖𝑡ℎ pass is
=
𝑗=1
𝑛−𝑖+1
𝑛 − 𝑖 + 1 − 𝑗 ∙ 𝑝𝑗
Bubble Sort: Complexity analysis
CS 10001 : Programming and Data Structures 44 Lecture #07: © DSamanta
Case 3: If the input list is in random order
Number of movements:
• 𝑝1 = 𝑝2 = 𝑝𝑛−𝑖+1 =
1
𝑛−𝑖+1
• Therefore, the average number of swaps in the 𝑖𝑡ℎ pass is
=
𝑗=1
𝑛−𝑖+1
1
𝑛 − 𝑖 + 1
∙ 𝑛 − 𝑖 + 1 − 𝑗 =
𝑛 − 𝑖
2
• The average number of movements
𝑀(𝑛) =
𝑖=1
𝑛−1
𝑛 − 𝑖
2
=
𝑛(𝑛 − 1)
4
Bubble Sort: Summary of Complexity analysis
CS 10001 : Programming and Data Structures 45 Lecture #07: © DSamanta
Case Comparisons Movement Memory Remarks
Case 1 𝑐 𝑛 =
𝑛(𝑛 − 1)
2
𝑀 𝑛 = 0 𝑆 𝑛 = 0 Input list is in sorted
order
Case 2 𝑐 𝑛 =
𝑛(𝑛 − 1)
2
𝑀 𝑛 =
𝑛(𝑛 − 1)
2
𝑆 𝑛 = 0 Input list is sorted in
reverse order
Case 3 𝑐 𝑛 =
𝑛(𝑛 − 1)
2
𝑀 𝑛 =
𝑛(𝑛 − 1)
4
𝑆 𝑛 = 0 Input list is in random
order
Case Run time, 𝑻(𝒏) Complexity Remarks
Case 1 𝑇 𝑛 = 𝑐
𝑛(𝑛 − 1)
2
𝑇 𝑛 = 𝑂(𝑛2
)
Best case
Case 2 𝑇 𝑛 = 𝑐𝑛 𝑛 − 1 𝑇 𝑛 = 𝑂(𝑛2
) Worst case
Case 3 𝑇 𝑛 =
3
4
𝑛(𝑛 − 1)
𝑇 𝑛 = 𝑂(𝑛2
)
Average case
Bubble Sort
CS 10001 : Programming and Data Structures 46 Lecture #07: © DSamanta
How do you make best case with (n-1)
comparisons only?
• By maintaining a variable flag, to check if
there has been any swaps in a given pass.
• If not, the array is already sorted.
Bubble Sort
CS 10001 : Programming and Data Structures 47 Lecture #07: © DSamanta
void bubble_sort(int x[], int n)
{
int i,j;
int flag = 0;
for (i=n-1; i>0; i--)
{
for (j=0; j<i; j++)
if (x[j] > x[j+1])
{
swap(&x[j],&x[j+1]);
flag = 1;
}
if (flag == 0) return;
}
}
Efficient Sorting algorithms
CS 10001 : Programming and Data Structures 48 Lecture #07: © DSamanta
Two of the most popular sorting algorithms are based on
divide-and-conquer approach.
• Quick sort
• Merge sort
sort (list)
{
if the list has length greater than 1
{
Partition the list into lowlist and highlist;
sort (lowlist);
sort (highlist);
combine (lowlist, highlist);
}
}
Basic concept of divide-and-conquer method:
Quick Sort
CS 10001 : Programming and Data Structures 49 Lecture #07: © DSamanta
Quick Sort – How it Works?
CS 10001 : Programming and Data Structures 50 Lecture #07: © DSamanta
At every step, we select a pivot element in the list
(usually the first element).
• We put the pivot element in the final position of the sorted
list.
• All the elements less than or equal to the pivot element are
to the left.
• All the elements greater than the pivot element are to the
right.
Quick Sort Partitioning
CS 10001 : Programming and Data Structures 51 Lecture #07: © DSamanta
0 size-1
x:
pivot
Values smaller Values greater
Perform
partitioning
Perform
partitioning
Quick Sort
CS 10001 : Programming and Data Structures 52 Lecture #07: © DSamanta
#include <stdio.h>
void quickSort( int[], int, int);
int partition( int[], int, int);
void main()
{
int i,a[] = { 7, 12, 1, -2, 0, 15, 4, 11, 9};
printf("nnUnsorted array is: ");
for(i = 0; i < 9; ++i)
printf(" %d ", a[i]);
quickSort( a, 0, 8);
printf("nnSorted array is: ");
for(i = 0; i < 9; ++i)
printf(" %d ", a[i]);
}
void quickSort( int a[], int l, int r)
{
int j;
if( l < r ) { // divide and conquer
j = partition( a, l, r);
quickSort( a, l, j-1);
quickSort( a, j+1, r);
}
}
Quick Sort
CS 10001 : Programming and Data Structures 53 Lecture #07: © DSamanta
int partition( int a[], int l, int r)
{
int pivot, i, j, t;
pivot = a[l];
i = l;
j = r+1;
while( 1) {
do {
++i;
} while(a[i]<=pivot && i<=r);
do {
--j;
} while( a[j] > pivot );
if( i >= j ) break;
t = a[i];
a[i] = a[j];
a[j] = t;
}
t = a[l];
a[l] = a[j];
a[j] = t;
return j;
}
Lecture #07: © DSamanta
CS 10001 : Programming and Data Structures 54
Input: 45 -56 78 90 -3 -6 123 0 -3 45 69 68
Output: -56 -6 -3 -3 0 45 45 68 69 78 90 123
45 -56 78 90 -3 -6 123 0 -3 45 69 68
-6 -56 -3 0 -3 45 123 90 78 45 69 68
-3 0 -3
-6
-56
0 -3
-3
-3 0
68 90 78 45 69 123
78 90 69
68
45
78
69 90
Quick Sort - Example
Quick Sort: Complexity analysis
CS 10001 : Programming and Data Structures 55 Lecture #07: © DSamanta
Memory requirement:
Size of the stack is:
𝑆 𝑛 = 𝑙𝑜𝑔2 𝑛 + 1
Number of comparisons:
• Let, 𝑇(𝑛) represents total time to sort n elements and
𝑃(𝑛) represents the time for perform a partition of a list of
𝑛 elements.
𝑇 𝑛 = 𝑃 𝑛 + 𝑇 𝑛𝑙 + 𝑇 𝑛𝑟 , with 𝑇 1 = 𝑇 0 = 0
where, 𝑛𝑙 = number of elements in the left sub list
𝑛𝑟 = number of elements in the right sub list and 0 ≤ 𝑛𝑙, 𝑛𝑟 < 𝑛
Quick Sort: Complexity analysis
CS 10001 : Programming and Data Structures 56 Lecture #07: © DSamanta
Case 1: Elements in the list are in ascending order
Number of comparisons:
𝐶 𝑛 = 𝑛 − 1 + 𝐶 𝑛 − 1 , with 𝐶 1 = 𝐶 0 = 0
𝐶 𝑛 = 𝑛 − 1 + 𝑛 − 2 + 𝑛 − 3 + ⋯ + 2 + 1
𝐶 𝑛 =
𝑛(𝑛 − 1)
2
Number of movements:
𝑀 𝑛 = 0
Quick Sort: Complexity analysis
CS 10001 : Programming and Data Structures 57 Lecture #07: © DSamanta
Case 2: Elements in the list are in reverse order
Number of comparisons:
Number of movements:
𝑀 𝑛 =
𝑛 − 1
2
, 𝑖𝑓 𝑛 𝑖𝑠 𝑜𝑑𝑑
𝑛
2
, 𝑖𝑓 𝑛 𝑖𝑠 𝑒𝑣𝑒𝑛
𝐶 𝑛 = 𝑛 − 1 + 𝐶 𝑛 − 1 , with 𝐶 1 = 𝐶 0 = 0
𝐶 𝑛 = 𝑛 − 1 + 𝑛 − 2 + 𝑛 − 3 + ⋯ + 2 + 1
𝐶 𝑛 =
𝑛(𝑛 − 1)
2
Quick Sort: Complexity analysis
CS 10001 : Programming and Data Structures 58 Lecture #07: © DSamanta
Case 3: Elements in the list are in random order
Number of comparisons:
𝐶 𝑛 = 𝑛 − 1 +
𝑖=1
𝑛−1
1
𝑛
𝐶 𝑖 − 1 + 𝐶(𝑛 − 1) 𝑤𝑖𝑡ℎ 𝐶 1 = 𝐶 0 = 0
𝐶 𝑛 = 𝑛 − 1 +
2
𝑛
𝑖=1
𝑛−1
𝐶(𝑖)
𝐶 𝑛 = 2 𝑛 + 1 𝑙𝑜𝑔𝑒𝑛 + 0.577 − 4𝑛
(i-1) (n-1)
𝒊𝒕𝒉 location
Quick Sort: Complexity analysis
CS 10001 : Programming and Data Structures 59 Lecture #07: © DSamanta
Case 3: Elements in the list are in random order
Number of movements:
𝑀 𝑛 =
1
𝑛
𝑖=1
𝑛
𝑖 − 1 + 𝑀 𝑖 − 1 + 𝑀(𝑛 − 𝑖)
𝑀 𝑛 =
𝑛 − 1
2
+
2
𝑛
𝑖=1
𝑛−1
𝑀(𝑖)
𝑀 𝑛 = 2 𝑛 + 1 𝑙𝑜𝑔𝑒𝑛 + 0.577 − 4𝑛
Quick Sort: Summary of Complexity analysis
CS 10001 : Programming and Data Structures 60 Lecture #07: © DSamanta
Case Comparisons Movement Memory Remarks
Case 1
𝑐 𝑛 =
𝑛(𝑛 − 1)
2
𝑀 𝑛 = 0 𝑆 𝑛 = 1 Input list is in sorted
order
Case 2
𝑐 𝑛 =
𝑛(𝑛 − 1)
2
𝑀 𝑛 =
𝑛
2
𝑆 𝑛 = 1 Input list is sorted in
reverse order
Case 3 𝐶 𝑛
= 2 𝑛 + 1 𝑙𝑜𝑔𝑒𝑛 + 0.577
− 4𝑛
𝑀 𝑛
= 2 𝑛 + 1 (𝑙𝑜𝑔𝑒𝑛
𝑆 𝑛
= 𝑙𝑜𝑔2 𝑛 + 1
Input list is in
random order
Case Run time, 𝑻(𝒏) Complexity Remarks
Case 1 𝑇 𝑛 = 𝑐
𝑛(𝑛 − 1)
2
𝑇 𝑛 = 𝑂(𝑛2
)
Worst case
Case 2
𝑇 𝑛 = 𝑐
𝑛(𝑛 − 1)
2
+
𝑛
2
𝑇 𝑛 = 𝑂(𝑛2
)
Worst case
Case 3 𝑇 𝑛 = 4𝑐 𝑛 + 1 𝑙𝑜𝑔𝑒𝑛 + 0.577 − 8𝑐𝑛
𝑇 𝑛 = 2𝑐 𝑛𝑙𝑜𝑔2𝑛 − 𝑛 + 1
𝑇 𝑛 = 𝑂(𝑛𝑙𝑜𝑔2𝑛) Best / Average
case
Merge Sort
CS 10001 : Programming and Data Structures 61 Lecture #07: © DSamanta
Merge Sort – How it Works?
CS 10001 : Programming and Data Structures 62 Lecture #07: © DSamanta
Input Array
Part-I Part-II
Part-I Part-II
Part-I Part-II
Split
Merge
Sorted arrays
Merging two Sorted arrays
CS 10001 : Programming and Data Structures 63 Lecture #07: © DSamanta
0
Sorted Array Sorted Array
0
l m
a: b:
Merged sorted array
0 l+m-1
c:
𝑷𝒂 𝑷𝒃
Move and copy elements pointed by 𝑃𝑎 if its value is smaller
than the element pointed by 𝑃𝑏 in (𝑙 + 𝑚 − 1) operations and otherwise.
Merge Sort – Example
CS 10001 : Programming and Data Structures 64 Lecture #07: © DSamanta
Merging two
sorted arrays
Splitting arrays
3 12 -5 6 72 21 -7 45
x:
3 12 -5 6 72 21 -7 45
3 12 -5 6 72 21 -7 45
3 12 -5 6 72 21 -7 45
3 12 -5 6 21 72 -7 45
-5 3 6 12 -7 21 45 72
-7 -5 3
-7 -5 3 6 12 21 45 72
Merge Sort Program
CS 10001 : Programming and Data Structures 65 Lecture #07: © DSamanta
#include<stdio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("nSorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
Merge Sort Program
CS 10001 : Programming and Data Structures 66 Lecture #07: © DSamanta
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j) {
mid=(i+j)/2;
/* left recursion */
mergesort(a,i,mid);
/* right recursion */
mergesort(a,mid+1,j);
/* merging of two sorted sub-arrays */
merge(a,i,mid,mid+1,j);
}
}
Merge Sort Program
CS 10001 : Programming and Data Structures 67 Lecture #07: © DSamanta
void merge(int a[],int i1,int i2,int j1,int j2)
{
int temp[50]; //array used for merging
int i=i1,j=j1,k=0;
while(i<=i2 && j<=j2) //while elements in both lists
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=i2) //copy remaining elements of the first list
temp[k++]=a[i++];
while(j<=j2) //copy remaining elements of the second list
temp[k++]=a[j++];
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j]; //Transfer elements from temp[] back to a[]
}
Merge Sort – Splitting Trace
CS 10001 : Programming and Data Structures 68 Lecture #07: © DSamanta
-56 23 43 -5 -3 0 123 -35 87 56 75 80
Output: -56 -35 -5 -3 0 23 43 56 75 80 87 123
-56 23 43
23 43
-56 23 43 -5 -3 0 123 -35 87 56 75 80
-56 23 43 -5 -3 0
-3 0
-3 0
123 -35 87
-35 87
123 -35 87 56 75 80
56 75 80
75 80
-5
Worst Case: O(n.log(n))
Space Complexity??
Merge Sort: Complexity analysis
CS 10001 : Programming and Data Structures 69 Lecture #07: © DSamanta
Time Complexity:
𝑇 𝑛 = 𝐷 𝑛 + 𝑇
𝑛
2
+ 𝑇
𝑛
2
+ 𝐶 𝑛 𝑖𝑓 𝑛 > 1
𝑇 𝑛 = 𝑐1 𝑖𝑓 𝑛 = 1
• For simplicity of calculation
𝑇
𝑛
2
= 𝑇
𝑛
2
= 𝑇
𝑛
2
𝑇 𝑛 = 𝑐1 𝑖𝑓 𝑛 = 1
𝑇 𝑛 = 𝑐2 + 2𝑇
𝑛
2
+ 𝑛 − 1 𝑖𝑓 𝑛 > 1
𝑇 𝑛 = 𝑛 ∙ 𝑐1 + 𝑛𝑙𝑜𝑔2𝑛 + 𝑐2 − 1 𝑛 − 1 𝐴𝑠𝑠𝑢𝑚𝑖𝑛𝑔 𝑛 =
2𝑘
Quick Sort vs. Merge Sort
CS 10001 : Programming and Data Structures 70 Lecture #07: © DSamanta
• Quick sort
• hard division, easy combination
• partition in the divide step of the divide-and-conquer
framework
• hence combine step does nothing
• Merge sort
• easy division, hard combination
• merge in the combine step
• the divide step in this framework does one simple
calculation only
Quick Sort vs. Merge Sort
CS 10001 : Programming and Data Structures 71 Lecture #07: © DSamanta
Both the algorithms divide the problem into two sub
problems.
• Merge sort:
– two sub problems are of almost equal size always.
• Quick sort:
– an equal sub division is not guaranteed.
• This difference between the two sorting methods appears as
the deciding factor of their run time performances.
Any question?
You may post your question(s) at the “Discussion Forum”
maintained in the course Web page.
CS 10001 : Programming and Data Structures 72 Lecture #07: © DSamanta
Problems to Ponder…
CS 10001 : Programming and Data Structures 73 Lecture #07: © DSamanta
Problems for Practice…
CS 10001 : Programming and Data Structures 74 Lecture #07: © DSamanta
*You can check the Moodle course management system for a
set of problems for your own practice.
• Login to the Moodle system at http://cse.iitkgp.ac.in/
• Select “PDS Spring-2017 (Theory) in the link “My Courses”
• Go to Topic 7: Practice Sheet #07 : Pointer in C
*Solutions to the problems in Practice Sheet #07 will be uploaded
in due time.
Lecture #07: © DSamanta
CS 10001 : Programming and Data Structures 75
If you try to solve problems yourself, then you will learn
many things automatically.
Spend few minutes and then enjoy the study.

More Related Content

Similar to 12 Sorting Techniques.pptx

Size Measurement and Estimation
Size Measurement and EstimationSize Measurement and Estimation
Size Measurement and EstimationLouis A. Poulin
 
DATA STRUCTURES unit 1.pptx
DATA STRUCTURES unit 1.pptxDATA STRUCTURES unit 1.pptx
DATA STRUCTURES unit 1.pptxShivamKrPathak
 
Principal Components Analysis, Calculation and Visualization
Principal Components Analysis, Calculation and VisualizationPrincipal Components Analysis, Calculation and Visualization
Principal Components Analysis, Calculation and VisualizationMarjan Sterjev
 
Size measurement and estimation
Size measurement and estimationSize measurement and estimation
Size measurement and estimationLouis A. Poulin
 
Data Augmentation and Disaggregation by Neal Fultz
Data Augmentation and Disaggregation by Neal FultzData Augmentation and Disaggregation by Neal Fultz
Data Augmentation and Disaggregation by Neal FultzData Con LA
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applicationsyazad dumasia
 
Data Structures unit I Introduction - data types
Data Structures unit I Introduction - data typesData Structures unit I Introduction - data types
Data Structures unit I Introduction - data typesAmirthaVarshini80
 
Aaa ped-17-Unsupervised Learning: Dimensionality reduction
Aaa ped-17-Unsupervised Learning: Dimensionality reductionAaa ped-17-Unsupervised Learning: Dimensionality reduction
Aaa ped-17-Unsupervised Learning: Dimensionality reductionAminaRepo
 
2.03.Asymptotic_analysis.pptx
2.03.Asymptotic_analysis.pptx2.03.Asymptotic_analysis.pptx
2.03.Asymptotic_analysis.pptxssuser1fb3df
 
Dimensionality Reduction and feature extraction.pptx
Dimensionality Reduction and feature extraction.pptxDimensionality Reduction and feature extraction.pptx
Dimensionality Reduction and feature extraction.pptxSivam Chinna
 
Logic Circuits Design - "Chapter 1: Digital Systems and Information"
Logic Circuits Design - "Chapter 1: Digital Systems and Information"Logic Circuits Design - "Chapter 1: Digital Systems and Information"
Logic Circuits Design - "Chapter 1: Digital Systems and Information"Ra'Fat Al-Msie'deen
 
Illustrative Introductory Neural Networks
Illustrative Introductory Neural NetworksIllustrative Introductory Neural Networks
Illustrative Introductory Neural NetworksYasutoTamura1
 
datastructure concepts ppt-190327174340.pptx
datastructure concepts ppt-190327174340.pptxdatastructure concepts ppt-190327174340.pptx
datastructure concepts ppt-190327174340.pptxJeevaMCSEKIOT
 
learned optimizer.pptx
learned optimizer.pptxlearned optimizer.pptx
learned optimizer.pptxQingsong Guo
 
Module 1 number systems and code1
Module 1  number systems and code1Module 1  number systems and code1
Module 1 number systems and code1Deepak John
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureRai University
 

Similar to 12 Sorting Techniques.pptx (20)

Size Measurement and Estimation
Size Measurement and EstimationSize Measurement and Estimation
Size Measurement and Estimation
 
DATA STRUCTURES unit 1.pptx
DATA STRUCTURES unit 1.pptxDATA STRUCTURES unit 1.pptx
DATA STRUCTURES unit 1.pptx
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Principal Components Analysis, Calculation and Visualization
Principal Components Analysis, Calculation and VisualizationPrincipal Components Analysis, Calculation and Visualization
Principal Components Analysis, Calculation and Visualization
 
Size measurement and estimation
Size measurement and estimationSize measurement and estimation
Size measurement and estimation
 
Data Augmentation and Disaggregation by Neal Fultz
Data Augmentation and Disaggregation by Neal FultzData Augmentation and Disaggregation by Neal Fultz
Data Augmentation and Disaggregation by Neal Fultz
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applications
 
Chapter two
Chapter twoChapter two
Chapter two
 
Data Structures unit I Introduction - data types
Data Structures unit I Introduction - data typesData Structures unit I Introduction - data types
Data Structures unit I Introduction - data types
 
Aaa ped-17-Unsupervised Learning: Dimensionality reduction
Aaa ped-17-Unsupervised Learning: Dimensionality reductionAaa ped-17-Unsupervised Learning: Dimensionality reduction
Aaa ped-17-Unsupervised Learning: Dimensionality reduction
 
2.03.Asymptotic_analysis.pptx
2.03.Asymptotic_analysis.pptx2.03.Asymptotic_analysis.pptx
2.03.Asymptotic_analysis.pptx
 
Dimensionality Reduction and feature extraction.pptx
Dimensionality Reduction and feature extraction.pptxDimensionality Reduction and feature extraction.pptx
Dimensionality Reduction and feature extraction.pptx
 
Logic Circuits Design - "Chapter 1: Digital Systems and Information"
Logic Circuits Design - "Chapter 1: Digital Systems and Information"Logic Circuits Design - "Chapter 1: Digital Systems and Information"
Logic Circuits Design - "Chapter 1: Digital Systems and Information"
 
Illustrative Introductory Neural Networks
Illustrative Introductory Neural NetworksIllustrative Introductory Neural Networks
Illustrative Introductory Neural Networks
 
datastructure concepts ppt-190327174340.pptx
datastructure concepts ppt-190327174340.pptxdatastructure concepts ppt-190327174340.pptx
datastructure concepts ppt-190327174340.pptx
 
Ada notes
Ada notesAda notes
Ada notes
 
learned optimizer.pptx
learned optimizer.pptxlearned optimizer.pptx
learned optimizer.pptx
 
Project PPT
Project PPTProject PPT
Project PPT
 
Module 1 number systems and code1
Module 1  number systems and code1Module 1  number systems and code1
Module 1 number systems and code1
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structure
 

Recently uploaded

毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxFurkanTasci3
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一F sss
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxStephen266013
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改yuu sss
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Cantervoginip
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDRafezzaman
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...limedy534
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Callshivangimorya083
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 

Recently uploaded (20)

毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptx
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docx
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Canter
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 

12 Sorting Techniques.pptx

  • 1. Debasis Samanta Computer Science & Engineering Indian Institute of Technology Kharagpur Spring-2017 Programming and Data Structures
  • 2. Lecture #7 Sorting algorithms Lecture #07: © DSamanta CS 10001 : Programming and Data Structures 2
  • 3. *Introduction *Different sorting algorithms *Sorting by distribution * Today’s Discussion… CS 10001 : Programming and Data Structures 3 Lecture #07: © DSamanta
  • 4. Introduction CS 11001 : Programming and Data Structures 4 Lecture #11: © DSamanta
  • 5. Sorting – The Task CS 10001 : Programming and Data Structures 5 Lecture #07: © DSamanta • Given an array x[0], x[1], … , x[size-1] reorder entries so that x[0] <= x[1] <= . . . <= x[size-1] Here, List is in non-decreasing order. • We can also sort a list of elements in non-increasing order.
  • 6. Sorting – Example CS 10001 : Programming and Data Structures 6 Lecture #07: © DSamanta • Original list: – 10, 30, 20, 80, 70, 10, 60, 40, 70 • Sorted in non-decreasing order: – 10, 10, 20, 30, 40, 60, 70, 70, 80 • Sorted in non-increasing order: – 80, 70, 70, 60, 40, 30, 20, 10, 10
  • 7. Sorting Problem CS 10001 : Programming and Data Structures 7 Lecture #07: © DSamanta Unsorted list • What do we want : - Data to be sorted in order x: 0 size-1 Sorted list
  • 8. Issues in Sorting CS 10001 : Programming and Data Structures 8 Lecture #07: © DSamanta Many issues are there in sorting techniques • How to rearrange a given set of data? • Which data structures are more suitable to store data prior to their sorting? • How fast the sorting can be achieved? • How sorting can be done in a memory constraint situation? • How to sort various types of data?
  • 9. Sorting Algorithms CS 11001 : Programming and Data Structures 9 Lecture #11: © DSamanta
  • 10. Sorting by Comparison CS 10001 : Programming and Data Structures 10 Lecture #07: © DSamanta • Basic operation involved in this type of sorting technique is comparison. A data item is compared with other items in the list of items in order to find its place in the sorted list. • Insertion • Selection • Exchange • Enumeration
  • 11. Sorting by Comparison CS 10001 : Programming and Data Structures 11 Lecture #07: © DSamanta Sorting by comparison – Insertion: • From a given list of items, one item is considered at a time. The item chosen is then inserted into an appropriate position relative to the previously sorted items. The item can be inserted into the same list or to a different list. e.g.: Insertion sort Sorting by comparison – Selection: • First the smallest (or largest) item is located and it is separated from the rest; then the next smallest (or next largest) is selected and so on until all item are separated. e.g.: Selection sort, Heap sort
  • 12. Sorting by Comparison CS 10001 : Programming and Data Structures 12 Lecture #07: © DSamanta Sorting by comparison – Exchange: • If two items are found to be out of order, they are interchanged. The process is repeated until no more exchange is required. e.g.: Bubble sort, Shell Sort, Quick Sort Sorting by comparison – Enumeration: • Two or more input lists are merged into an output list and while merging the items, an input list is chosen following the required sorting order. e.g.: Merge sort
  • 13. Sorting by Distribution CS 10001 : Programming and Data Structures 13 Lecture #07: © DSamanta • No key comparison takes place • All items under sorting are distributed over an auxiliary storage space based on the constituent element in each and then grouped them together to get the sorted list. • Distributions of items based on the following choices  Radix - An item is placed in a space decided by the bases (or radix) of its components with which it is composed of.  Counting - Items are sorted based on their relative counts.  Hashing - Items are hashed, that is, dispersed into a list based on a hash function. Note: This lecture concentrates only on sorting by comparison.
  • 14. Insertion Sort CS 10001 : Programming and Data Structures 14 Lecture #07: © DSamanta
  • 15. Insertion Sort CS 10001 : Programming and Data Structures 15 Lecture #07: © DSamanta General situation : 0 size-1 i remainder, unsorted smallest elements, sorted 0 size-1 i x: i j Compare and Shift till x[i] is larger.
  • 16. Insertion Sort CS 10001 : Programming and Data Structures 16 Lecture #07: © DSamanta void insertionSort (int list[], int size) { int i,j,item; for (i=1; i<size; i++) { item = list[i] ; /* Move elements of list[0..i-1], that are greater than item, to one position ahead of their current position */ for (j=i-1; (j>=0)&& (list[j] > item); j--) list[j+1] = list[j]; list[j+1] = item ; } }
  • 17. Insertion Sort CS 10001 : Programming and Data Structures 17 Lecture #07: © DSamanta int main() { int x[ ]={-45,89,-65,87,0,3,-23,19,56,21,76,-50}; int i; for(i=0;i<12;i++) printf("%d ",x[i]); printf("n"); insertionSort(x,12); for(i=0;i<12;i++) printf("%d ",x[i]); printf("n"); } OUTPUT -45 89 -65 87 0 3 -23 19 56 21 76 -50 -65 -50 -45 -23 0 3 19 21 56 76 87 89
  • 18. Lecture #07: © DSamanta CS 10001 : Programming and Data Structures 18 Insertion Sort - Example
  • 19. Insertion Sort: Complexity Analysis CS 10001 : Programming and Data Structures 19 Lecture #07: © DSamanta Case 1: If the input list is already in sorted order Number of comparisons: Number of comparison in each iteration is 1. Number of movement: No data movement takes place in any iteration. 𝑀 𝑛 = 0 𝐶 𝑛 = 1 + 1 + 1 + ⋯ + 1 upto (n-1)th iteration.
  • 20. Insertion Sort: Complexity analysis CS 10001 : Programming and Data Structures 20 Lecture #07: © DSamanta Case 2: If the input list is sorted but in reverse order Number of comparisons: Number of comparison in each iteration is 1. Number of movement: Number of movements takes place in any ith iteration is i. 𝑀 𝑛 = 1 + 2 + 3 + ⋯ + 𝑛 − 1 = 𝑛(𝑛 − 1) 2 𝐶 𝑛 = 1 + 2 + 3 + ⋯ + 𝑛 − 1 = 𝑛(𝑛 − 1) 2
  • 21. Insertion Sort: Complexity analysis CS 10001 : Programming and Data Structures 21 Lecture #07: © DSamanta Case 3: If the input list is in random order • Let 𝑝𝑗 be the probability that the key will go to the 𝑗𝑡ℎ location (1 ≤ 𝑗 ≤ 𝑖 + 1). Then the number of comparisons will be 𝑗 ∙ 𝑝𝑗. • The average number of comparisons in the (i + 1)𝑡ℎ iteration is 𝐴𝑖+1 = 𝑗=1 𝑖+1 𝑗 ∙ 𝑝𝑗 • Assume that all keys are distinct and all permutations of keys are equally likely. 𝑝1 = 𝑝2 = 𝑝3 = ⋯ = 𝑝𝑖+1 = 1 𝑖 + 1
  • 22. Insertion Sort: Complexity analysis CS 10001 : Programming and Data Structures 22 Lecture #07: © DSamanta Case 3: Number of comparisons • Therefore, the average number of comparisons in the (i + 1)𝑡ℎ iteration 𝐴𝑖+1 = 1 𝑖 + 1 𝑗=1 𝑖+1 𝑗 = 1 𝑖 + 1 ∙ 𝑖 + 1 ∙ 𝑖 + 2 2 = 𝑖 + 2 2 • Total number of comparisons for all (𝑛 − 1) iterations is 𝐶 𝑛 = 𝑖=0 𝑛−1 𝐴𝑖+1 = 1 2 ∙ 𝑛 𝑛 − 1 2 + (𝑛 − 1)
  • 23. Insertion Sort: Complexity analysis CS 10001 : Programming and Data Structures 23 Lecture #07: © DSamanta Case 3: Number of Movements • On the average, number of movements in the 𝑖𝑡ℎ iteration 𝑀𝑖 = 𝑖 + 𝑖 − 1 + 𝑖 − 2 + ⋯ + 2 + 1 𝑖 = 𝑖 + 1 2 • Total number of movements 𝑀 𝑛 = 𝑖=1 𝑛−1 𝑀𝑖 = 1 2 ∙ 𝑛 𝑛 − 1 2 + 𝑛 − 1 2
  • 24. Insertion Sort: Summary of Complexity Analysis CS 10001 : Programming and Data Structures 24 Lecture #07: © DSamanta Case Comparisons Movement Memory Remarks Case 1 C 𝑛 = (𝑛 − 1) 𝑀 𝑛 = 0 𝑆 𝑛 = 𝑛 Input list is in sorted order Case 2 C 𝑛 = 𝑛(𝑛 − 1) 2 𝑀 𝑛 = 𝑛(𝑛 − 1) 2 𝑆 𝑛 = 𝑛 Input list is sorted in reverse order Case 3 C 𝑛 = (𝑛 − 1)(𝑛 + 4) 4 𝑀 𝑛 = (𝑛 − 1)(𝑛 + 2) 4 𝑆 𝑛 = 𝑛 Input list is in random order Case Run time, 𝑻(𝒏) Complexity Remarks Case 1 𝑇 𝑛 = 𝑐 (𝑛 − 1) 𝑇 𝑛 = 𝑂(𝑛) Best case Case 2 𝑇 𝑛 = 𝑐 𝑛(𝑛 − 1) 𝑇 𝑛 = 𝑂(𝑛2 ) Worst case Case 3 𝑇 𝑛 = 𝑐 (𝑛 − 1)(𝑛 + 3) 2 𝑇 𝑛 = 𝑂(𝑛2 ) Average case
  • 25. Selection Sort CS 10001 : Programming and Data Structures 25 Lecture #07: © DSamanta
  • 26. Selection Sort CS 10001 : Programming and Data Structures 26 Lecture #07: © DSamanta General situation : remainder, unsorted smallest elements, sorted 0 size-1 k x: Steps : • Find smallest element, mval, in x[k…size-1] • Swap smallest element with x[k], then increase k. 0 k size-1 mval swap x:
  • 27. Selection Sort CS 10001 : Programming and Data Structures 27 Lecture #07: © DSamanta /* Yield location of smallest element in x[k .. size-1];*/ int findMinLloc (int x[ ], int k, int size) { int j, pos; /* x[pos] is the smallest element found so far */ pos = k; for (j=k+1; j<size; j++) if (x[j] < x[pos]) pos = j; return pos; }
  • 28. Selection Sort CS 10001 : Programming and Data Structures 28 Lecture #07: © DSamanta /* The main sorting function */ /* Sort x[0..size-1] in non-decreasing order */ int selectionSort (int x[], int size) { int k, m; for (k=0; k<size-1; k++) { m = findMinLoc(x, k, size); temp = a[k]; a[k] = a[m]; a[m] = temp; } }
  • 29. Selection Sort - Example CS 10001 : Programming and Data Structures 29 Lecture #07: © DSamanta -17 12 -5 6 142 21 3 45 x: 3 12 -5 6 142 21 -17 45 x: -17 -5 12 6 142 21 3 45 x: -17 -5 3 6 142 21 12 45 x: -17 -5 3 6 142 21 12 45 x: -17 -5 3 6 12 21 142 45 x: -17 -5 3 6 12 21 45 142 x: -17 -5 3 6 21 142 45 x: 12 -17 -5 3 6 12 21 45 142 x:
  • 30. Selection Sort: Complexity Analysis CS 10001 : Programming and Data Structures 30 Lecture #07: © DSamanta Case 1: If the input list is already in sorted order Number of comparisons: 𝐶 𝑛 = 𝑖=1 𝑛−1 𝑛 − 𝑖 = 𝑛(𝑛 − 1) 2 Number of movement: no data movement takes place in any iteration. 𝑀 𝑛 = 0
  • 31. Selection Sort: Complexity Analysis CS 10001 : Programming and Data Structures 31 Lecture #07: © DSamanta Case 2: If the input list is sorted but in reverse order Number of comparisons: Number of movements: 𝑀 𝑛 = 3 2 (𝑛 − 1) 𝑐 𝑛 = 𝑖=1 𝑛−1 𝑛 − 𝑖 = 𝑛(𝑛 − 1) 2
  • 32. Selection Sort: Complexity Analysis CS 10001 : Programming and Data Structures 32 Lecture #07: © DSamanta Case 3: If the input list is in random order Number of comparisons: 𝐶 𝑛 = 𝑛(𝑛 − 1) 2 • Let 𝑝𝑖 be the probability that the 𝑖𝑡ℎ smallest element is in the 𝑖𝑡ℎ position. Number of total swap operations = (1 − 𝑝𝑖) × (𝑛 − 1) where 𝑝1 = 𝑝2 = 𝑝3 = ⋯ = 𝑝𝑛 = 1 𝑛 • Total number of movements 𝑀 𝑛 = 1 − 1 𝑛 × 𝑛 − 1 × 3 = 3(𝑛 − 1)(𝑛 − 1) 𝑛
  • 33. Selection Sort: Summary of Complexity analysis CS 10001 : Programming and Data Structures 33 Lecture #07: © DSamanta Case Comparisons Movement Memory Remarks Case 1 𝑐 𝑛 = 𝑛(𝑛 − 1) 2 𝑀 𝑛 = 0 𝑆 𝑛 = 0 Input list is in sorted order Case 2 𝑐 𝑛 = 𝑛(𝑛 − 1) 2 𝑀 𝑛 = 3(𝑛 − 1) 2 𝑆 𝑛 = 0 Input list is sorted in reverse order Case 3 𝑐 𝑛 = 𝑛(𝑛 − 1) 2 𝑀 𝑛 = 3 𝑛 − 1 2 𝑛 𝑆 𝑛 = 0 Input list is in random order Case Run time, 𝑻(𝒏) Complexity Remarks Case 1 𝑇 𝑛 = 𝑛(𝑛 − 1) 2 𝑇 𝑛 = 𝑂(𝑛2 ) Best case Case 2 𝑇 𝑛 = (𝑛 − 1)(𝑛 + 3) 2 𝑇 𝑛 = 𝑂(𝑛2 ) Worst case Case 3 𝑇 𝑛 ≈ (𝑛 − 1)(2𝑛 + 3) 2 (Taking 𝑛 − 1 ≈ 𝑛) 𝑇 𝑛 = 𝑂(𝑛2 ) Average case
  • 34. Bubble Sort CS 10001 : Programming and Data Structures 34 Lecture #07: © DSamanta
  • 35. Bubble Sort CS 10001 : Programming and Data Structures 35 Lecture #07: © DSamanta The sorting process proceeds in several passes. • In every pass we go on comparing neighbouring pairs, and swap them if out of order. • In every pass, the largest of the elements under considering will bubble to the top (i.e., the right). In every iteration heaviest element drops at the bottom. The bottom moves upward.
  • 36. Bubble Sort CS 10001 : Programming and Data Structures 36 Lecture #07: © DSamanta How the passes proceed? • In pass 1, we consider index 0 to n-1. • In pass 2, we consider index 0 to n-2. • In pass 3, we consider index 0 to n-3. • …… • …… • In pass n-1, we consider index 0 to 1.
  • 37. Bubble Sort - Example CS 10001 : Programming and Data Structures 37 Lecture #07: © DSamanta 3 12 -5 6 72 21 -7 45 x: Pass: 1 3 12 -5 6 72 21 -7 45 x: 3 -5 12 6 72 21 -7 45 x: 3 -5 6 12 72 21 -7 45 x: 3 -5 6 12 72 21 -7 45 x: 3 -5 6 12 21 72 -7 45 x: 3 -5 6 12 21 -7 72 45 x: 3 -5 6 12 21 -7 45 72 x:
  • 38. Bubble Sort - Example CS 10001 : Programming and Data Structures 38 Lecture #07: © DSamanta 3 -5 6 12 21 -7 45 72 x: Pass: 2 -5 3 6 12 21 -7 45 72 x: -5 3 6 12 21 -7 45 72 x: -5 3 6 12 21 -7 45 72 x: -5 3 6 12 21 -7 45 72 x: -5 3 6 12 -7 21 45 72 x: -5 3 6 12 -7 21 45 72 x:
  • 39. Bubble Sort CS 10001 : Programming and Data Structures 39 Lecture #07: © DSamanta void swap(int *x, int *y) { int tmp = *x; *x = *y; *y = tmp; } void bubble_sort(int x[], int n) { int i,j; for (i=n-1; i>0; i--) for (j=0; j<i; j++) if (x[j] > x[j+1]) swap(&x[j],&x[j+1]); }
  • 40. Bubble Sort CS 10001 : Programming and Data Structures 40 Lecture #07: © DSamanta int main() { int x[ ]={-45,89,-65,87,0,3,-23,19,56,21,76,-50}; int i; for(i=0;i<12;i++) printf("%d ",x[i]); printf("n"); bubble_sort(x,12); for(i=0;i<12;i++) printf("%d ",x[i]); printf("n"); } OUTPUT -45 89 -65 87 0 3 -23 19 56 21 76 -50 -65 -50 -45 -23 0 3 19 21 56 76 87 89
  • 41. Bubble Sort: Complexity analysis CS 10001 : Programming and Data Structures 41 Lecture #07: © DSamanta Case 1: If the input list is already in sorted order Number of comparisons: 𝐶 𝑛 = 𝑛(𝑛 − 1) 2 Number of movements: 𝑀 𝑛 = 0
  • 42. Bubble Sort: Complexity analysis CS 10001 : Programming and Data Structures 42 Lecture #07: © DSamanta Case 2: If the input list is sorted but in reverse order Number of comparisons: Number of movements: 𝑀 𝑛 = 𝑛(𝑛 − 1) 2 𝑐 𝑛 = 𝑛(𝑛 − 1) 2
  • 43. Bubble Sort: Complexity analysis CS 10001 : Programming and Data Structures 43 Lecture #07: © DSamanta Case 3: If the input list is in random order Number of comparisons: 𝐶 𝑛 = 𝑛(𝑛 − 1) 2 Number of movements: • Let 𝑝𝑗 be the probability that the largest element is in the unsorted part is in 𝑗𝑡ℎ (1 ≤ 𝑗 ≤ 𝑛 − 𝑖 + 1) location. • The average number of swaps in the 𝑖𝑡ℎ pass is = 𝑗=1 𝑛−𝑖+1 𝑛 − 𝑖 + 1 − 𝑗 ∙ 𝑝𝑗
  • 44. Bubble Sort: Complexity analysis CS 10001 : Programming and Data Structures 44 Lecture #07: © DSamanta Case 3: If the input list is in random order Number of movements: • 𝑝1 = 𝑝2 = 𝑝𝑛−𝑖+1 = 1 𝑛−𝑖+1 • Therefore, the average number of swaps in the 𝑖𝑡ℎ pass is = 𝑗=1 𝑛−𝑖+1 1 𝑛 − 𝑖 + 1 ∙ 𝑛 − 𝑖 + 1 − 𝑗 = 𝑛 − 𝑖 2 • The average number of movements 𝑀(𝑛) = 𝑖=1 𝑛−1 𝑛 − 𝑖 2 = 𝑛(𝑛 − 1) 4
  • 45. Bubble Sort: Summary of Complexity analysis CS 10001 : Programming and Data Structures 45 Lecture #07: © DSamanta Case Comparisons Movement Memory Remarks Case 1 𝑐 𝑛 = 𝑛(𝑛 − 1) 2 𝑀 𝑛 = 0 𝑆 𝑛 = 0 Input list is in sorted order Case 2 𝑐 𝑛 = 𝑛(𝑛 − 1) 2 𝑀 𝑛 = 𝑛(𝑛 − 1) 2 𝑆 𝑛 = 0 Input list is sorted in reverse order Case 3 𝑐 𝑛 = 𝑛(𝑛 − 1) 2 𝑀 𝑛 = 𝑛(𝑛 − 1) 4 𝑆 𝑛 = 0 Input list is in random order Case Run time, 𝑻(𝒏) Complexity Remarks Case 1 𝑇 𝑛 = 𝑐 𝑛(𝑛 − 1) 2 𝑇 𝑛 = 𝑂(𝑛2 ) Best case Case 2 𝑇 𝑛 = 𝑐𝑛 𝑛 − 1 𝑇 𝑛 = 𝑂(𝑛2 ) Worst case Case 3 𝑇 𝑛 = 3 4 𝑛(𝑛 − 1) 𝑇 𝑛 = 𝑂(𝑛2 ) Average case
  • 46. Bubble Sort CS 10001 : Programming and Data Structures 46 Lecture #07: © DSamanta How do you make best case with (n-1) comparisons only? • By maintaining a variable flag, to check if there has been any swaps in a given pass. • If not, the array is already sorted.
  • 47. Bubble Sort CS 10001 : Programming and Data Structures 47 Lecture #07: © DSamanta void bubble_sort(int x[], int n) { int i,j; int flag = 0; for (i=n-1; i>0; i--) { for (j=0; j<i; j++) if (x[j] > x[j+1]) { swap(&x[j],&x[j+1]); flag = 1; } if (flag == 0) return; } }
  • 48. Efficient Sorting algorithms CS 10001 : Programming and Data Structures 48 Lecture #07: © DSamanta Two of the most popular sorting algorithms are based on divide-and-conquer approach. • Quick sort • Merge sort sort (list) { if the list has length greater than 1 { Partition the list into lowlist and highlist; sort (lowlist); sort (highlist); combine (lowlist, highlist); } } Basic concept of divide-and-conquer method:
  • 49. Quick Sort CS 10001 : Programming and Data Structures 49 Lecture #07: © DSamanta
  • 50. Quick Sort – How it Works? CS 10001 : Programming and Data Structures 50 Lecture #07: © DSamanta At every step, we select a pivot element in the list (usually the first element). • We put the pivot element in the final position of the sorted list. • All the elements less than or equal to the pivot element are to the left. • All the elements greater than the pivot element are to the right.
  • 51. Quick Sort Partitioning CS 10001 : Programming and Data Structures 51 Lecture #07: © DSamanta 0 size-1 x: pivot Values smaller Values greater Perform partitioning Perform partitioning
  • 52. Quick Sort CS 10001 : Programming and Data Structures 52 Lecture #07: © DSamanta #include <stdio.h> void quickSort( int[], int, int); int partition( int[], int, int); void main() { int i,a[] = { 7, 12, 1, -2, 0, 15, 4, 11, 9}; printf("nnUnsorted array is: "); for(i = 0; i < 9; ++i) printf(" %d ", a[i]); quickSort( a, 0, 8); printf("nnSorted array is: "); for(i = 0; i < 9; ++i) printf(" %d ", a[i]); } void quickSort( int a[], int l, int r) { int j; if( l < r ) { // divide and conquer j = partition( a, l, r); quickSort( a, l, j-1); quickSort( a, j+1, r); } }
  • 53. Quick Sort CS 10001 : Programming and Data Structures 53 Lecture #07: © DSamanta int partition( int a[], int l, int r) { int pivot, i, j, t; pivot = a[l]; i = l; j = r+1; while( 1) { do { ++i; } while(a[i]<=pivot && i<=r); do { --j; } while( a[j] > pivot ); if( i >= j ) break; t = a[i]; a[i] = a[j]; a[j] = t; } t = a[l]; a[l] = a[j]; a[j] = t; return j; }
  • 54. Lecture #07: © DSamanta CS 10001 : Programming and Data Structures 54 Input: 45 -56 78 90 -3 -6 123 0 -3 45 69 68 Output: -56 -6 -3 -3 0 45 45 68 69 78 90 123 45 -56 78 90 -3 -6 123 0 -3 45 69 68 -6 -56 -3 0 -3 45 123 90 78 45 69 68 -3 0 -3 -6 -56 0 -3 -3 -3 0 68 90 78 45 69 123 78 90 69 68 45 78 69 90 Quick Sort - Example
  • 55. Quick Sort: Complexity analysis CS 10001 : Programming and Data Structures 55 Lecture #07: © DSamanta Memory requirement: Size of the stack is: 𝑆 𝑛 = 𝑙𝑜𝑔2 𝑛 + 1 Number of comparisons: • Let, 𝑇(𝑛) represents total time to sort n elements and 𝑃(𝑛) represents the time for perform a partition of a list of 𝑛 elements. 𝑇 𝑛 = 𝑃 𝑛 + 𝑇 𝑛𝑙 + 𝑇 𝑛𝑟 , with 𝑇 1 = 𝑇 0 = 0 where, 𝑛𝑙 = number of elements in the left sub list 𝑛𝑟 = number of elements in the right sub list and 0 ≤ 𝑛𝑙, 𝑛𝑟 < 𝑛
  • 56. Quick Sort: Complexity analysis CS 10001 : Programming and Data Structures 56 Lecture #07: © DSamanta Case 1: Elements in the list are in ascending order Number of comparisons: 𝐶 𝑛 = 𝑛 − 1 + 𝐶 𝑛 − 1 , with 𝐶 1 = 𝐶 0 = 0 𝐶 𝑛 = 𝑛 − 1 + 𝑛 − 2 + 𝑛 − 3 + ⋯ + 2 + 1 𝐶 𝑛 = 𝑛(𝑛 − 1) 2 Number of movements: 𝑀 𝑛 = 0
  • 57. Quick Sort: Complexity analysis CS 10001 : Programming and Data Structures 57 Lecture #07: © DSamanta Case 2: Elements in the list are in reverse order Number of comparisons: Number of movements: 𝑀 𝑛 = 𝑛 − 1 2 , 𝑖𝑓 𝑛 𝑖𝑠 𝑜𝑑𝑑 𝑛 2 , 𝑖𝑓 𝑛 𝑖𝑠 𝑒𝑣𝑒𝑛 𝐶 𝑛 = 𝑛 − 1 + 𝐶 𝑛 − 1 , with 𝐶 1 = 𝐶 0 = 0 𝐶 𝑛 = 𝑛 − 1 + 𝑛 − 2 + 𝑛 − 3 + ⋯ + 2 + 1 𝐶 𝑛 = 𝑛(𝑛 − 1) 2
  • 58. Quick Sort: Complexity analysis CS 10001 : Programming and Data Structures 58 Lecture #07: © DSamanta Case 3: Elements in the list are in random order Number of comparisons: 𝐶 𝑛 = 𝑛 − 1 + 𝑖=1 𝑛−1 1 𝑛 𝐶 𝑖 − 1 + 𝐶(𝑛 − 1) 𝑤𝑖𝑡ℎ 𝐶 1 = 𝐶 0 = 0 𝐶 𝑛 = 𝑛 − 1 + 2 𝑛 𝑖=1 𝑛−1 𝐶(𝑖) 𝐶 𝑛 = 2 𝑛 + 1 𝑙𝑜𝑔𝑒𝑛 + 0.577 − 4𝑛 (i-1) (n-1) 𝒊𝒕𝒉 location
  • 59. Quick Sort: Complexity analysis CS 10001 : Programming and Data Structures 59 Lecture #07: © DSamanta Case 3: Elements in the list are in random order Number of movements: 𝑀 𝑛 = 1 𝑛 𝑖=1 𝑛 𝑖 − 1 + 𝑀 𝑖 − 1 + 𝑀(𝑛 − 𝑖) 𝑀 𝑛 = 𝑛 − 1 2 + 2 𝑛 𝑖=1 𝑛−1 𝑀(𝑖) 𝑀 𝑛 = 2 𝑛 + 1 𝑙𝑜𝑔𝑒𝑛 + 0.577 − 4𝑛
  • 60. Quick Sort: Summary of Complexity analysis CS 10001 : Programming and Data Structures 60 Lecture #07: © DSamanta Case Comparisons Movement Memory Remarks Case 1 𝑐 𝑛 = 𝑛(𝑛 − 1) 2 𝑀 𝑛 = 0 𝑆 𝑛 = 1 Input list is in sorted order Case 2 𝑐 𝑛 = 𝑛(𝑛 − 1) 2 𝑀 𝑛 = 𝑛 2 𝑆 𝑛 = 1 Input list is sorted in reverse order Case 3 𝐶 𝑛 = 2 𝑛 + 1 𝑙𝑜𝑔𝑒𝑛 + 0.577 − 4𝑛 𝑀 𝑛 = 2 𝑛 + 1 (𝑙𝑜𝑔𝑒𝑛 𝑆 𝑛 = 𝑙𝑜𝑔2 𝑛 + 1 Input list is in random order Case Run time, 𝑻(𝒏) Complexity Remarks Case 1 𝑇 𝑛 = 𝑐 𝑛(𝑛 − 1) 2 𝑇 𝑛 = 𝑂(𝑛2 ) Worst case Case 2 𝑇 𝑛 = 𝑐 𝑛(𝑛 − 1) 2 + 𝑛 2 𝑇 𝑛 = 𝑂(𝑛2 ) Worst case Case 3 𝑇 𝑛 = 4𝑐 𝑛 + 1 𝑙𝑜𝑔𝑒𝑛 + 0.577 − 8𝑐𝑛 𝑇 𝑛 = 2𝑐 𝑛𝑙𝑜𝑔2𝑛 − 𝑛 + 1 𝑇 𝑛 = 𝑂(𝑛𝑙𝑜𝑔2𝑛) Best / Average case
  • 61. Merge Sort CS 10001 : Programming and Data Structures 61 Lecture #07: © DSamanta
  • 62. Merge Sort – How it Works? CS 10001 : Programming and Data Structures 62 Lecture #07: © DSamanta Input Array Part-I Part-II Part-I Part-II Part-I Part-II Split Merge Sorted arrays
  • 63. Merging two Sorted arrays CS 10001 : Programming and Data Structures 63 Lecture #07: © DSamanta 0 Sorted Array Sorted Array 0 l m a: b: Merged sorted array 0 l+m-1 c: 𝑷𝒂 𝑷𝒃 Move and copy elements pointed by 𝑃𝑎 if its value is smaller than the element pointed by 𝑃𝑏 in (𝑙 + 𝑚 − 1) operations and otherwise.
  • 64. Merge Sort – Example CS 10001 : Programming and Data Structures 64 Lecture #07: © DSamanta Merging two sorted arrays Splitting arrays 3 12 -5 6 72 21 -7 45 x: 3 12 -5 6 72 21 -7 45 3 12 -5 6 72 21 -7 45 3 12 -5 6 72 21 -7 45 3 12 -5 6 21 72 -7 45 -5 3 6 12 -7 21 45 72 -7 -5 3 -7 -5 3 6 12 21 45 72
  • 65. Merge Sort Program CS 10001 : Programming and Data Structures 65 Lecture #07: © DSamanta #include<stdio.h> void mergesort(int a[],int i,int j); void merge(int a[],int i1,int j1,int i2,int j2); int main() { int a[30],n,i; printf("Enter no of elements:"); scanf("%d",&n); printf("Enter array elements:"); for(i=0;i<n;i++) scanf("%d",&a[i]); mergesort(a,0,n-1); printf("nSorted array is :"); for(i=0;i<n;i++) printf("%d ",a[i]); return 0; }
  • 66. Merge Sort Program CS 10001 : Programming and Data Structures 66 Lecture #07: © DSamanta void mergesort(int a[],int i,int j) { int mid; if(i<j) { mid=(i+j)/2; /* left recursion */ mergesort(a,i,mid); /* right recursion */ mergesort(a,mid+1,j); /* merging of two sorted sub-arrays */ merge(a,i,mid,mid+1,j); } }
  • 67. Merge Sort Program CS 10001 : Programming and Data Structures 67 Lecture #07: © DSamanta void merge(int a[],int i1,int i2,int j1,int j2) { int temp[50]; //array used for merging int i=i1,j=j1,k=0; while(i<=i2 && j<=j2) //while elements in both lists { if(a[i]<a[j]) temp[k++]=a[i++]; else temp[k++]=a[j++]; } while(i<=i2) //copy remaining elements of the first list temp[k++]=a[i++]; while(j<=j2) //copy remaining elements of the second list temp[k++]=a[j++]; for(i=i1,j=0;i<=j2;i++,j++) a[i]=temp[j]; //Transfer elements from temp[] back to a[] }
  • 68. Merge Sort – Splitting Trace CS 10001 : Programming and Data Structures 68 Lecture #07: © DSamanta -56 23 43 -5 -3 0 123 -35 87 56 75 80 Output: -56 -35 -5 -3 0 23 43 56 75 80 87 123 -56 23 43 23 43 -56 23 43 -5 -3 0 123 -35 87 56 75 80 -56 23 43 -5 -3 0 -3 0 -3 0 123 -35 87 -35 87 123 -35 87 56 75 80 56 75 80 75 80 -5 Worst Case: O(n.log(n)) Space Complexity??
  • 69. Merge Sort: Complexity analysis CS 10001 : Programming and Data Structures 69 Lecture #07: © DSamanta Time Complexity: 𝑇 𝑛 = 𝐷 𝑛 + 𝑇 𝑛 2 + 𝑇 𝑛 2 + 𝐶 𝑛 𝑖𝑓 𝑛 > 1 𝑇 𝑛 = 𝑐1 𝑖𝑓 𝑛 = 1 • For simplicity of calculation 𝑇 𝑛 2 = 𝑇 𝑛 2 = 𝑇 𝑛 2 𝑇 𝑛 = 𝑐1 𝑖𝑓 𝑛 = 1 𝑇 𝑛 = 𝑐2 + 2𝑇 𝑛 2 + 𝑛 − 1 𝑖𝑓 𝑛 > 1 𝑇 𝑛 = 𝑛 ∙ 𝑐1 + 𝑛𝑙𝑜𝑔2𝑛 + 𝑐2 − 1 𝑛 − 1 𝐴𝑠𝑠𝑢𝑚𝑖𝑛𝑔 𝑛 = 2𝑘
  • 70. Quick Sort vs. Merge Sort CS 10001 : Programming and Data Structures 70 Lecture #07: © DSamanta • Quick sort • hard division, easy combination • partition in the divide step of the divide-and-conquer framework • hence combine step does nothing • Merge sort • easy division, hard combination • merge in the combine step • the divide step in this framework does one simple calculation only
  • 71. Quick Sort vs. Merge Sort CS 10001 : Programming and Data Structures 71 Lecture #07: © DSamanta Both the algorithms divide the problem into two sub problems. • Merge sort: – two sub problems are of almost equal size always. • Quick sort: – an equal sub division is not guaranteed. • This difference between the two sorting methods appears as the deciding factor of their run time performances.
  • 72. Any question? You may post your question(s) at the “Discussion Forum” maintained in the course Web page. CS 10001 : Programming and Data Structures 72 Lecture #07: © DSamanta
  • 73. Problems to Ponder… CS 10001 : Programming and Data Structures 73 Lecture #07: © DSamanta
  • 74. Problems for Practice… CS 10001 : Programming and Data Structures 74 Lecture #07: © DSamanta *You can check the Moodle course management system for a set of problems for your own practice. • Login to the Moodle system at http://cse.iitkgp.ac.in/ • Select “PDS Spring-2017 (Theory) in the link “My Courses” • Go to Topic 7: Practice Sheet #07 : Pointer in C *Solutions to the problems in Practice Sheet #07 will be uploaded in due time.
  • 75. Lecture #07: © DSamanta CS 10001 : Programming and Data Structures 75 If you try to solve problems yourself, then you will learn many things automatically. Spend few minutes and then enjoy the study.