0% found this document useful (0 votes)
59 views6 pages

Bubble Sort : Copied

The document describes three sorting algorithms: Bubble Sort, Insertion Sort, and Merge Sort. Bubble Sort compares adjacent elements and swaps them if they are in the wrong order. Insertion Sort iterates through the list, inserting each element into the sorted portion. Merge Sort divides the list, recursively sorts the halves, and then merges the sorted halves.

Uploaded by

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

Bubble Sort : Copied

The document describes three sorting algorithms: Bubble Sort, Insertion Sort, and Merge Sort. Bubble Sort compares adjacent elements and swaps them if they are in the wrong order. Insertion Sort iterates through the list, inserting each element into the sorted portion. Merge Sort divides the list, recursively sorts the halves, and then merges the sorted halves.

Uploaded by

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

Bubble Sort (copied)

#include<iostream>
using namespace std;
int main ()
{
int i, j,temp,pass=0;
int a[10] = {10,2,0,14,43,25,18,1,5,45};
cout <<"Input list ...\n";
for(i = 0; i<10; i++)
{
cout <<a[i]<<"\t";
}
cout<<endl;
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] < a[i]) \
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
pass++;
}
cout <<"Sorted Element List ...\n";
for(i = 0; i<10; i++)
{
cout <<a[i]<<"\t";
}
cout<<"\nNumber of passes taken to sort the list:"<<pass<<endl;
return 0;
}

Insertion Sort
#include"iostream"
#include"conio.h"
using namespace::std;

void insert_sort(int *arr, int s);

int main(void)
{
int n;

cout<<"\nEnter the size of array ";


cin>>n;
int *ar = new int[n];
cout<<"\nEnter the array of "<<n<<" elements ";
for(int e=0; e<n; e++)
{
cin>>ar[e];
}
insert_sort(ar,n);
cout<<"Sorted Array ";
for(int e=0; e<n; e++)
{
cout<<"\n "<<ar[e];
}

cin.ignore(); cin.get();
}
void insert_sort(int *arr, int s)
{

for(int j=1; j<s; j++)


{
int check = arr[j];
int i = j-1;
while(i>=0 && arr[i]>check)
{
arr[i+1] = arr[i];
i--;
}
arr[i+1] = check;
}
}

Merge Sort

#include"iostream"
#include"conio.h"
using namespace::std;

void merge_sort(int *Arr ,int st,int en);


void merge(int*Arr ,int st,int md,int en);
int main()
{
int n;
cout<<"\nEnter the size of array = ";
cin >> n;
int *Arr = new int[n];
cout<<"\nEnter array of size n ";
for(int j=0; j<n; j++)
{
cin>>Arr[j];
}
merge_sort(Arr,0,n);

cout<<"\nThe Sorted Array : ";


for (int k=0; k<n; k++)
{
cout<<" "<<Arr[k];
}
cin.ignore(); cin.get();
return 0;
}

void merge_sort(int *Arr,int st,int en)


{
if(st<en)
{
int md=((st+en)/2);
merge_sort(Arr,st,md);
merge_sort(Arr,md+1,en);
merge(Arr,st,md,en);
}
}

void merge(int*Arr,int st,int md,int en)


{
int n1=md-st+1;
int n2=en-md;
int *L=new (int [n1+1]);
int *R=new (int [n2+1]);
int i,j;
for(i=0;i<n1;i++)
{
L[i]=Arr[st+i-1];
}

for(j=0;j<n2;j++)
{
R[j]=Arr[md+j];
}

L[n1]=9999;
R[n2]=9999;

i=0;
j=0;

for(int k=st-1; k<en ; k++)


{
if(L[i]<=R[j])
{
Arr[k]=L[i];
i++;
}
else
{
Arr[k]=R[j];
j++;
}
}

You might also like