0% found this document useful (0 votes)
4 views11 pages

Exp 03 DS Jaid

The document contains four sorting algorithms: Bubble Sort, Insertion Sort, Selection Sort, and Merge Sort, each implemented in C++. Each algorithm includes a code snippet demonstrating its functionality and an output section showing the sorted array. The document is structured with clear headings for each sorting method and includes example arrays for sorting.

Uploaded by

atharvathorave4
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)
4 views11 pages

Exp 03 DS Jaid

The document contains four sorting algorithms: Bubble Sort, Insertion Sort, Selection Sort, and Merge Sort, each implemented in C++. Each algorithm includes a code snippet demonstrating its functionality and an output section showing the sorted array. The document is structured with clear headings for each sorting method and includes example arrays for sorting.

Uploaded by

atharvathorave4
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/ 11

EXPERIMENT NO.

:- 03

NAME:- SHAIKH MAHAMMADJAID YASIN


STD.:- BCA II
DIV:- C
ROLL NO. :- 53
DATE :-

TITLE: Write To Programs to perform Bubble Sort, Insertion Sort, Selection Sort,
Merge Sort.

1) Bubble Sort –

#include<iostream.h>

#include<conio.h>

void print(int a[],int n)

int i;

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

cout<<a[i]<<" ";

void bubble(int a[],int n)

int i,j,temp;

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

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

if(a[j]<a[i])

temp=a[i];

a[i]=a[j];

a[j]=temp;

int main()

int a[5]={53,10,1,25,93};

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

int i,j,temp;

cout<<"Before sorting array elements are - \n";

for(int i=0;i<5;i++)

cout<<a[i]<<" ";

bubble(a,n);

cout<<"\nAfter sorting array elements are - \n";

print(a,n);
return 0;

Output :
2) Insertion Sort –

#include<iostream.h>
#include<conio.h>

void insertionSort(int arr[], int n)

int i,j;

for(i=1;i<n;i++)
{
int var = arr[i];

for(j=i-1;j>=0;j--)

if(arr[j]>var)

{ arr[j+1]=arr[j]; }

else

break;

arr[j+1]=var;

cout<<"Array Sorting Array\n”;

for(i=0;i<=n-1;i++)
{
cout<<arr[i]<<" ";
}
}

void main()

int array[]={15,35,20,17,31,40}; int


size=6; insertionSort(array,size);

getch();

Output :
3) Selection Sort –

#include<iostream.h>
#include<conio.h> void
swap(int*xp,int *yp)

{
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; }
void main()
{
clrscr();
int arr[] ={53,10,1,25,93};
int n=sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);

cout << "Sorted array: ";


printArray(arr,n);

getch();

Output :
4) Merge sort –

#include<iostream.h>
#include<conio.h>

void printArray(int *arr, int length) {

for (int i = 0; i < length; i++) {

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

cout << endl;

void merge(int *arr, int low, int mid,


int high)

int lengthLeft = mid - low + 1;

int lengthRight = high - mid;

int arrLeft[lengthLeft];

int arrRight[lengthRight];

for (int i = 0; i < lengthLeft; i++) {

arrLeft[i] = arr[low + i];

for (int j = 0; j < lengthRight; j++) {

arrRight[j] = arr[mid + 1 + j];

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

while (i < lengthLeft && j <


lengthRight)
{

if (arrLeft[i] <= arrRight[j])

arr[k] = arrLeft[i];

i++;

else

arr[k] = arrRight[j];

j++;

k++;

while (i < lengthLeft)

arr[k] = arrLeft[i];

i++;

k++;

while (j < lengthRight)

arr[k] = arrRight[j];

j++;

k++;

}
}

void mergeSort(int *arr, int low, int


high) {

if (low < high)

int mid = (low + high) / 2;

mergeSort(arr, low, mid);

mergeSort(arr, mid + 1, high);

merge(arr, low, mid, high);

int main()

int arr[] = {53,10,1,25,93,3,5}

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

cout << "Array before calling the


mergeSort(): ";

printArray(arr, length);

mergeSort(arr, 0, length - 1);

cout << "Array after calling the


mergeSort(): ";

printArray(arr, length);

return 0;

}
Output :

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