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

DSA Lab Manual(Selection Sort)

The document provides a lab manual for implementing the Selection Sort algorithm in C++. It explains the algorithm's characteristics, including its time complexity of O(n²) and in-place sorting nature, and includes a C++ code implementation along with execution steps. The conclusion notes that while Selection Sort is effective for small datasets, it is not efficient for larger ones.

Uploaded by

Irfan Ul Haq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

DSA Lab Manual(Selection Sort)

The document provides a lab manual for implementing the Selection Sort algorithm in C++. It explains the algorithm's characteristics, including its time complexity of O(n²) and in-place sorting nature, and includes a C++ code implementation along with execution steps. The conclusion notes that while Selection Sort is effective for small datasets, it is not efficient for larger ones.

Uploaded by

Irfan Ul Haq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Data Structures and Algorithms Lab Manual

Experiment: Selection Sort Algorithm


Objective
To understand and implement the Selection Sort algorithm using C++ and analyze its working
and efficiency.
Theory
Selection Sort is a simple comparison-based sorting algorithm. It divides the input list into two
parts: the sorted part and the unsorted part. Initially, the sorted part is empty, and the unsorted
part is the entire list. The algorithm repeatedly selects the smallest (or largest, depending on
sorting order) element from the unsorted part and moves it to the sorted part.
Characteristics of Selection Sort:
1. In-place Sorting: It requires no additional storage, as sorting is done in the array itself.
2. Time Complexity:
o Best case: O(n²)
o Worst case: O(n²)
o Average case: O(n²)
3. Space Complexity: O(1)
4. Stability: Selection Sort is not a stable sorting algorithm.
Algorithm
1. Start with the first element as the minimum.
2. Compare the current minimum with the remaining elements in the array.
3. Swap the smallest element with the current minimum.
4. Move to the next element and repeat steps 2-3 until the array is sorted.

C++ Code Implementation


#include <iostream>
using namespace std;

// Function to perform selection sort


void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
// Find the minimum element in the unsorted part
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
swap(arr[minIndex], arr[i]);
}
}

// Function to print an array


void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;

int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

cout << "Array before sorting: ";


printArray(arr, n);

selectionSort(arr, n);

cout << "Array after sorting: ";


printArray(arr, n);

return 0;
}
Execution Steps
1. Input the size of the array and its elements.
2. Display the original array.
3. Call the selectionSort function to sort the array.
4. Display the sorted array.

Sample Input and Output

Input:
Enter the number of elements: 5
Enter the elements: 64 34 25 12 22

Output:
Array before sorting: 64 34 25 12 22
Array after sorting: 12 22 25 34 64

Conclusion
The Selection Sort algorithm is an effective way to sort small datasets. It is easy to understand
and implement but may not be efficient for larger datasets due to its O(n²) time complexity.

You might also like