Vous êtes sur la page 1sur 29

PRACTICAL FILE – DATA STRUCTURES

UNIVERSITY INSTITUTE OF ENGINEERING AND TECHNOLOGY,


KURUKSHETRA UNIVERSITY,
KURUKSHETRA
(SESSION: 2022-2026)

SUBMITTED TO: SUBMITTED BY:


Mr. VIJAY PREETI
252202060
CSE-A 3RD Sem
PROGRAM 1

Q1. Write a program for Binary search methods.


Source code:
#include<iostream>
using namespace std;
int binarySearch( int array[], int x, int low, int high){
while(low<=high){
int mid= low + (high-low)/2;
if( array[mid]==x)
return mid;
if (array[mid]<x)
low= mid+1;
else
high=mid-1;
}
return -1;
}
int main(void){
int array[7];
cout<<"Enter the elements of a sorted array:";
for(int i=0;i<7;i++){
cin>>array[i];
}
int x;
cout<<endl<<"Enter the number to be searched:";
cin>>x;
int n = sizeof(array)/sizeof(array[0]);
int result= binarySearch(array,x,0,n-1);
if(result == -1)
cout<<endl<<"Not found";
else
cout<<"Element is found at index"<<result;
}
Output:
PROGRAM 2

Q2. Write a program for insertion sort, selection sort, and bubble sort.
Source code:
Insertion Sort-
#include<iostream>
using namespace std;
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;
while(key<array[j] && j>=0)
{
array[j+1] = array[j];
--j;
}
array[j+1] = key;
}
}
int main(){
int data[]= {30,20,40,70,10,60,50};
int size = sizeof(data)/sizeof(data[0]);
insertionSort(data,size);
cout<<"Sorted array in ascending order (by Insertion Sort):\n";
printArray(data,size);
}
Output:
Source Code:
Selection sort-
#include<iostream>
using namespace std;
void swap(int *a, int *b){
int temp= *a;
*a = *b;
*b = temp;
}

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++){
if (array[i]<array[min_idx])
min_idx = i;
}
swap(&array[min_idx], &array[step]);
}
}
int main()
{
int data[] = {89,23,56,14,69,47};
int size= sizeof(data)/sizeof(data[0]);
selectionSort(data,size);
cout<<"Sorted array in Ascending Order (by Selection Sort):\n";
printArray(data,size);
}
Output:
Source Code:
Bubble sort-
#include<iostream>
using namespace std;
void bubbleSort(int array[], int size)
{
for(int step = 0; step<size-1; step++)
{
for(int i=0; i<size-step-1; i++){
if (array[i]>array[i+1])
{
int temp = array[i];
array[i]= array[i+1];
array[i+1] = temp;
}
}
}
}

void printArray(int array[], int size)


{
for(int i=0; i<size; i++){
cout<<" "<<array[i];
}
cout<<"\n";
}
int main()
{
int data[] = {-15,17,91,3,82,-27};
int size = sizeof(data)/sizeof(data[0]);

bubbleSort(data,size);
cout<<"Sorted Array in Ascending Order(bu Bubble Sort):\n";
printArray(data,size);
}
Output:
PROGRAM 3

Q3 Write a program to implement Stack and its operation.

Source Code:
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack<int> stack;
stack.push(4);
stack.push(9);
stack.push(18);
stack.push(12);
stack.push(13);
stack.push(30);
stack.push(1);
stack.pop();
stack.pop();

while(!stack.empty()){
cout<<stack.top()<<" ";
stack.pop();
}
}
Output:
PROGRAM 4

Q4. Write a program for quick sort.


Source Code:
#include<iostream>
using namespace std;
void swap(int *a, int *b)
{ int t = *a;
*a = *b;
*b = t; }
void printArray(int array[], int size)
{ int i;
for(i=0;i<size;i++)
cout<<array[i]<<" ";
cout<<endl; }
int partition(int array[], int low, int high)
{ int pivot = array[high];
int i = (low-1);
for(int j=low; j<high; j++)
{ if(array[j]<=pivot)
{ i++;
swap(&array[i], &array[j]); }
}
swap(&array[i+1], &array[high]);
return(i+1); }
void quickSort(int array[],int low, int high)
{ if(low<high)
{ int pi = partition(array,low,high);
quickSort(array, low, pi-1);
quickSort(array, pi+1, high); }
}
int main()
{
int data[] = { 15,18,5,29,6,98,49};
int n = sizeof(data)/sizeof(data[0]);
cout<<"Unsorted Array: \n";
printArray(data, n);
quickSort(data, 0, n-1);
cout<<"Sorted array in ascending order: \n";
printArray(data, n);
}
Output:
PROGRAM 5

Q5. Write a program for merge sort.


Source Code:
#include<iostream>
using namespace std;
void merge(int arr[],int p,int q, int r)
{
int n1= q-p+1;
int n2= r-q;
int L[n1],M[n2];

for(int i=0;i<n1;i++)
L[i] = arr[p+i];
for(int j=0;j<n2;j++)
M[j] = arr[q+1+j];

int i,j,k;
i = 0;
j = 0;
k = p;

while(i<n1 && j<n2)


{
if(L[i] <= M[j]){
arr[k] = L[i];
i++;
}
else{
arr[k] = M[j];
j++;
}
k++;
}
while(i<n1)
{
arr[k]= L[i];
i++;
k++;
}
while(j<n2)
{
arr[k]=M[j];
j++;
k++;
}
}

void mergeSort(int arr[], int l, int r)


{
if(l<r)
{
int m = l+ (r-l)/2;
mergeSort(arr,l,m);
mergeSort(arr,m+1,r);
merge(arr,l,m,r);
}
}
void printArray(int arr[], int size)
{
for(int i=0; i<size; i++)
cout<<arr[i]<<" ";
cout<<endl;
}
int main()
{
int arr[] = { 1000,466,288,256,19,678,987};
int size = sizeof(arr)/sizeof(arr[0]);
mergeSort(arr,0,size-1);
cout<<"Sorted array:\n";
printArray(arr,size);
return 0;
}
Output:
PROGRAM 6

Q6. Write a program to understand Queue and its operation.


Source Code:
#include<iostream>
#include<queue>
using namespace std;
void display_queue(queue<string>q)
{
while(!q.empty()){
cout<<q.front()<<",";
q.pop();
}
cout<<endl;
}
int main() {
queue<string>birds;
birds.push("Sparrow");
birds.push("Cuckoo");
birds.push("Humming bird");
birds.push("Woodpecker");
cout<<"Initial Queue: ";
display_queue(birds);
birds.pop();
birds.pop();
cout<<"Final Queue: ";
display_queue(birds);
return 0;
}
Output:
PROGRAM 7

Q7. Write a program to implement circular queue and its operation.


Source Code:
#include<iostream>
using namespace std;
class Queue
{
int rear,front;
int size;
int *arr;
public:
Queue(int s)
{
front = rear = -1;
size = s;
arr = new int[s];
}
void enQueue(int value);
int deQueue();
void displayQueue();
};
void Queue::enQueue(int value)
{
if((front == 0 && rear == size-1)|| (rear == (front-1)%(size-1)))
{
cout<<"\nQueue is Full";
}
else if(front == -1)
{
front = rear = 0;
arr[rear] = value;
}
else if(rear == size-1 && front != 0)
{
rear = 0;
arr[rear] = value;
}
else
{
rear++;
arr[rear] = value;
}
}
int Queue::deQueue()
{
if(front == -1)
{
cout<<"\nQueue is Empty";
return INT_MIN;
}
int data = arr[front];
arr[front] = -1;
if(front == rear)
{
front = -1;
rear = -1;
}
else if(front == size-1)
front = 0;
else
front++;
return data;
}

void Queue::displayQueue()
{
if(front == -1)
{
cout<<"\nQueue is Empty";
}
cout<<"\nElements in Circular Queue are:";
if(rear>=front)
{
for(int i=front; i<=rear; i++)
cout<<arr[i]<<" ";
}
else
{
for(int i=front;i<size;i++)
cout<<arr[i]<<" ";
for(int i=0; i<=rear; i++)
cout<<arr[i]<<" ";
}
}
int main()
{
Queue q(4);
q.enQueue(18);
q.enQueue(22);
q.enQueue(26);
q.enQueue(40);
q.displayQueue();
cout<<"\nDeleted value = "<<q.deQueue();
q.displayQueue();
q.enQueue(-44);
q.displayQueue();
q.enQueue(48);
return 0;
}
Output:

Vous aimerez peut-être aussi

  • Ds Assignment
    Ds Assignment
    Document11 pages
    Ds Assignment
    harry
    Pas encore d'évaluation
  • C++ Les Equations
    C++ Les Equations
    Document29 pages
    C++ Les Equations
    Âs Sîa
    Pas encore d'évaluation
  • Hình
    Hình
    Document14 pages
    Hình
    Tuấn Linh Đoàn
    Pas encore d'évaluation
  • Compte Rendu C++
    Compte Rendu C++
    Document18 pages
    Compte Rendu C++
    essadam96
    Pas encore d'évaluation
  • A
    A
    Document13 pages
    A
    hamzaberriche332
    Pas encore d'évaluation
  • Untitled
    Untitled
    Document32 pages
    Untitled
    Ilyass Amrouss
    Pas encore d'évaluation
  • Bellman
    Bellman
    Document4 pages
    Bellman
    Amidou Bagayogo
    Pas encore d'évaluation
  • TD7 Fonctions Tab Corr
    TD7 Fonctions Tab Corr
    Document5 pages
    TD7 Fonctions Tab Corr
    wissal midani
    Pas encore d'évaluation
  • Corrigé - TP02 Java
    Corrigé - TP02 Java
    Document4 pages
    Corrigé - TP02 Java
    Laila Moufakker
    Pas encore d'évaluation
  • Série 3 Programmation C
    Série 3 Programmation C
    Document20 pages
    Série 3 Programmation C
    Haythem Jbely
    Pas encore d'évaluation
  • Devoir 5 Langage C++-1
    Devoir 5 Langage C++-1
    Document10 pages
    Devoir 5 Langage C++-1
    Id Soukain
    Pas encore d'évaluation
  • Tds INFO2 NewVersion
    Tds INFO2 NewVersion
    Document16 pages
    Tds INFO2 NewVersion
    youssefachaaou10
    Pas encore d'évaluation
  • Objectif Du Tpe
    Objectif Du Tpe
    Document7 pages
    Objectif Du Tpe
    g3161532
    Pas encore d'évaluation
  • Tp2
    Tp2
    Document6 pages
    Tp2
    moetazbenhassen2003
    Pas encore d'évaluation
  • Ins 6068
    Ins 6068
    Document45 pages
    Ins 6068
    Shivam Patel
    Pas encore d'évaluation
  • TP3 POO JAVA Corrige
    TP3 POO JAVA Corrige
    Document10 pages
    TP3 POO JAVA Corrige
    Othman Ait Chaib
    Pas encore d'évaluation
  • Exo01 Corrigé
    Exo01 Corrigé
    Document6 pages
    Exo01 Corrigé
    Neda Rwn
    Pas encore d'évaluation
  • TP Oop2
    TP Oop2
    Document11 pages
    TP Oop2
    David Munyaga
    Pas encore d'évaluation
  • TD 04 Correction
    TD 04 Correction
    Document2 pages
    TD 04 Correction
    BABG
    Pas encore d'évaluation
  • Corrigé - TP02 Java 2023
    Corrigé - TP02 Java 2023
    Document9 pages
    Corrigé - TP02 Java 2023
    Laila Moufakker
    Pas encore d'évaluation
  • Serie Corrigu00e9e Pointeurs en C PDF
    Serie Corrigu00e9e Pointeurs en C PDF
    Document7 pages
    Serie Corrigu00e9e Pointeurs en C PDF
    Skander Tmar
    Pas encore d'évaluation
  • CorrectionTD5 C
    CorrectionTD5 C
    Document7 pages
    CorrectionTD5 C
    Lahmar Zakariae
    Pas encore d'évaluation
  • Exemple Code C
    Exemple Code C
    Document9 pages
    Exemple Code C
    Fahasoavana
    Pas encore d'évaluation
  • 19bce233 Ai Practical 2
    19bce233 Ai Practical 2
    Document6 pages
    19bce233 Ai Practical 2
    Gaurav Sakariya
    Pas encore d'évaluation
  • TP1 (1)
    TP1 (1)
    Document6 pages
    TP1 (1)
    moetazbenhassen2003
    Pas encore d'évaluation
  • Dequeue Assignment
    Dequeue Assignment
    Document23 pages
    Dequeue Assignment
    ASIF ALI KHAN
    Pas encore d'évaluation
  • Corrections TP C
    Corrections TP C
    Document30 pages
    Corrections TP C
    Ibrahim Benali
    Pas encore d'évaluation
  • TP 6-1
    TP 6-1
    Document15 pages
    TP 6-1
    houda hrouch
    Pas encore d'évaluation
  • Les Fonctions en C
    Les Fonctions en C
    Document15 pages
    Les Fonctions en C
    Ali Aourdou
    Pas encore d'évaluation
  • Exam Corrig
    Exam Corrig
    Document34 pages
    Exam Corrig
    Khadija Doja
    Pas encore d'évaluation
  • Exercice C++ GASA
    Exercice C++ GASA
    Document61 pages
    Exercice C++ GASA
    Akinlola Yawolo
    Pas encore d'évaluation
  • Exercice 3
    Exercice 3
    Document12 pages
    Exercice 3
    hajiraabdourazak
    Pas encore d'évaluation
  • Le Titre Du TP
    Le Titre Du TP
    Document34 pages
    Le Titre Du TP
    mohamed lachhab
    Pas encore d'évaluation
  • Devoir N°1 (Corrigé)
    Devoir N°1 (Corrigé)
    Document2 pages
    Devoir N°1 (Corrigé)
    Abdo Smaha
    Pas encore d'évaluation
  • corrigéTD2
    corrigéTD2
    Document4 pages
    corrigéTD2
    Momed
    Pas encore d'évaluation
  • Java
    Java
    Document6 pages
    Java
    Yassin Obie
    Pas encore d'évaluation
  • TP 4 Correction
    TP 4 Correction
    Document15 pages
    TP 4 Correction
    rebaiahmed244
    Pas encore d'évaluation
  • CC Info2021 PDF
    CC Info2021 PDF
    Document5 pages
    CC Info2021 PDF
    Nicolas
    Pas encore d'évaluation
  • Uas Sistem Operasi
    Uas Sistem Operasi
    Document4 pages
    Uas Sistem Operasi
    Aaf Andesum
    Pas encore d'évaluation
  • Correction Du TP 1 Atelier Prog II
    Correction Du TP 1 Atelier Prog II
    Document9 pages
    Correction Du TP 1 Atelier Prog II
    Safa Ayadi
    Pas encore d'évaluation
  • Matrice
    Matrice
    Document1 page
    Matrice
    Saliou Thiam
    Pas encore d'évaluation
  • Clever Guy
    Clever Guy
    Document11 pages
    Clever Guy
    abdelmoumenkrt
    Pas encore d'évaluation
  • Data Structure
    Data Structure
    Document16 pages
    Data Structure
    Il Ias
    Pas encore d'évaluation
  • Correction Examen 2019 - 2020 Java New Version
    Correction Examen 2019 - 2020 Java New Version
    Document5 pages
    Correction Examen 2019 - 2020 Java New Version
    hanona
    Pas encore d'évaluation
  • Devoir 1
    Devoir 1
    Document5 pages
    Devoir 1
    asmae el haiad
    Pas encore d'évaluation
  • TP1 TP2 TP3 TP4 Wahbi Yassine PDF
    TP1 TP2 TP3 TP4 Wahbi Yassine PDF
    Document34 pages
    TP1 TP2 TP3 TP4 Wahbi Yassine PDF
    wahbi yassine
    Pas encore d'évaluation
  • TP 2 PDF
    TP 2 PDF
    Document6 pages
    TP 2 PDF
    Mi Lou
    Pas encore d'évaluation
  • Exercice Entrepot
    Exercice Entrepot
    Document11 pages
    Exercice Entrepot
    Aurelien
    Pas encore d'évaluation
  • TP1 Analyse Numerique
    TP1 Analyse Numerique
    Document4 pages
    TP1 Analyse Numerique
    Youssef_Consty_4665
    Pas encore d'évaluation
  • Code
    Code
    Document10 pages
    Code
    Phong Nguyen hai
    Pas encore d'évaluation
  • MecDesSysSolIndef Cours
    MecDesSysSolIndef Cours
    Document5 pages
    MecDesSysSolIndef Cours
    TàHa Samih
    100% (1)
  • Exercice 1
    Exercice 1
    Document5 pages
    Exercice 1
    Hanen ghazouani
    Pas encore d'évaluation
  • DS Programs
    DS Programs
    Document34 pages
    DS Programs
    navin_killer
    Pas encore d'évaluation
  • TP 06 07
    TP 06 07
    Document9 pages
    TP 06 07
    olfaFakh
    Pas encore d'évaluation
  • Corrections TP1 C
    Corrections TP1 C
    Document6 pages
    Corrections TP1 C
    emilien72200
    Pas encore d'évaluation
  • Process Us
    Process Us
    Document4 pages
    Process Us
    Franc Zogning
    Pas encore d'évaluation
  • chiffrment complet
    chiffrment complet
    Document10 pages
    chiffrment complet
    Racha
    Pas encore d'évaluation
  • TPJAVAEMPIRE
    TPJAVAEMPIRE
    Document7 pages
    TPJAVAEMPIRE
    Nehemie Diavangama
    Pas encore d'évaluation
  • Sondage: Compilation et corrélation
    Sondage: Compilation et corrélation
    D'Everand
    Sondage: Compilation et corrélation
    Pas encore d'évaluation