0% found this document useful (0 votes)
65 views25 pages

Q.5 Write A Program of Bubble Sort

The document contains C/C++ code for implementing various sorting algorithms like bubble sort, selection sort, merge sort, insertion sort and quick sort. It also contains code for implementing stack and queue data structures using arrays. The last program shows code to find the largest and smallest element in an array.

Uploaded by

Yogesh Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views25 pages

Q.5 Write A Program of Bubble Sort

The document contains C/C++ code for implementing various sorting algorithms like bubble sort, selection sort, merge sort, insertion sort and quick sort. It also contains code for implementing stack and queue data structures using arrays. The last program shows code to find the largest and smallest element in an array.

Uploaded by

Yogesh Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Q.5 Write a program of Bubble sort.

#include <stdio.h>

void swap(int *xp, int *yp)

int temp = *xp;

*xp = *yp;

*yp = temp;

void bubbleSort(int arr[], int n)

int i, j;

bool swapped;

for (i = 0; i < n-1; i++)

swapped = false;

for (j = 0; j < n-i-1; j++)

if (arr[j] > arr[j+1])

swap(&arr[j], &arr[j+1]);

swapped = true;

if (swapped == false)

break;
}

void printArray(int arr[], int size)

int i;

for (i=0; i < size; i++)

printf("%d ", arr[i]);

printf("n");

int main()

int arr[] = {64, 34, 25, 12, 22, 11, 90};

int n = sizeof(arr)/sizeof(arr[0]);

bubbleSort(arr, n);

printf("Sorted array: \n");

printArray(arr, n);

return 0;

}
Q.6 Write a program of Selection sort.
#include <iostream>

using namespace std;

void swap(int *xp, int *yp)

int temp = *xp;

*xp = *yp;

*yp = temp;

void selectionSort(int arr[], int n)

int i, j, min_idx;

for (i = 0; i < n-1; i++)

min_idx = i;

for (j = i+1; j < n; j++)

if (arr[j] < arr[min_idx])

min_idx = j;

swap(&arr[min_idx], &arr[i]);

void printArray(int arr[], int size)

{
int i;

for (i=0; i < size; i++)

cout << arr[i] << " ";

cout << endl;

int main()

int arr[] = {64, 25, 12, 22, 11};

int n = sizeof(arr)/sizeof(arr[0]);

selectionSort(arr, n);

cout << "Sorted array: \n";

printArray(arr, n);

return 0;

}
Q.7 Write a program of merge sort.
#include<stdlib.h>

#include<stdio.h>

void merge(int arr[], int l, int m, int r)

int i, j, k;

int n1 = m - l + 1;

int n2 = r - m;

int L[n1], R[n2];

for (i = 0; i < n1; i++)

L[i] = arr[l + i];

for (j = 0; j < n2; j++)

R[j] = arr[m + 1+ j];

i = 0;

j = 0;

k = l;

while (i < n1 && j < n2)

if (L[i] <= R[j])

arr[k] = L[i];

i++;

else

{
arr[k] = R[j];

j++;

k++;

while (i < n1)

arr[k] = L[i];

i++;

k++;

while (j < n2)

arr[k] = R[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 A[], int size)

int i;

for (i=0; i < size; i++)

printf("%d ", A[i]);

printf("\n");

int main()

int arr[] = {12, 11, 13, 5, 6, 7};

int arr_size = sizeof(arr)/sizeof(arr[0]);

printf("Given array is \n");

printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

printf("\nSorted array is \n");

printArray(arr, arr_size);

return 0;

}
Q.8 Write a program of Insertion sort.
#include <iostream>

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;

cout<<"soeted array is ; ";

for (i = 0; i < n; i++)

cout << arr[i] << " ";

cout << endl;


}

int main()

int arr[] = { 45,34,56,23,11};

int n = sizeof(arr) / sizeof(arr[0]);

insertionSort(arr, n);

printArray(arr, n);

return 0;

}
Q.9 Write a program of Quick sort.
#include<iostream>

using namespace std;

int main ()

int arr[10], n, i, max, min;

cout << "Enter the size of the array : ";

cin >> n;

cout << "Enter the elements of the array : ";

for (i = 0; i < n; i++){ cin >> arr[i]; max = arr[0];

for (i = 0; i < n; i++){

if (max < arr[i])

max = arr[i];

min = arr[0];

for (i = 0; i < n; i++)

if (min > arr[i])

min = arr[i];

cout << "Largest element : " << max;

cout << "Smallest element : " << min;

return 0;

}
Q.10 Write a program of Stack implementation using array.
#include<iostream>

#include<stdlib.h>

#include<conio.h>

using namespace std;

int push(int [], int &, int);

void display(int [], int);

const int SIZE = 50;

int main()

int stack[SIZE], item, top=-1, res;

char ch='y';

while(ch=='y' || ch=='Y')

cout<<"Enter item for insertion: ";

cin>>item;

res = push(stack, top, item);

if(res == -1)

cout<<"Overflow..!!..Aborting..Press a key to exit..\n";

getch();

exit(1);

cout<<"Element inserted successfully..!!\n";

cout<<"\nThe Stack now is:\n";


display(stack, top);

cout<<"\nWant to enter more ? (y/n).. ";

cin>>ch;

return -3;

int push(int stack[], int &top, int elem)

if(top == SIZE-1)

{ return -1; }

else

top++;

stack[top] = elem;

return 0;

void display(int stack[], int top)

cout<<stack[top]<<" <-- "<<"\n";

for(int i=top-1; i>=0; i--)

cout<<stack[i]<<"\n";

}
Q.10 Write a program of Queue implementation using array.
#include <iostream>

#include <stdio.h>

using namespace std;

struct Queue {

int front, rear, capacity;

int* queue;

Queue(int c)

front = rear = 0;

capacity = c;

queue = new int;

~Queue() { delete[] queue; }

void queueEnqueue(int data)

if (capacity == rear) {

printf("\nQueue is full\n");

return;

else {

queue[rear] = data;

rear++;

return;
}

void queueDequeue()

if (front == rear) {

printf("\nQueue is empty\n");

return;

else {

for (int i = 0; i < rear - 1; i++) {

queue[i] = queue[i + 1];

rear--;

return;

void queueDisplay()

int i;

if (front == rear) {

printf("\nQueue is Empty\n");

return;

for (i = front; i < rear; i++) {

printf(" %d <-- ", queue[i]);

}
return;

void queueFront()

if (front == rear) {

printf("\nQueue is Empty\n");

return;

printf("\nFront Element is: %d", queue[front]);

return;

};

int main(void)

Queue q(4);

q.queueDisplay();

q.queueEnqueue(54);

q.queueEnqueue(11);

q.queueEnqueue(82);

q.queueEnqueue(94);

q.queueDisplay();

q.queueEnqueue(95);

q.queueDisplay();

q.queueDequeue();

q.queueDequeue();
printf("\n\nafter two node deletion\n\n");

q.queueDisplay();

q.queueFront();

return 0;

}
Q.10 Write a programm to find ith smallest and largest no..
#include<iostream>

using namespace std;

int main ()

int arr[10], n, i, max, min;

cout << "Enter the size of the array : ";

cin >> n;

cout << "Enter the elements of the array : ";

for (i = 0; i < n; i++)

cin >> arr[i];

max = arr[0];

for (i = 0; i < n; i++)

if (max < arr[i])

max = arr[i];

min = arr[0];

for (i = 0; i < n; i++)

{ if (min > arr[i]) min = arr[i]; }

cout << "Largest element : " << max;

cout << "Smallest element : " << min;

return 0;

You might also like