0% found this document useful (0 votes)
5 views

4Selection Sort

The document provides an overview of various sorting algorithms including Selection Sort, Shell Sort, Radix Sort, Merge Sort, and Bubble Sort, detailing their methods, performance, and examples. It explains the principles behind sorting, such as the divide and conquer strategy used in Merge Sort, and provides specific time complexities for each algorithm. Additionally, it includes links to video resources for further understanding and exercises to practice sorting techniques.

Uploaded by

zainkhizar786786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

4Selection Sort

The document provides an overview of various sorting algorithms including Selection Sort, Shell Sort, Radix Sort, Merge Sort, and Bubble Sort, detailing their methods, performance, and examples. It explains the principles behind sorting, such as the divide and conquer strategy used in Merge Sort, and provides specific time complexities for each algorithm. Additionally, it includes links to video resources for further understanding and exercises to practice sorting techniques.

Uploaded by

zainkhizar786786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 98

Selection Sort

Sorting
• Sorting is a process in which records are arranged in
ascending or descending order
1 2 3 4 5 6

77 42 35 12 101 5

1 2 3 4 5 6

5 12 35 42 77 101

Computer Science Department


Types of sorting
• Selection sort
• Insertion sort
• Bubble sort
• Merge sort
• Quick sort
• Heap sort
• Shell sort

Computer Science Department


Sorting Algorithm
• A sorting algorithm is a method for reorganizing a large number
of items into a specific order, such as alphabetical, highest-to-
lowest value or shortest-to-longest distance.
• Sorting algorithms take lists of items as input data, perform
specific operations on those lists and deliver ordered arrays as
output.
Sorting by Selection Sort

- Picks the smallest element in the array and brings it in the 1st
position; then picks the next smallest and puts it in the 2nd
position and so on
• Takes more time on the list sorted in descending order (worst possible case)
• Takes less time on the list sorted in ascending order (best possible case)
• Average time & worst-case time is quadratic Ο(n2)
Algorithm Selection Sort
How Selection Sort Work
Example: Selection Sort
• 26 33 43 100 46 88 52 17 53 77
• 17 | 33 43 100 46 88 52 26 53 77
• 17 26 | 43 100 46 88 52 33 53 77
• 17 26 33 | 100 46 88 52 43 53 77
• 17 26 33 43 | 46 88 52 100 53 77
• 17 26 33 43 46 | 88 52 100 53 77
• 17 26 33 43 46 52 | 88 100 53 77
• 17 26 33 43 46 52 53 | 100 88 77
• 17 26 33 43 46 52 53 77 | 88 100
• 17 26 33 43 46 52 53 77 88 | 100

Computer Science Department


For More Example
• https://www.youtube.com/watch?v=EdUWyka7kpI
• https://www.youtube.com/watch?v=Aq2E47uU2ao
• https://www.youtube.com/watch?v=cqh8nQwuKNE
Shell Sort
Shell Sort
• Uses insertion sort on periodic subsequences of the input to produce a
faster sorting algorithm.
• Algorithm sorts an array with n elements.
• The subsequences to be sorted are determined by increments (ht, ht-1,…..,
h1).
• The number of subsequences are obtained by the number of increments
(h values).
• Provides better performance due the fact that when the last passes are
made using small increments, few elements will be out of order because
of all the work that was done in earlier passes.
• The number of comparisons done by Shell sort is a function of the
sequence of increments used so a complete analysis is extremely
difficult.
• It has been shown that the best choice of h is approximately 1.72 3 n
and with this choice the average running time is proportional to n5/3.
• The Average - case : C(n) = O(n5/3) when h = 1.72 3 n
• The worst - case : C(n) = O(n2) when h = 1
• The best - case : C(n) = O(n (logn)2) if increments are
carefully chosen for fairly large n
List of numbers: 7 19 24 13 31 8 82 18 44 63 5 29
H = 1.72 3 12 ≈ 4

H4=4 7 19 24 13 31 8 82 18 44 63 5 29
1 2 3 4 5 6 7 8 9 10 11 12

H3=3 7 8 24 13 31 19 5 18 44 63 82 29
1 2 3 4 5 6 7 8 9 10 11 12

H2=2 7 8 19 5 18 24 13 31 29 63 82 44
1 2 3 4 5 6 7 8 9 10 11 12

H1=1
7 5 18 8 13 24 19 31 29 44 82 63
1 2 3 4 5 6 7 8 9 10 11 12

Sorted list 5 7 8 13 18 19 24 29 31 44 63 82
1 2 3 4 5 6 7 8 9 10 11 12
For More Example
• https://www.youtube.com/watch?v=CmPA7zE8mx0
https://youtu.be/ddeLSDsYVp8
Exercise
Solve this array using Shell Sort

23 4 7 34 45 2 43 1 5
Radix Sort
Radix Sort
• It is used to sort numbers using 10 buckets for decimal numbers
ranging 0 - 9.
• The given numbers are first sorted according to the unit digit and
each number is placed in the concerned bucket, and then the buckets
are combined in order before distributing according to the tens digit.
• This sorting process continues to the last digit (i.e. the most
significant bit) in d-passes for d-digit numbers.
• For a 5- digit numbers, radix sort places numbers in the bucket in 5
passes and combines the buckets five times.
• Radix sort is sometimes used to sort records of information that are
keyed by multiple fields (i.e. to sort dates by year, month and day).
• Number of comparisons (Cn) = d*s*n
Where d = Digits in a number (d=10 for decimal digit)
s = Number of digits in a number (s = 4 for 972, 8345 & 89
numbers)
n = Number of items (given numbers to be sorted)
The worst - case = C(n) = O(n2) as s = n
The best - case = C(n) = O(n logn) as s=log10n
The average - case = C(n) = O(s*n)
Activity
Number MSB(Most Significant Bit) LSB(Least Significant Bit)
234567 2 7
58356 5 6
3621 3 1
7824 7 4
Example
Input

4 3 2

5 6 4

1 2 1

0 4 5

0 1

7 8 8

2 3
Example
List of numbers:
48081, 90342, 90287, 90583, 53202, 65215, 78397, 48001, 972, 65315,
41983, 90283, 81664, 38107

Unsorted list
bkt 1st Pass bkt 2nd Pass bkt 3rd Pass bkt 4th Pass bkt 5th Pass Sorted list

48081 0 0 48001 0 48001 90283 0 00972 00972


97342 1 48081 53202 48081 0 90287 1 38107
90287 48001 38107 1 38107 90583 2 41983
90583 97342 1 65215 53202 00972 3 38107 48001
53202 2 53202 65315 2 65215 1 81664 41983 48081
65215 00972 2 90283 41983 4 48001 53202
78397 90583 3 90287 2 48081 65215
48001 3 41983 4 97342 65315 3 53202 5 53202 65315
00972 90283 5 3 97342 4 6 65215 78397
65315 4 81664 6 81664 78397 5 65215 65315 81664
41983 5 65215 7 00972 4 65315 7 78397 90283
90283 65315 48081 5 90583 6 8 81664 90287
81664 6 90583 6 81664 7 97342 90283 90583
38107 90287 8 41983 7 48001 9 90287 97342
7 78397 90283 8 8 48081 90583
38107 90287 9 00972 38107 97342
8 9 78397 41983 78397
9 9
Videos Link

• https://
www.youtube.com/watch?time_continue=53&v=nu4gDuFabIM&feat
ure=emb_logo
Activity
List of the numbers sort it using Radix Sort
[234,3478,102,2003,450,2819,611,2976,90283]
Merge Sort

Merge Sort Algorithm uses Divide and Conquer


Principle. A given array is split up recursively into
halves. The recursive calls continue to divide arrays into
halves or pieces until each piece has only one item. Then
these pieces are sorted and merged together recursively
until a sorted array is formed.
Divide and Conquer
• The concept of Divide and Conquer involves three steps:
1.Divide the problem into multiple small problems.
2.Conquer the subproblems by solving them. The idea is to
break down the problem into atomic subproblems, where
they are actually solved.
3.Combine the solutions of the subproblems to find the
solution of the actual problem.
Divide and Conquer
Merge SortAlogrithm

Procedure Mergesort( A[F…..L),F,L)


if F < L
then mid ← (F+L) / 2
Mergesort (A,F,mid)

Mergesort (A , mid+1 , L)
Merge ( A, F, mid, L)
Merge Sort Algorithm
Procedure Merge( A[F…..L), F, mid, L)

first1 F

last1 mid

first2 mid+1

last2 L
Index ← first1
for first1 to last1 and first2 to last2 do
if A[first1] < A[first2]
then temparray[index] ← A[first1]

A[first1] temparray[index]
else temparray[index] ← A[first2]

A[first2] t emparray[index]
for first1 to last1
temparray[index] ←
A[first1]
for first2temparray[index]
to last2 ←
← F←to temparray[index]
A[first2]
A[index]
for index L
Merge Sort Analysis
• Let f(n) be of number comparisons needed
to sort an n element array. Algorithm
requires at most n passes and each pass
merges n elements. Also each pass will
require n comparisons.
so for both the worst case and average case
running time will be O(n log 2 n).
Merge Sort

Divide the Array in Half


8 1 4 3 2

Sort the Halves


1 4 8 2 3

1 2 3 4 8 Merge The Halves

Copy Temporary Array


1 2 3 4 8
Example
Example
Example
Con’t
More Help
• https://
www.youtube.com/watch?time_continue=95&v=JSceec-wEyw&featur
e=emb_logo
• https://www.youtube.com/watch?
v=mB5HXBb_HY8&feature=emb_rel_pause
Activity Merge Sort
Sort The numbers using merge Sort

21 7 42 88 3 9 23 67 12 6
Bubble Sort
• In Bubble sort N-1 (N is Number of items to
be sorted) passes are made through array.
• During 1st pass each element is compared
with next one in the array and then their
position is exchanged if required according to
criteria (whether ascending or descending)
Bubble Sort
• At the end of 1st pass, the largest element gets
to its proper position (in case of ascending
order) i.e; the last location of array. So we do
not compare it during 2nd pass. The 2nd pass is
made like 1st one and at the end of 2 nd pass,
the 2nd largest element goes to its proper
position. So we do not compare it during 3 rd
pass and so on it continues.
Bubble Sort
• In this way N-1 passes are made through
array and during each pass comparisons
made are : N- number of passes
Bubble Sort Algorithm
Procedure BubbleSort( arr[1..n])
{ Bubble sort algorithm to arrange elements in
ascending order}
for j ← n to 2 do
for i ← 1 to (j-1)do
if arr[ i ]> arr [i+1] then
hold ← arr[ i ]
arr[ i ] ← arr[ i +1]
arr[i +1] ← hold
Bubble Sort Examples
• Example
• Original Array:
32 51 27 85 66 23 13 57 60 41 30 29
1 2 3 4 5 6 7 8 9 10 11 12
Total number of passes = N-1 = 12-1=11
Comparisons during each pass = N - number of passes
Bubble Sort Analysis
• There are N-1 passes
• Pass one requires N-1 comparisons and at most N-1
exchanges
• Pass two requires N-2 comparisons and at most N-2
exchanges
• So in worst case bubble requires
(N-1)+(N-2)+(N-3)+--------+1=N*(N-1)/2
comparisons
and also
(N-1)+(N-2)+(N-3)+--------+1=N*(N-1)/2 exchanges
Bubble Sort Analysis
• Now operations are:
N*(N-1)/2+ N*(N-1)/2 =N*(N-1)
• Each exchange requires 3 data moves hence there
are :
3* (N*(N-1))=3 * N²-3*N operations in
the
worst
Hencecase
Bubble Sort is O(N²) in the worst case
Also in average case Bubble Sort is O(N²)
Example
Bubble Sort
• https://www.youtube.com/watch?v=xli_FI7CuzA
• https://www.youtube.com/watch?v=18OO361--1E
Activity
4 7 3 0 8 7 9 6 1 2
Bucket Sort
• Bucket sorts work well for data sets where the
possible key values are known and relatively small
and there are on average just a few elements per
bucket.
• Time Complexity:
Best Case : O(N)
Average Case : O(N)
Worst Case : O(N*N) (i.e. Insertion Sort)
• Bucket-Sort (A)
1 n = length [A]
2 for i = 1 to n
3 do insert A [i] into list B [n A [i]]
4 for i = 0 to n - 1
5 do sort list B [i] with insertion sort
6 concatenate the lists B[0], B[1] ... B[n-1] together in order
1 n = 10
A B
2 for i = 1
1 .78 0
3 do insert A[1] into list B[10 A [1]]
2 .17 1
3 .39 2
4 .26 3
5 .72 4
6 .94 5
7 .21 6
8 .12 7 .78
9 .23 8
10 .68 9
1 n = 10
A B
2 for i = 2
1 .78 0
3 do insert A[2] into list B[10 A [2]]
2 .17 1 .17
3 .39 2
4 .26 3
5 .72 4
6 .94 5
7 .21 6
8 .12 7 .78
9 .23 8
10 .68 9
1 n = 10
A B
2 for i = 3
1 .78 0
3 do insert A[3] into list B[10 A [3]]
2 .17 1 .17
3 .39 2
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6
8 .12 7 .78
9 .23 8
10 .68 9
1 n = 10
A B
2 for i = 4
1 .78 0
3 do insert A[4] into list B[10 A [4]]
2 .17 1 .17
3 .39 2 .26
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6
8 .12 7 .78
9 .23 8
10 .68 9
1 n = 10
A B
2 for i = 5
1 .78 0
3 do insert A[5] into list B[10 A [5]]
2 .17 1 .17
3 .39 2 .26
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6
8 .12 7 .78 .72
9 .23 8
10 .68 9
1 n = 10
A B
2 for i = 6
1 .78 0
3 do insert A[6] into list B[10 A [6]]
2 .17 1 .17
3 .39 2 .26
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6
8 .12 7 .78 .72
9 .23 8
10 .68 9 .94
1 n = 10
A B
2 for i = 7
1 .78 0
3 do insert A[7] into list B[10 A [7]]
2 .17 1 .17
3 .39 2 .26 .21
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6
8 .12 7 .78 .72
9 .23 8
10 .68 9 .94
1 n = 10
A B
2 for i = 8
1 .78 0
3 do insert A[8] into list B[10 A [8]]
2 .17 1 .17 .12
3 .39 2 .26 .21
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6
8 .12 7 .78 .72
9 .23 8
10 .68 9 .94
1 n = 10
A B
2 for i = 9
1 .78 0
3 do insert A[9] into list B[10 A [9]]
2 .17 1 .17 .12
3 .39 2 .26 .21 .23
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6
8 .12 7 .78 .72
9 .23 8
10 .68 9 .94
1 n = 10
A B
2 for i = 10
1 .78 0
3 do insert A[10] into list B[10 A [10]]
2 .17 1 .17 .12
3 .39 2 .26 .21 .23
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6 .68

8 .12 7 .78 .72


9 .23 8
10 .68 9 .94
4- i=0
A B
5 - do sort list B[0] with insertion sort
1 .78 0
2 .17 1 .17 .12
3 .39 2 .26 .21 .23
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6 .68

8 .12 7 .78 .72


9 .23 8
10 .68 9 .94
4- i=1
A B
5 - do sort list B[1] with insertion sort
1 .78 0
2 .17 1 .12 .17
3 .39 2 .26 .21 .23
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6 .68

8 .12 7 .78 .72


9 .23 8
10 .68 9 .94
4- i=2
A B
5 - do sort list B[2] with insertion sort
1 .78 0
2 .17 1 .12 .17
3 .39 2 .21 .23 .26
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6 .68

8 .12 7 .78 .72


9 .23 8
10 .68 9 .94
4- i=3
A B
5 - do sort list B[3] with insertion sort
1 .78 0
2 .17 1 .12 .17
3 .39 2 .21 .23 .26
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6 .68

8 .12 7 .78 .72


9 .23 8
10 .68 9 .94
4- i=4
A B
5 - do sort list B[4] with insertion sort
1 .78 0
2 .17 1 .12 .17
3 .39 2 .21 .23 .26
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6 .68

8 .12 7 .78 .72


9 .23 8
10 .68 9 .94
4- i=5
A B
5 - do sort list B[5] with insertion sort
1 .78 0
2 .17 1 .12 .17
3 .39 2 .21 .23 .26
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6 .68

8 .12 7 .78 .72


9 .23 8
10 .68 9 .94
4- i=6
A B
5 - do sort list B[6] with insertion sort
1 .78 0
2 .17 1 .12 .17
3 .39 2 .21 .23 .26
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6 .68

8 .12 7 .78 .72


9 .23 8
10 .68 9 .94
4- i=7
A B
5 - do sort list B[7] with insertion sort
1 .78 0
2 .17 1 .12 .17
3 .39 2 .21 .23 .26
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6 .68

8 .12 7 .72 .78


9 .23 8
10 .68 9 .94
4- i=8
A B
5 - do sort list B[8] with insertion sort
1 .78 0
2 .17 1 .12 .17
3 .39 2 .21 .23 .26
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6 .68

8 .12 7 .72 .78


9 .23 8
10 .68 9 .94
4- i=9
A B
5 - do sort list B[9] with insertion sort
1 .78 0
2 .17 1 .12 .17
3 .39 2 .21 .23 .26
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6 .68

8 .12 7 .72 .78


9 .23 8
10 .68 9 .94
Example
Example
Bucket Sort
• https://www.youtube.com/watch?v=geVyIsFpxUs
• https://www.cs.usfca.edu/~galles/visualization/BucketSort.html
• https://www.youtube.com/watch?v=VuXbEb5ywrU
Activity Sort the array using Bucket
Sort
.39 .23 .78 .49 .56 .31 .89 .17 .41 .90 .12

• https://www.youtube.com/watch?v=geVyIsFpxUs
Note: Must watch this video to solve this Activity
Insertion Sort
Example
Example
Example
Example
Con’t
Example

You might also like