0% found this document useful (0 votes)
7 views11 pages

Practical4final (Ada)

This document outlines a practical implementation and comparison of Bubble Sort and Selection Sort algorithms, highlighting their definitions, advantages, disadvantages, and time complexities. Both algorithms are simple and best suited for small datasets due to their O(n²) time complexity, with Bubble Sort being stable and Selection Sort requiring fewer swaps. The document includes pseudocode, code implementation in C++, and a comparison of their performance on integer and string data types.

Uploaded by

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

Practical4final (Ada)

This document outlines a practical implementation and comparison of Bubble Sort and Selection Sort algorithms, highlighting their definitions, advantages, disadvantages, and time complexities. Both algorithms are simple and best suited for small datasets due to their O(n²) time complexity, with Bubble Sort being stable and Selection Sort requiring fewer swaps. The document includes pseudocode, code implementation in C++, and a comparison of their performance on integer and string data types.

Uploaded by

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

D.O.P:-21/01/25 PRACTICAL 4 D.O.

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.

Comparison of Bubble Sort and Selection Sort

Feature Bubble Sort Selection Sort

Time Complexity (Worst & Average Case) O(n²) O(n²)

Time Complexity (Best Case) O(n) (if optimized) O(n²)

Swaps More swaps Fewer swaps

Comparisons O(n²) O(n²)

Stable Sort Yes No

Efficiency on Large Datasets Inefficient Inefficient

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

1. Input: An array A of size n


2. Output: Sorted array A in ascending order
3. Steps:
a. Repeat for (n-1) passes
a. Set a flag swapped = false
b. For i = 0 to n-2:
i. If A[i] > A[i+1], swap A[i] and A[i+1]
ii. Set swapped = true
c. If no swaps occurred (swapped = false), break
b. End For
4. Return sorted array A

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;

// Function for Bubble Sort (Integers)


void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}

// Function for Bubble Sort (Strings)


void bubbleSort(string arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}

Page | 34
MCO23383
D.O.P:-21/01/25 PRACTICAL 4 D.O.S:-28/01/25

// Function for Selection Sort (Integers)


void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
swap(arr[i], arr[minIdx]);
}
}

// Function for Selection Sort (Strings)


void selectionSort(string arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
swap(arr[i], arr[minIdx]);
}
}

// Function to take user input for integers


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

Page | 35
MCO23383
D.O.P:-21/01/25 PRACTICAL 4 D.O.S:-28/01/25

cout << "Enter " << n << " elements: ";


for (int i = 0; i < n; i++) {
cin >> arr[i];
}
}

// Function to take user input for strings


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

// Function to print the array


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

// Function to print the array


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

Page | 36
MCO23383
D.O.P:-21/01/25 PRACTICAL 4 D.O.S:-28/01/25

// Function to compare sorting algorithms


void compareSortingAlgorithms(int arr[], int n) {
int arrCopy[n];
copy(arr, arr + n, arrCopy);

auto start = high_resolution_clock::now();


bubbleSort(arrCopy, n);
auto stop = high_resolution_clock::now();
auto bubbleTime = duration_cast<nanoseconds>(stop - start).count();

copy(arr, arr + n, arrCopy);


start = high_resolution_clock::now();
selectionSort(arrCopy, n);
stop = high_resolution_clock::now();
auto selectionTime = duration_cast<nanoseconds>(stop - start).count();

cout << "Sorted using Bubble Sort: ";


printArray(arrCopy, n);
cout << "Bubble Sort Time: " << bubbleTime << " nanoseconds\n";

cout << "Sorted using Selection Sort: ";


printArray(arrCopy, n);
cout << "Selection Sort Time: " << selectionTime << " nanoseconds\n";
}

void compareSortingAlgorithms(string arr[], int n) {


string arrCopy[n];
copy(arr, arr + n, arrCopy);

Page | 37
MCO23383
D.O.P:-21/01/25 PRACTICAL 4 D.O.S:-28/01/25

auto start = high_resolution_clock::now();


bubbleSort(arrCopy, n);
auto stop = high_resolution_clock::now();
auto bubbleTime = duration_cast<nanoseconds>(stop - start).count();

copy(arr, arr + n, arrCopy);


start = high_resolution_clock::now();
selectionSort(arrCopy, n);
stop = high_resolution_clock::now();
auto selectionTime = duration_cast<nanoseconds>(stop - start).count();

cout << "Sorted using Bubble Sort: ";


printArray(arrCopy, n);
cout << "Bubble Sort Time: " << bubbleTime << " nanoseconds\n";

cout << "Sorted using Selection Sort: ";


printArray(arrCopy, n);
cout << "Selection Sort Time: " << selectionTime << " nanoseconds\n";
}

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

You might also like