Practical4final (Ada)
Practical4final (Ada)
S:-28/01/25
AIM:-
Implementation and comparison of Bubble sort and Selection sort algorithm.
THEORY:-
Introduction
Sorting is a fundamental operation in computer science that involves arranging data in a specific
order (ascending or descending). Efficient sorting is crucial for optimizing searching algorithms
and data retrieval processes. In this report, we analyze two basic sorting algorithms: Bubble Sort
and Selection Sort, and compare their efficiency.
Bubble Sort
Definition
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares
adjacent elements, and swaps them if they are in the wrong order. This process is repeated until
the entire list is sorted.
Advantages
Easy to understand and implement.
Works well with small datasets.
Stable Sort (maintains the relative order of equal elements).
Disadvantages
Inefficient for large datasets (O(n²) time complexity).
Requires multiple passes even if the array is almost sorted.
Selection Sort
Definition
Selection Sort works by selecting the smallest (or largest) element from the unsorted portion and
swapping it with the first unsorted element. The process is repeated until the entire array is
sorted.
Advantages
Performs fewer swaps compared to Bubble Sort.
Simple and easy to implement for small datasets.
Requires only O(1) extra memory, making it a good choice for memory-constrained
environments.
Disadvantages
O(n²) time complexity, making it inefficient for large datasets.
Page | 30
MCO23383
D.O.P:-21/01/25 PRACTICAL 4 D.O.S:-28/01/25
Not a stable sort, as it may change the relative order of equal elements.
Slower than other advanced sorting algorithms like Merge Sort or Quick Sort.
Both Bubble Sort and Selection Sort are simple sorting algorithms best suited for small datasets
due to their O(n²) time complexity. While Bubble Sort is stable, Selection Sort performs fewer
swaps. However, for larger datasets, more efficient algorithms like Merge Sort or Quick Sort are
preferred.
PSEUDOCODE:-
Page | 31
MCO23383
D.O.P:-21/01/25 PRACTICAL 4 D.O.S:-28/01/25
Bubble Sort
BubbleSort(A, n)
1. For pass = 1 to n-1 do
a. swapped = false
b. For i = 0 to n-pass-1 do
If A[i] > A[i+1] then
Swap(A[i], A[i+1])
swapped = true
c. If swapped = false then
Break
2. Return sorted array A
Selection Sort
SelectionSort(A, n)
1. For i = 0 to n-2 do
a. minIndex = i
b. For j = i+1 to n-1 do
If A[j] < A[minIndex] then
minIndex = j
c. Swap(A[i], A[minIndex])
2. Return sorted array A
ALGORITHM:-
Page | 32
MCO23383
D.O.P:-21/01/25 PRACTICAL 4 D.O.S:-28/01/25
Selection Sort
1. Input: An array A of size n
2. Output: Sorted array A in ascending order
3. Steps:
1. For i = 0 to n-2
a. Set minIndex = i
b. For j = i+1 to n-1:
If A[j] < A[minIndex], set minIndex = j
c. Swap A[i] and A[minIndex]
2. End For
4. Return sorted array A
Bubble Sort
CODE IMPLEMENTATION
Page | 33
MCO23383
D.O.P:-21/01/25 PRACTICAL 4 D.O.S:-28/01/25
#include <iostream>
#include <string>
#include <chrono>
using namespace std;
using namespace std::chrono;
Page | 34
MCO23383
D.O.P:-21/01/25 PRACTICAL 4 D.O.S:-28/01/25
Page | 35
MCO23383
D.O.P:-21/01/25 PRACTICAL 4 D.O.S:-28/01/25
Page | 36
MCO23383
D.O.P:-21/01/25 PRACTICAL 4 D.O.S:-28/01/25
Page | 37
MCO23383
D.O.P:-21/01/25 PRACTICAL 4 D.O.S:-28/01/25
int main() {
int choice, n;
cout << "Choose data type: 1 for Integers, 2 for Strings: ";
cin >> choice;
cout << "Enter the number of elements: ";
cin >> n;
Page | 38
MCO23383
D.O.P:-21/01/25 PRACTICAL 4 D.O.S:-28/01/25
if (choice == 1) {
int arr[n];
takeInput(arr, n);
compareSortingAlgorithms(arr, n);
}
else if (choice == 2) {
string arr[n];
takeInput(arr, n);
compareSortingAlgorithms(arr, n);
}
else {
cout << "Invalid choice!";
}
return 0;
}
OUTPUT:-
Page | 39
MCO23383
D.O.P:-21/01/25 PRACTICAL 4 D.O.S:-28/01/25
Integers
Strings
Page | 40
MCO23383