0% found this document useful (0 votes)
44 views9 pages

1DSL Practical

The document summarizes an experiment to implement bubble sort and insertion sort algorithms in C++. It includes the theory behind each algorithm, example code implementations in Turbo C++ and C++11/14, and expected output. Bubble sort is a simple sorting algorithm that compares and swaps adjacent elements. Insertion sort iteratively builds a sorted output by inserting elements into their correct position. The code examples demonstrate implementing the sorting algorithms on an array and printing the output before and after sorting.

Uploaded by

Sushant Wagh
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)
44 views9 pages

1DSL Practical

The document summarizes an experiment to implement bubble sort and insertion sort algorithms in C++. It includes the theory behind each algorithm, example code implementations in Turbo C++ and C++11/14, and expected output. Bubble sort is a simple sorting algorithm that compares and swaps adjacent elements. Insertion sort iteratively builds a sorted output by inserting elements into their correct position. The code examples demonstrate implementing the sorting algorithms on an array and printing the output before and after sorting.

Uploaded by

Sushant Wagh
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/ 9

Sushant Wagh Div-A Roll No.

60

Practical No. 1
Aim: To Implement different sorting algorithms like bubble sort and insertion sort.

A) Bubble Sort.

Theory:
1. What is C++?
○ C++ is a programming language which directly maps our code to the
hardware and provides zero-overhead for abstractions like classes with
constructors and destructors, inheritance, generic programming and
functional programming techniques.
2. C++ Fundamentals(That were used to in this practical session)
○ Header
i. Header files’ (.h/.hpp) are text files whose primary purpose is to
propagate declarations to code files. They allow us to put declarations
in one location and then import them wherever we need them. This
can save a lot of typing in multi-file programs, eg. iostream.
○ Variables
i. An object is a region of storage (usually memory) that has a value and
other associated properties and a named object is called a variable,
eg. int i=8; // i is a variable.
○ Methods/Functions
i. A function is a reusable sequence of statements designed to do a
particular job, eg. void do_something(){}
○ Class
i. A class is a blueprint for creating objects (a particular data structure),
providing initial values for state (member variables or attributes), and
implementations of behavior (member functions or methods),
ii. The class keyword defines a new program-defined type called a class,
eg. class A{}
○ Control Statements
i. Conditional statements
1. A conditional statement is a statement that specifies whether
some associated statement(s) should be executed or not, eg.
if(condition){do_something();} else{}
ii. Loop
1. while
a. The while statement (also called a while loop) is the
simplest of the three loop types that C++ provides, and
it has a definition very similar to that of an if statement,
eg while(condition){do_something();}
2. for

1
Sushant Wagh Div-A Roll No. 60

a. The for statement (also called a for loop) is preferred


when we have an obvious loop variable because it lets
us easily and concisely define, initialize, test, and
change the value of loop variables, eg. for(int
i=0;i<arr.size();++i){}, for(auto ele: arr){}
3. do…while
a. A do while statement is a looping construct that works
just like a while loop, except the statement always
executes at least once.
○ Scope
i. An identifier’s scope determines where the identifier can be accessed
within the source code.
○ Used Headers
i. iostream
1. This header is part of the Input/output library.
3. What is Bubble sort and how does it work ?
○ Bubble sort is a simple sorting algorithm. This sorting algorithm is
comparison-based algorithm in which each pair of adjacent elements is
compared and the elements are swapped if they are not in order. Each time
the algorithm goes through the list it is called a ‘pass’.
○ Time complexities based on different scenarios:
i. Worst Case: O(n^2)
ii. Average Case: 𝛉(n^2)
iii. Best Case: 𝛀(n)

Code:

Turbo C++:

#include <conio.h>
#include <iostream.h>
#define ARRAY_SIZE 5

class SimpleSorts {
int arr[ARRAY_SIZE];

static void swap(int &a, int &b);


void shift_elements_till(int start, int end);

public:
SimpleSorts();
void bubble_sort();
void print();
};

SimpleSorts::SimpleSorts() {
int temp_arr[] = {1, 6, 5, 2, 4};
for (int i = 0; i < ARRAY_SIZE; ++i) {

2
Sushant Wagh Div-A Roll No. 60

arr[i] = temp_arr[i];
}
}

void SimpleSorts::swap(int &a, int &b) {


int temp = a;
a = b;
b = temp;
}
void SimpleSorts::bubble_sort() {
for (int i = 0; i < ARRAY_SIZE; ++i) {
for (int j = 0; j < ARRAY_SIZE; ++j) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}

void SimpleSorts::print() {
cout << "Array: ";
for (int i = 0; i < ARRAY_SIZE; ++i) {
cout << arr[i] << ' ';
}
cout << '\n';
}
void runBubbleSortDemo() {
SimpleSorts *simple_sorts = new SimpleSorts();
cout << "Before sorting:-\n";
simple_sorts->print();
simple_sorts->bubble_sort();
cout << "After sorting using Bubble Sort:-\n";
simple_sorts->print();
delete simple_sorts;
}

int main() {
clrscr();
print_author_info();
runBubbleSortDemo();
getch();
return 0;
}

C++11:

#include <algorithm>
#include <initializer_list>
#include <iostream>

3
Sushant Wagh Div-A Roll No. 60

#include <vector>

template <typename T> class SimpleSorts {

const std::vector<T> m_org_array{};


std::vector<T> m_array{};

public:
SimpleSorts(std::initializer_list<T> list)
: m_org_array{list}, m_array{list} {}

auto reset() { m_array = m_org_array; }


auto bubble_sort() {
for (unsigned int i{0}; i < m_array.size() - 1; ++i) {
for (unsigned int j{0}; j < m_array.size() - 1; ++j) {
if (m_array[j] > m_array[j + 1]) {
std::swap(m_array[j], m_array[j + 1]);
}
}
}
}

auto print() {
std::cout << "Array: ";
for (auto ele : m_array) {
std::cout << ele << ' ';
}
std::cout << '\n';
}
};
auto run_demo() {
SimpleSorts<int> ssorts{6, 4, 7, 2, 1};
std::cout << "Before sorting:\n";
ssorts.print();
ssorts.bubble_sort();
std::cout << "After sorting using Bubble Sort:\n";
ssorts.print();
}
auto main() -> int {
print_author_info();
run_demo();
return 0;
}

Output:

Turbo C++:

4
Sushant Wagh Div-A Roll No. 60

C++11:

B) Insertion Sort.

Theory:

What is Insertion sort and how does it work ?


● Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based
algorithm in which each pair of adjacent elements is compared and the elements are
swapped if they are not in order. This algorithm is not suitable for large data sets as
its average and worst case complexity are of Ο(n2) where n is the number of items.
● Time complexities based on different scenarios:
○ Worst Case: O(n^2)
○ Average Case: 𝛉(n^2)
○ Best Case: 𝛀(n)
Code:

Turbo C++:

#include <conio.h>
#include <iostream.h>
#define ARRAY_SIZE 5

class SimpleSorts {
int arr[ARRAY_SIZE];

static void swap(int &a, int &b);


void shift_elements_till(int start, int end);

public:
SimpleSorts();
void insertion_sort();

5
Sushant Wagh Div-A Roll No. 60

void print();
};

SimpleSorts::SimpleSorts() {
int temp_arr[] = {1, 6, 5, 2, 4};
for (int i = 0; i < ARRAY_SIZE; ++i) {
arr[i] = temp_arr[i];
}
}

void SimpleSorts::swap(int &a, int &b) {


int temp = a;
a = b;
b = temp;
}
void SimpleSorts::shift_elements_till(int start, int end) {
for (int i = end; i > start; --i) {
arr[i] = arr[i - 1];
}
}

void SimpleSorts::insertion_sort() {
for (int i = 0; i < ARRAY_SIZE - 1; ++i) {
for (int j = i + 1; j < ARRAY_SIZE; ++j) {
if (arr[j] < arr[i]) {
const int element_to_insert = arr[j];
shift_elements_till(i, j);
arr[i] = element_to_insert;
}
}
}
}

void SimpleSorts::print() {
cout << "Array: ";
for (int i = 0; i < ARRAY_SIZE; ++i) {
cout << arr[i] << ' ';
}
cout << '\n';
}
void runInsertionSortDemo() {
SimpleSorts *simple_sorts = new SimpleSorts();
cout << "Before sorting:-\n";
simple_sorts->print();
simple_sorts->insertion_sort();
cout << "After sorting using Insertion Sort:-\n";
simple_sorts->print();
delete simple_sorts;
}

6
Sushant Wagh Div-A Roll No. 60

int main() {
clrscr();
print_author_info();
runInsertionSortDemo();
getch();
return 0;
}

C++14:
#include <algorithm>
#include <initializer_list>
#include <iostream>
#include <vector>

template <typename T> class SimpleSorts {

const std::vector<T> m_org_array{};


std::vector<T> m_array{};

auto shift_elements_till(std::size_t start, std::size_t end) {


for (std::size_t i{end}; i > start; --i) {
m_array[i] = m_array[i - 1];
}
}

public:
SimpleSorts(std::initializer_list<T> list)
: m_org_array{list}, m_array{list} {}

auto reset() { m_array = m_org_array; }


auto insertion_sort() {
for (unsigned int i{0}; i < m_array.size() - 1; ++i) {
for (unsigned int j{i + 1}; j < m_array.size(); ++j) {
if (m_array[j] < m_array[i]) {
const int element_to_insert = m_array[j];
shift_elements_till(i, j);
m_array[i] = element_to_insert;
}
}
}
}

auto print() {
std::cout << "Array: ";
for (auto ele : m_array) {
std::cout << ele << ' ';
}

7
Sushant Wagh Div-A Roll No. 60

std::cout << '\n';


}
};
auto run_demo() {
SimpleSorts<int> ssorts{6, 4, 7, 2, 1};
std::cout << "Before sorting:\n";
ssorts.print();
ssorts.insertion_sort();
std::cout << "After sorting using Insertion Sort:\n";
ssorts.print();
ssorts.reset();
}
auto main() -> int {
print_author_info();
run_demo();
return 0;
}

Output:

Turbo C++:

C++11:

Conclusion:

● Sorting algorithms are a method of organizing data in a certain order and can be
used to organize messy data to be easier to use. Therefore, developing a strong
understanding of sorting algorithms and how they work is a crucial fundamental of
computer science.

8
Sushant Wagh Div-A Roll No. 60

● Sorting algorithms can help to sort database information, customer data, and
financial reports. It’s important to understand how sorting algorithms are beneficial to
your business and the right ones to use when performing certain tasks that may
require sorting larger portions of data.

Sorting Algorithm Advantages Disadvantages

Bubble Sort 1. Very simple. 1. Not very performant.


2. Easy to implement. 2. Not suitable for
arrays with a large
number of elements

Insertion Sort 1. Easy to understand. 1. Performance is poor


2. Easy to implement. compared to other
3. Slightly more sorting algorithms
performant than like merge sort.
Bubble Sort. 2. Not suitable for
arrays with a large
number of elements.

You might also like