0% found this document useful (0 votes)
25 views7 pages

AOA Experiment 1

The document outlines an experiment to implement the Selection Sort algorithm, detailing its aim, objectives, and theoretical background. It explains the algorithm's process, provides a code example, and discusses its time complexity of O(n²) while highlighting its advantages in terms of memory usage. The conclusion emphasizes Selection Sort's effectiveness for small datasets and its role in understanding sorting techniques.

Uploaded by

ashcraftgamer8
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)
25 views7 pages

AOA Experiment 1

The document outlines an experiment to implement the Selection Sort algorithm, detailing its aim, objectives, and theoretical background. It explains the algorithm's process, provides a code example, and discusses its time complexity of O(n²) while highlighting its advantages in terms of memory usage. The conclusion emphasizes Selection Sort's effectiveness for small datasets and its role in understanding sorting techniques.

Uploaded by

ashcraftgamer8
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/ 7

Experiment No.

1
To implement Selection Sort
Date of Performance: 07/01/2025
Date of Submission: 22/01/2025
Experiment No. 1

Title: To implement selection sort.

Aim: To study, implement and Analyze Selection Sort Algorithm

Objective: To introduce the methods of designing and analyzing algorithms

Theory: Selection sort is a sorting algorithm, specifically an in-place comparison sort.


Selection sort is noted for its simplicity, and it has performance advantages over more
complicated algorithmsin certain situations, particularly where auxiliary memory is limited.

The algorithm divides the input list into two parts: the sub list of items already sorted,
which is built up from left to right at the front (left) of the list, and the sub list of items
remaining to be sorted that occupy the rest of the list. Initially, the sorted sub list is empty
and the unsorted sub list is the entire input list. The algorithm proceeds by finding the
smallest (or largest, depending on sorting order) element in the unsorted sub list,
exchanging it with the leftmost unsorted element (putting it in sorted order), and moving
the sub-list boundaries one elementto the right.

Example:
Sort the given array using selection sort. arr[] = 64 25 12 22 11

11 25 12 22 64 Find the minimum element in arr[0...4] and place it at


beginning
11 12 25 22 64 Find the minimum element in arr[1...4] and place it at
beginning of arr[1...4]
11 12 22 25 64 Find the minimum element in arr[2...4] and place it at
beginning of arr[2...4]
11 12 22 25 64 Find the minimum element in arr[3...4] and place it at
beginning of arr[3...4]

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Algorithm and Complexity:

The recurrence relation for selection sort is:


T(n) = 1 for n=0
= T(n – 1) + n for n>0 ---- 1
From above equation,
T (n – 1) = T(n – 2) + (n – 1)
Use above in equation 1
T(n) = T(n – 2) + (n – 1) + n----2
Let T(n – 2) = T(n – 3) + n – 2
Use above in equation 2
T(n) = T(n – 3) + (n – 2) + (n – 1) + n
After k iterations,
T(n) = T(n – k) + (n – k + 1) + (n – k + 2) + ..… + (n – 1) + n
When k approaches to n,
T(n) = T(0) + 1 + 2 + 3 + … + (n –1) + n
T(0) = 0,
T(n) = 1 + 2 + 3 + … + n
= n(n + 1) / 2
= (n2 /2) + (n/2)
T(n) = O(max( (n2 /2) + (n/2) ))
= O(n2 / 2)
= O(n2)
T(n) = O(n2)

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Code:

#include <stdio.h>

void selectionSort(int arr[], int n) {


int i, j, min_idx, temp;
for (i = 0; i < n - 1; i++) {
min_idx = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;

printf("Iteration %d: ", i + 1);


for (int k = 0; k < n; k++) {
printf("%d ", arr[k]);
}
printf("\n\n");
}
}

int main() {
int n;
printf("Enter the size of the array: ");

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Original Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n\n");
selectionSort(arr, n);
printf("Sorted Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n\n");
return 0;
}

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Output:

Conclusion:

The Selection Sort algorithm is a simple and effective sorting technique that works well for
small datasets. It systematically selects the smallest element from the unsorted list and places
it in its correct position, gradually building a sorted sequence. Though its time complexity is
O(n²), making it inefficient for large datasets, it is advantageous when memory usage is a
concern since it operates in-place with O(1) auxiliary space. Despite its limitations, Selection
Sort serves as a fundamental algorithm for understanding sorting techniques and algorithm
analysis.

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering

You might also like