Bubble sort(Practical 1)
#include <iostream>
using namespace std;
// perform bubble sort
void bubbleSort(int array[], int size) {
// loop to access each array element
for (int step = 0; step < size -1; ++step) {
// loop to compare array elements
for( int i = 0; i < size - step - 1; ++i) {
// compare two adjacent elements
// change > to < to sort in descending order
if (array[i] > array[i + 1]) {
// swapping elements if elements
// are not in the intended order
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
//print array
void printArray(int array[], int size) {
for(int i = 0; i < size; ++i) {
cout << " " << array[i];
cout << "\n";
}
int main() {
int data[] = {-2, 45, 0, 11, -9};
// find array's length
int size = sizeof(data) / sizeof(data[0]);
bubbleSort(data, size);
cout << "Sorted Array in Ascending order:\n";
printArray(data, size);
}
Insertion sort(Practical 2)
#include <iostream>
using namespace std;
// Function to print an array
void printArray(int array[], int size) {
for (int i = 0; i <size; i++) {
cout << array[i] << " ";
cout << endl;
void insertionSort(int array[], int size) {
for (int step = 1; step < size; step++) {
int key = array[step];
int j = step - 1;
// Comapre key with each element on the left of it until an element smaller than
// it is found.
// For descending order, change key<array[j] to key>array[j].
while (key < array[j] && j >= 0) {
array[j + 1] = array[j];
--j;
array[j + 1] = key;
}
//Driver code
int main() {
int data[] = {9, 5, 1, 4, 3};
int size = sizeof(data) / sizeof(data[0]);
insertionSort(data, size);
cout << "Sorted array in ascending order:\n";
printArray(data, size);
}
Selection sort(Practical 3)
#include <iostream>
using namespace std;
// function to swap the position of two elements
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
// function to print an array
void printArray(int array[], int size) {
for (int i = 0; i < size; i++) {
cout << array[i] << " ";
cout << endl;
void selectionSort(int array[], int size) {
for (int step = 0; step < size - 1; step++) {
int min_idx = step;
for (int i = step + 1; i < size; i++) {
//To sort in descending order, change > to < in this line.
//Select the minimum element in each loop.
if (array[i] < array[min_idx])
min_idx = i;
}
// put min at the correct position
swap(&array[min_idx], &array[step]);
// driver code
int main() {
int data[] = {20, 12, 10, 15, 2};
int size = sizeof(data) / sizeof(data[0]);
selectionSort(data, size);
cout << "Sorted array in Ascending order:\n";
printArray(data, size);
}
Binary Search(Practical 4)
#include <iostream>
using namespace std;
int binarySearch(int arr[], int low, int high, int key) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == key) {
return mid;
}
if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1; // Element not found
}
int main() {
int arr[] = {2, 5, 8, 12, 15, 23, 38, 44, 51};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 23;
int result = binarySearch(arr, 0, n - 1, key);
if (result != -1){
printf("Element %d found at index %d\n" , key, result);
} else {
printf("Element %d not found in the array\n", key);
}
return 0;
}
// Quick sort in c++(Practical 5)
#include <iostream>
using namespace std;
//function to swap elements
void swap(int *a,int *b){
int t= *a;
*a= *b;
*b=t;
//function to print the array
void printArray(int array[], int size){
int i;
for(i=0; i<size; i++)
cout<<array[i]<<"";
cout<<endl;
//function to rearrange array(find the partition point)
int partition(int array[], int low, int high){
//select the rightmost element as pivot
int pivot=array[high];
//pointer for greater element
int i = (low-1);
//traverse each element of the array
//compare them with the pivot
for(int j = low; j<high; j++){
if (array[j] <= pivot){
//if element smaller than the pivot is found
//swap it with the greater element pointed by i
i++;
//swap element at i with element at j
swap(&array[i], &array[j]);
//swap pivot with the greater element at i
swap(&array[i+1], &array[high]);
//return the partition point
return (i+1);
void quickSort(int array[], int low, int high){
if(low<high){
//find the pivot element such that
//elements smaller than pivot are on the left of pivot
//elements greater than pivot are on right of pivot
int pi=partition(array,low,high);
//recursive call on the right of pivot
quickSort(array,pi + 1,high);
}
//Driver Code
int main(){
int data[] = {8,7,6,1,0,9,2};
int n = sizeof(data)/sizeof(data[0]);
cout<<"Unsorted Array:\n";
printArray(data, n);
//perform quicksort on data
quickSort(data, 0,n-1);
cout<<"Sorted array in ascending order:\n";
printArray(data, n);
//Factorial of a number using recursion. Factorial of
n=1*2*3...*n(Practical 6)
#include <iostream>
using namespace std;
int factorial(int);
int main(){
int n, result;
cout<<"Enter a non-negative number:";
cin>>n;
result=factorial(n);
cout<<"Factorial of "<<n<<"="<<result;
return 0;
int factorial(int n){
if(n>1){
return n*factorial(n-1);
}else{
return 1;
//Taking input of two dimensional array(Practical 7)
#include <iostream>
using namespace std;
int main(){
int numbers[2][3];
cout<<"Enter 6 numbers:"<<endl;
//Storing user input in the array
for(int i = 0;i < 2; ++i){
for(int j = 0;j < 3; ++j){
cin>>numbers[i][j];
}
}
cout<<"The numbers are:"<<endl;
//Printing array elements
for (int i = 0; i<2;++i){
for(int j=0;j<3;++j){
cout<<"numbers["<<i<<"]["<<j<<"]:"<<numbers[i][j]<<endl;
return 0;
//Add element in the stack(Practical 8)
#include <iostream>
#include <stack>
using namespace std;
int main(){
//create a stack of strings
stack<string>colors;
//push elements into the stack
[Link]("Red");
[Link]("Orange");
cout<<"Stack:";
//print elements of stack
while(![Link]()){
cout<<[Link]()<<",";
[Link]();
return 0;
//Implement algorithms to find Nth Max/Min
element in a list(Practical 9)
#include <iostream>
#include<limits.h>
using namespace std;
int setmini(int A[], int N)
int mini = INT_MAX;
for(int i = 0; i < N; i++){
if(A[i]<mini){
mini=A[i];
return mini;
}
int setmaxi(int A[], int N)
int maxi=INT_MIN;
for(int i = 0; i<N;i++){
if(A[i]>maxi){
maxi=A[i];
return maxi;
int main()
int A[]={4,9,6,5,2,3};
int N=6;
cout<<"Minimum element is:"<<setmini(A,N)<<endl;
cout<<"Maximum element is:"<<setmaxi(A,N)<<endl;