Dsa Practicals

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Program no.

8
Write a program to implement bubble sort
#include <iostream>
using namespace std;
void bubbleSort(int arr[], int n){
int i, j;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
}
void printArray(int arr[], int size){
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main(){
int arr[] = {10,15,65,2,3};
int N = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, N);
cout << "Sorted array: \n";
printArray(arr, N);
return 0;
}

AKSHAT DIXIT 2121231


Output:

AKSHAT DIXIT 2121231


Program no. 9
Wap to implement insertion and deletion in a queue using
array
#include <bits/stdc++.h>
using namespace std;
int arr[100], val;
int n = 100;
int front = -1, rear = -1;
void insertion()
{
if (rear == n - 1){
cout << "Queue overflow" << endl;
}
else{
front = 0;
cout << "Enter the value to be inserted : ";
cin >> val;
rear++;
arr[rear] = val;
}
}
void deletion(){
if (front == -1 || rear == -1){
cout << "Queue underflow";
}
else{
cout << "Element deleted from queue : " << arr[front] << endl;
front++;
}
}
int main(){
int ch;
cout << "1 for insertion" << endl;
cout << "2 for deletion" << endl;
cout << "Other number to exit" << endl;
do{
cout << "Enter your choice : ";
cin >> ch;
switch (ch)
AKSHAT DIXIT 2121231
{
case 1:
insertion();
break;
case 2:
deletion();
break;
default:
cout << "Invalid choice" << endl;
}
} while (ch <= 2);
return 0;
}

AKSHAT DIXIT 2121231


Output:

AKSHAT DIXIT 2121231


Program No.10
Wap to implement insertion sort
#include <bits/stdc++.h>
using namespace std;
void insertionSort(int arr[], int n){
int i, key, j;
for (i = 1; i < n; i++){
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key){
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n){
int i;
for (i = 0; i < n; i++) cout << arr[i] << " ";
cout << endl;
}
int main(){
int arr[] = {56,15,47,23,65,48};
int N = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, N);
printArray(arr, N);
return 0;
}

AKSHAT DIXIT 2121231


Output:

AKSHAT DIXIT 2121231


Program No. 11
Wap to implement selection sort

#include<iostream>
using namespace std;

void printArray(int A[], int n){


for (int i = 0; i < n; i++){
cout<<A[i]<<" ";
}
cout<<endl;
}

void selectionSort(int A[], int n){


int indexOfMin, temp;
for (int i = 0; i < n-1; i++){
indexOfMin = i;
for (int j = i+1; j < n; j++){
if(A[j] < A[indexOfMin]){
indexOfMin = j;
}
}
swap(A[i], A[indexOfMin]);
}
}

int main(){
int A[] = {3, 5, 2, 13, 12};
int n = 5;
cout<<"Before sort"<<endl;
printArray(A, n);
selectionSort(A, n);
cout<<"\nAfter sort"<<endl;
printArray(A, n);
return 0;
}

AKSHAT DIXIT 2121231


Output:

AKSHAT DIXIT 2121231


Program No. 12
WAP to implement Quick sort

#include<iostream>
using namespace std;

void printArray(int A[], int n){


for (int i = 0; i < n; i++){
cout<<A[i]<<" ";
}
cout<<endl;
}

int partition(int A[], int low, int high){


int pivot = A[low];
int i = low + 1;
int j = high;
int temp;

do{
while(A[i] <= pivot){
i++;
}

while(A[j] > pivot){


j--;
}

if(i<j){
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}while(i<j);

swap(A[low], A[j]);

AKSHAT DIXIT 2121231


return j;
}

void quickSort(int A[], int low, int high){


int partitionIndex;

if(low<high){
partitionIndex = partition(A, low, high);
quickSort(A, low, partitionIndex-1);
quickSort(A, partitionIndex+1, high);
}
}

int main(){
int A[] = {5, 2, 13, 12, 3, 2, 10, 25, 56, 76, 45, 5, 74};
int n = sizeof(A)/sizeof(int);
cout<<"Before sorting\n";
printArray(A, n);
quickSort(A, 0, n-1);
cout<<"\nAfter sorting\n";
printArray(A, n);
return 0;
}

AKSHAT DIXIT 2121231


Output:

AKSHAT DIXIT 2121231


AKSHAT DIXIT 2121231

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy