0% found this document useful (0 votes)
3 views10 pages

Algo Lec 4

Uploaded by

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

Algo Lec 4

Uploaded by

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

Selection Sort :

Selection sort is a simple and intuitive


sorting algorithm. It works by
repeatedly finding the smallest (or
largest, depending on the order)
element from the unsorted portion of
the list and moving it to the sorted
portion.
The algorithm divides the array into
two parts:
1. Sorted portion: Starts empty
and grows as elements are sorted.
2. Unsorted portion: Shrinks as
elements are moved to the sorted
portion.

How Selection Sort Works


1. Start with the entire list as
the unsorted portion.
2. Find the smallest element in the
unsorted portion.
3. Swap it with the first element of
the unsorted portion (put it in its
Design and Analysis of Algorithms 2

correct position in the sorted


portion).
4. Move the boundary between the
sorted and unsorted portions one
step forward.
5. Repeat the process for the rest of
the unsorted portion until the entire
list is sorted.

Step-by-Step Example
Input Array:
[64, 25, 12, 22, 11]
We want to sort this in ascending order.

Step 1: Find the smallest element


The unsorted portion is [64, 25, 12,

22, 11].
The smallest element is 11.

Swap 11 with the first element (64).


Array after Step 1: [11, 25, 12, 22,


64].

2 2
Design and Analysis of Algorithms 3

Step 2: Find the smallest element in


the remaining unsorted portion
The unsorted portion is [25, 12, 22,

64].
The smallest element is 12.

Swap 12 with the first element of


the unsorted portion (25).


Array after Step 2: [11, 12, 25, 22,

64].

Step 3: Find the smallest element in


the remaining unsorted portion
The unsorted portion is [25, 22, 64].

The smallest element is 22.


Swap 22 with the first element of


the unsorted portion (25).


Array after Step 3: [11, 12, 22, 25,

64].

Step 4: Find the smallest element in


the remaining unsorted portion
The unsorted portion is [25, 64].

The smallest element is 25.


3 3
Design and Analysis of Algorithms 4

 No need to swap, as 25 is already in


the correct position.
 Array after Step 4: [11, 12, 22, 25,
64].

Step 5: Final Step


The last element (64) is already

sorted, so no action is needed.


Final Sorted Array: [11, 12, 22,

25, 64].

Key Points
Selection sort always performs n-1

comparisons for n elements.


The algorithm is simple but not

very efficient for large datasets


because its time complexity
is O(n2) in all cases (best, average,
and worst).

4 4
Design and Analysis of Algorithms 5

Algorithm:

selection_sort(arr):
n = len(arr)
for i to(n)
min_index = i
for j in range(i + 1, n):
if arr[j] < arr[min_index]:
min_index = j

arr[i], arr[min_index] = arr[min_index], arr[i]

arr = [64, 25, 12, 22, 11]

1. How Time Complexity is


Calculated in Selection Sort
Selection sort works by:
1. Scanning the unsorted portion of
the array to find the smallest (or
largest) element.
2. Swapping it into the correct
position.
The algorithm makes the same number
of comparisons regardless of the

5 5
Design and Analysis of Algorithms 6

arrangement of elements. For an array


of size n:
First pass: n−1comparisons (find

the smallest element).


Second pass: n−1comparisons.

This simplifies to O(n2).

2. Time Complexity in Different


Cases
Best Case (Already Sorted)
Even if the array is already sorted,

the algorithm still performs the


same O(n2) comparisons because it
always scans the unsorted portion to
find the smallest element.
Swaps will be fewer (or none), but

this doesn’t affect the time


complexity significantly.
Worst Case (Reverse Sorted)
In the worst case, the array is in

descending order. The algorithm


still performs O(n2) comparisons
6 6
Design and Analysis of Algorithms 7

and swaps for every element to


place them in the correct order.
Average Case
In the average case, the array is in

random order. The number of


comparisons is the same as in the
best and worst cases because
selection sort always
makes n(n−1) comparisons.

Summary Table
Number of Time
Case Comparisons Complexity
Best Case 2n(n−1) O(n2)
Worst Case 2n(n−1) O(n2)
Average
Case 2n(n−1) O(n2)

7 7
Design and Analysis of Algorithms 8

Space Complexity

 Selection Sort is in-place, meaning


it doesn’t require extra memory
apart from a few variables for
swapping.
 Space Complexity:O(1).

Key point: Selection sort’s time


complexity is O(n2), making it
inefficient for large datasets but useful
for understanding basic sorting
principles.

Application of selection sort:


While selection sort is not commonly
used in modern software due to its
inefficiency compared to other sorting
algorithms, it has some practical
applications in specific scenarios where
its simplicity and ease of
implementation are advantageous. Here
8 8
Design and Analysis of Algorithms 9

are some real-life applications of


selection sort:

1. Sorting Small Data Sets


Selection sort is easy to understand and
implement, making it a good choice for
sorting small datasets where efficiency
is less critical.
Example: A student writing a simple
program to sort a short list of names,
scores, or other data.
2. Embedded Systems with Limited
Resources
Selection sort has a space complexity
of O(1), meaning it sorts in place
without requiring extra memory. This
makes it suitable for systems with very
limited memory.
Example: Sorting a small list of sensor
readings in an embedded device where
memory is constrained.
3.When Minimizing Swaps is
Important
9 9
Design and Analysis of Algorithms 10

Selection sort performs at most


n−1 swaps, which can be advantageous
if swapping elements is costly (e.g., in
systems where memory writes are
expensive).
Example: Sorting in scenarios
involving flash memory, where
minimizing writes can extend the
device’s lifes.
4. As a Backup in Simple Systems
Selection sort is easy to implement as a
fallback in systems where no optimized
sorting libraries are available.

10 10

You might also like