1DSL Practical
1DSL Practical
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
Code:
Turbo C++:
#include <conio.h>
#include <iostream.h>
#define ARRAY_SIZE 5
class SimpleSorts {
int arr[ARRAY_SIZE];
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::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>
public:
SimpleSorts(std::initializer_list<T> list)
: m_org_array{list}, m_array{list} {}
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:
Turbo C++:
#include <conio.h>
#include <iostream.h>
#define ARRAY_SIZE 5
class SimpleSorts {
int arr[ARRAY_SIZE];
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::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>
public:
SimpleSorts(std::initializer_list<T> list)
: m_org_array{list}, m_array{list} {}
auto print() {
std::cout << "Array: ";
for (auto ele : m_array) {
std::cout << ele << ' ';
}
7
Sushant Wagh Div-A Roll No. 60
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.