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

CSC2302_Lecture11

The lecture covers sorting algorithms, emphasizing their importance in reducing problem complexity and identifying data patterns. It discusses three types of sorting algorithms: Selection Sort, Bubble Sort, and Insertion Sort, detailing their mechanisms and time complexities. Additionally, a homework assignment is provided to implement a recursive version of Selection Sort.

Uploaded by

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

CSC2302_Lecture11

The lecture covers sorting algorithms, emphasizing their importance in reducing problem complexity and identifying data patterns. It discusses three types of sorting algorithms: Selection Sort, Bubble Sort, and Insertion Sort, detailing their mechanisms and time complexities. Additionally, a homework assignment is provided to implement a recursive version of Selection Sort.

Uploaded by

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

CSC 2302

Data Structures

Lecture 10: Sorting Algorithms


Raifa Akkaoui
School of Science and Engineering, AUI
[email protected]

Spring 2024
Why Sorting?

• Reduce the complexity of a problem


• Searching an element in a massive dataset becomes relatively easy if the elements
are sorted
• e.g., binary search from O(n) to O(logn)

• Useful for identifying patterns and trends in data

10/02/2024 Data Structures 2


Types of Sorting Algorithm

• Selection Sort

• Bubble Sort

• Insertion Sort

10/02/2024 Data Structures 3


Selection Sort

• Selection sort is a simple and efficient sorting algorithm that works


by repeatedly selecting the smallest (or largest) element from the
unsorted portion of the list and moving it to the sorted portion of
the list (by swapping it with the first element of the unsorted list)

• First pass: the whole array is traversed from index 0 to 4 sequentially,


it is clear that 11 is the lowest value, thus swap it with 64
• Second pass: from the second position, where 25 is present, again we
traverse the rest of the array in a sequential manner, we found that
12 is the second lowest value, thus we swap it with 25
• …
• Last pass: the largest value present in the array automatically get
placed at the last position in the array
• The resulted array is the sorted array

10/02/2024 Data Structures 4


Selection Sort (Cont.)

10/02/2024 Data Structures 5


Selection Sort (Cont.)

• Analysis of Selection Sort

• Inside the body of the first loop


• Inner loop that finds min_idx, which takes n-i steps, where i is the value of the first for loop
variable
• Swapping the values which takes constant time, so we’ll count that as 1
• Thus, the total time is:

𝑛−1 𝑛−1
𝑛 𝑛−1 𝑛(𝑛 + 3)
෍𝑛−𝑖+1=𝑛 𝑛+1 − ෍𝑖 =𝑛 𝑛+1 − =
2 2
𝑖=0 𝑖=0

• Thus, the running time of selection sort is O(n²)

10/02/2024 Data Structures 6


Bubble Sort

• Bubble Sort is the simplest sorting


algorithm that works by repeatedly
swapping the adjacent elements if they
are in the wrong order

10/02/2024 Data Structures 7


Bubble Sort (Cont.)

10/02/2024 Data Structures 8


Insertion Sort

• Insertion sort is a simple sorting algorithm that works


similar to the way you sort playing cards in your
hands
• The array is virtually split into a sorted and an
unsorted part and values from the unsorted part are
picked and placed at the correct position in the
sorted part

10/02/2024 Data Structures 9


Insertion Sort (Cont.)

10/02/2024 Data Structures 10


Selection Sort (Homework)

• Recursive Selection Sort

• Try writing a recursive version of the selection sort algorithm


• if n is 1 → array is sorted
• else
• Place the largest value in last array element
• Sort the subarray which excludes the last array element (array[0] . . array[n-2])

10/02/2024 Data Structures 11


Thank you!
Your attendance and engagement in this
lecture are more than appreciated : )

You might also like