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

Selection Sort A Simple Sorting Algorithm

Uploaded by

youngcoders6969
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Selection Sort A Simple Sorting Algorithm

Uploaded by

youngcoders6969
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Selection Sort: A

Simple Sorting
Algorithm

Sorting is a fundamental operation in computer science, and


selection sort is one of the simplest sorting algorithms. It
works by repeatedly finding the minimum element from the
unsorted part of the array and swapping it with the first
element of the unsorted part.
by Young Coders
Understanding Sorting
Algorithms

1 What is Sorting? 2 Why is Sorting


Important?
Sorting is the process of
arranging elements in a Sorting is a crucial
specific order (e.g., operation that enables
ascending or descending) efficient data manipulation,
to make data more searching, and analysis in
organized and easier to various applications, from
search. databases to video
streaming.

3 Types of Sorting Algorithms


There are many sorting algorithms, each with its own strengths
and weaknesses, such as Bubble Sort, Insertion Sort, Merge
Sort, and Quick Sort.
The Concept of Selection
Sort

1 Find Minimum
Scan the unsorted part of the array to find the
minimum element.

2 Swap with First


Swap the minimum element with the first element
of the unsorted part.

3 Repeat
Repeat the process until the entire array is sorted.
Step-by-Step Explanation of
Selection Sort
Step 1
Find the minimum element in the unsorted part of the array.

Step 2
Swap the minimum element with the first element of the unsorted par

Step 3
Repeat the process until the entire array is sorted.
Implementing Selection Sort in C
Pseudocode C Code

for i = 0 to n-1: void selectionSort(int arr[], int n) {


min_idx = i for (int i = 0; i < n-1; i++) {
for j = i+1 to n-1: int min_idx = i;
if arr[j] < arr[min_idx]: for (int j = i+1; j < n; j++)
min_idx = j if (arr[j] < arr[min_idx])
swap arr[i] and arr[min_idx] min_idx = j;
swap(&arr[i], &arr[min_idx]);
}
}
Analyzing the Time
Complexity of Selection Sort

1 Time Complexity 2 Comparison-based


Selection sort has a time Selection sort is a
complexity of O(n^2), comparison-based sorting
which makes it less algorithm, meaning it
efficient for large datasets relies on comparing
compared to algorithms elements to determine
like Merge Sort or Quick their relative order.
Sort.

3 Space Complexity
Selection sort has a space complexity of O(1), as it only
requires a constant amount of additional space to store
temporary variables.

You might also like