You are on page 1of 34

LECTURE WEEK 3 SORTING

AGENDA
We will learn this week about various sorting algorithms
Bubble sort
Selection sort
Insertion sort

https://www.toptal.com/developers/sorting-algorithms
SORTING

WHAT?
Refers to the operation of arranging the data in a certain order, such as increasing or
decreasing. The output is reordering of the input.
For Example let A be a list of n numbers then:
A[1]<A[2]<A[3]<…………..A[n]

WHY?
Sorting is one of the important categories of algorithm in CS. Sorting can
significantly reduce the complexity of a problem, and is often used for database
algorithms and searches.
CLASSIFICATION OF SORTING
ALGORITHMS
Generally categorized based on the following parameters.
 By no. of comparisons

 By no. of swaps

 By memory usage

 By recursion
SORTING TECHNIQUES
Some of the sorting techniques used to arrange data are as follows :
Bubble sort
Selection sort
Insertion sort
BUBBLE SORT

The general idea of the bubble sort is that each element is compared to all the
elements in the data.
The bubble sort algorithm requires N-1 passes to sort N items of data.
The bubble sort is notoriously slow, but it is conceptually the simplest of the sorting
algorithms.
1. The result of the first pass is that the largest item is in the last location of the array.
2. The result of the second pass is that the second largest item is in the second last location of
the array.
3. etc.
4. After N passes, the entire array is sorted.
BUBBLE SORT

The operation in each pass is as follows:


1. First, the values in the first two locations are compared. If necessary the values are exchanged,
so that the largest one is last.
2. Then, the values in the second and third locations are compared. If necessary the values are
exchanged, so that again the largest one is last.
3. This process repeats to the end of the array.
4. In effect, the largest item bubbles its way towards the top. It keeps on going until either it
reaches the top and the pass ends.

If a complete pass is made without any exchanges being made, the data must already
be sorted.
EXAMPLE
25 15 10 2 17 1

Pass 1 : 25 15 10 2 17 1
15 25 20 2 17 1
15 20 25 2 17 1
15 20 2 25 17 1
15 20 2 17 25 1
15 20 2 17 1 25
EXAMPLE
Pass2: 15 20 2 17 1 25
15 20 2 17 1 25
15 2 20 17 1 25
15 2 17 20 1 25
15 2 17 1 20 25
15 2 17 1 20 25

Pass 3: 15 2 17 1 20 25
2 15 17 1 20 25
2 15 17 1 20 25
2 15 1 17 20 25
2 15 1 17 20 25
EXAMPLE
Pass 4 : 2 15 1 17 20 25
2 15 1 17 20 25
2 1 15 17 20 25
2 1 15 17 20 25
Pass 5: 2 1 15 17 20 25
1 2 15 17 20 25
1 2 15 17 20 25
1 2 15 17 20 25
1 2 15 17 20 25
EXAMPLE
Suppose the following numbers are stored in an array A:
32,51,27,85,66,23,13,57
Pass 1:
a) Compare A[1] and A[2]:Since 32<51 the list is not altered.
b) Compare A[2] and A[3]: Since 51>27 interchange 51 and 27.
32,27,51,85,66,23,13,57
c) Compare A[3] and A[4]:Since 51<85 the list is not altered.
d) Compare A[4] and A[5]: Since 85>66 interchange 85 and 66.
32,27,51,66,85,23,13,57
e) Compare A[5] and A[6]: Since 85>23 interchange 85 and 23.
32,27,51,66,23,85,13,57
f) Compare A[6] and A[7]: Since 85>13 interchange 85 and 13.
g) 32,27,51,66,23,13,85,57
h) Compare A[7] and A[8]: Since 85>57 interchange 85 and 57.
i) 32,27,51,66,23,13,57,85
APPLICATIONS
Bubble sort is mainly used in educational purposes for helping students understand
the foundations of sorting.
This is used to identify whether the list is already sorted. When the list is already
sorted (which is the best-case scenario), the complexity of bubble sort is only O(n).
Activity : Height
BUBBLE SORT ALGORITHM
void Bubble sort (int A[ ], int n) void main( )
{ {
for (pass=0; pass < n -1; pass++) int arr[5]= {2,1,55,3,6};
{ int size=5;
for (c=0; c< (n-pass-1) ; c++) Bubble sort(arr,size);
{ //display elements of array.
if (A[c]>A[c+1]) for(int i=0;i<size;i++)
{ cout<< arr[i]<<endl;
swap (A[c], A[c+1]) }
}}
}}
BUBBLE SORT ALGORITHM
void Bubble sort (int A[ ], int n) void main( )
{
{
for (pass=0; pass < n -1; pass++)
int arr[5]= {2,1,55,3,6};
{
s=false; int size=5;
for (c=0; c< (n-pass-1) ; c++) Bubble sort(arr,size);
{ //display elements of array.
if (A[c]>A[c+1])
for(int i=0;i<size;i++)
{
swap (A[c], A[c+1]) cout<< arr[i]<<endl;
s=true; }
}}
if ( !s )
break;
}}
COMPLEXITY OF BUBBLE SORT

Worst Case:
The worst case occurs when we have to search through the entire array of data and the item does not
appear in data. In this case the algorithm requires f(n) = n-1 comparisons.

Average Case:
The average number of comparisons required to find the location item is approximately equal to half
number of elements in the array that is :
f(n) = (n-1)/2 comparisons.
COMPLEXITY OF BUBBLE
SORT
Time consuming operations
 compares, swaps.

 Best Case O(n)


 Worst Case (n-1)+(n-2)+(n-3) …+1 , or O(n2)
 Average Case O(n2)

Space
 size of the array
 an in-place algorithm
SELECTION SORT
SELECTION SORT
The general idea of the selection sort is that for each slot, find the element that
belongs there.
The selection sort improves on the bubble sort by reducing the number of swaps.
The number of comparisons remains the same that is to sort N items you make N-1
passes through them.
SELECTION SORT
The operation in each pass is as follows:
1. First find the smallest element in the list and swap it with the element in the first position.
2. On the second pass you scan through just the first N-1 entries. Then find the second smallest
element in the list and swap it in the element in the second position.
3. This process is repeated, with one item being placed in its correct location each time.
4. After N-1 passes, the entire collection of data is sorted.
EXAMPLE

Suppose the following numbers are stored in an array A:


77,33,44,11,88,22,66,55

Pass 1: 77,33,44,11,88,22,66,55
Pass 2: 11,33,44,77,88,22,66,55
Pass 3: 11,22,44,77,88,33,66,55
Pass 4: 11,22,33,77,88,44,66,55
Pass 5: 11,22,33,44,88,77,66,55
Pass 6: 11,22,33,44,55,77,66,88
Pass 7: 11,22,33,44,55,66,77,88
SELECTION SORT ALGORITHM
void Selection sort (int A[ ], int n)
{
for (i=0;i<n-1;i++)
{
min=i;
for (j=i+1;j<n;j++)
{
if (A[j]<A[min])
min=j;
}
swap (A[min],A[i])
}}
COMPLEXITY OF SELECTION
SORT

Best Case Worst Case

O(n2) O(n2)
INSERTION SORT
Idea: like sorting a hand of playing cards
 Start with an empty left hand and the cards facing down on the table.
 Remove 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
 The cards held in the left hand are sorted
 these cards were originally the top cards of the pile on the table

24
INSERTION SORT

To insert 12, we need to make


room for it by moving first 36
and then 24.
6 10 24 36

12

25
INSERTION SORT

6 10 24 36

12

26
INSERTION SORT

6 10 24 3
6

12

27
INSERTION SORT input array

5 2 4 6 1 3

at each iteration, the array is divided in two sub-arrays:

left sub-array right sub-array

sorted unsorted

28
INSERTION SORT

29
INSERTION SORT
The operation in each pass is as follows:
1. A[1 ] by itself is sorted.
2. A[2] is inserted either before A[1] or after A[1] so that A[1] and A[2]
are sorted.
3. A[N] is inserted into its proper place in A[1],A[2]…A[N-1] so that
A[1],A[2]…A[N] is sorted.
EXAMPLE
Suppose the following numbers are stored in an array A:
68145372  (consider index 0)
68145372  (consider index 0 - 1)
16845372  (consider index 0 - 2)
14685372  (consider index 0 - 3)
14568372  (consider index 0- 4 )
13456872  (consider index 0- 5)
13456782
12345678
Insertion Sort Algorithm
void Insertion sort (int A[ ], int n)
{
for (i=1;i<=n-1;i++)
{
key=A[i]
j=i-1;
while (j>=0 && A[j]>key)
{
A[j+1]=A[j];
j--;
}
A[j+1]=key;
}
}
COMPLEXITY OF INSERTION
SORT

Best Case Worst Case

O(n2) O(n2)
SOME INTERESTING LINKS
https://visualgo.net/en/sorting
https://www.youtube.com/watch?v=kPRA0W1kECg (for all sorting algorithms)
https://www.youtube.com/watch?v=18OO361--1E (for bubble sort)
https://www.youtube.com/watch?v=R_f3PJtRqUQ (for selection sort)
https://www.youtube.com/watch?v=uMqVuEEWJv4 (for insertion sort)

You might also like