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

SelectionSort

The document provides an overview of selection sort, a sorting algorithm. It demonstrates the steps of selection sort visually with diagrams showing the sorting of sample data from an unsorted to sorted state. Key steps of selection sort include finding the minimum element, swapping it with the first element, and repeating this process until the array is fully sorted. The time complexity of selection sort is O(n^2) in all cases (best, average, worst).

Uploaded by

Amar Thakur
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

SelectionSort

The document provides an overview of selection sort, a sorting algorithm. It demonstrates the steps of selection sort visually with diagrams showing the sorting of sample data from an unsorted to sorted state. Key steps of selection sort include finding the minimum element, swapping it with the first element, and repeating this process until the array is fully sorted. The time complexity of selection sort is O(n^2) in all cases (best, average, worst).

Uploaded by

Amar Thakur
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

Lovely Professional University, Punjab

Data Structures

Lecture: Selection Sort


Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Min

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2

Min

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Min

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Min

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Min

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 5 6

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 5 6
DONE!
Comparison

Data Movement

Sorted
Selection Sort
Selection_Sort (A, n)

1. Set j = 0.
2. Repeat While j < n – 1:
3. Set Min = j and i = j+1.
4. Repeat step 5 while i < n:
5. if(a[i] < a[Min]), then:
Min = i.
[End of step 4 loop.]
6. if (Min != j), then:
7. swap (a[j], a[Min]).
8.[End of step 2 loop.]
9.Return.
Comparison Table of Sorting
Best Average Worst
Case Case Case

Bubble Sort O(n) O(n2) O(n2)


Insertion Sort O(n) O(n2) O(n2)
Selection Sort O(n2) O(n2) O(n2)
Merge Sort O(n log n) O(n log n) O(n log n)
Quick Sort O(n log n) O(n log n) O(n2)
Heap Sort O(n log n) O(n log n) O(n log n)
Questions?

You might also like