0% found this document useful (0 votes)
49 views10 pages

Arrays: Bubble Sorting

The document discusses three algorithms for sorting and merging arrays: 1) Bubble sort, which iterates through an array, comparing and swapping adjacent elements if out of order until sorted. 2) Insertion sort, which iterates through an array and inserts each element into its sorted position. 3) Merging arrays, which takes two sorted arrays and combines them into a single sorted array by comparing elements and inserting in order.

Uploaded by

Riddhim Sehgal
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)
49 views10 pages

Arrays: Bubble Sorting

The document discusses three algorithms for sorting and merging arrays: 1) Bubble sort, which iterates through an array, comparing and swapping adjacent elements if out of order until sorted. 2) Insertion sort, which iterates through an array and inserts each element into its sorted position. 3) Merging arrays, which takes two sorted arrays and combines them into a single sorted array by comparing elements and inserting in order.

Uploaded by

Riddhim Sehgal
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/ 10

ARRAYS:Bubble Sorting

#include <iostream.h>
#include <conio.h>
#include<stdio.h>
void bubble(int ar[],int s)
{
int temp;
for(int i=0;i<s-1;i++)
{
for(int j=0;j<s-1-i;j++)
{
if(ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
}
for(int a=0;a<s;a++)
{
cout<<ar[a];
}
}

void main ()

{
clrscr();
int rs[50],size;
cout<<"Enter size of the array:";
cin>>size;
for(int b=0;b<size;b++)
{
cout<<"Enter the "<<b+1<<" element of the array:\n";
cin>>rs[b];
}
bubble(rs,size);
getch();

}
ARRAYS:Insertion Sorting
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
void isort(int ar[],int s)
{
int i,j,temp;
for(i=0;i<s;i++)
{
temp=ar[i];
j=i-1;
while((temp<ar[j])&&(j>=0))
{
ar[j+1]=ar[j];
j--;
}
ar[j+1]=temp;
}
for(int a=0;a<s;a++)
{
cout<<ar[a];
}
}
void main()

{
clrscr();
int size,rs[50];
cout<<"Enter Size of the array:\n";
cin>>size;
for(int k=0;k<size;k++)
{
cout<<"Enter The "<<k+1<<" element of the array:";
cin>>rs[k];
}
isort(rs,size);

getch();
}
ARRAYS:Merging of arrays
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
void merging(int ar[],int rs[],int m,int n)
{

int k=0,i=0,j=0,ar3[50];
for(i=0,j=0;i<m&&j<n;)
{
if(ar[i]<=rs[j])
ar3[k++]=ar[i++];
else
ar3[k++]=rs[j++];
}
if(i<n)
{
while(i<n)
ar3[k++]=ar[i++];
}
if(j<m)
{
while(j<m)
ar3[k++]=rs[j++];
}
for(k=0;k<m+n;k++)
{
cout<<"The merged array is:\n"<<ar3[k]<<"\t";
}
}
void main()
{
clrscr();
int rid[50],sg[50],size1,size2;
cout<<"Enter Size of Array 1:";
cin>>size1;
cout<<"Enter Size of Array 2:";
cin>>size2;
for(int a=0;a<size1;a++)
{
cout<<"Enter the "<<a+1<<" element of array 1:\n";
cin>>rid[a];
}
cout<<"\n";
for(int b=0;b<size2;b++)
{
cout<<"Enter the "<<b+1<<"element of array 2:\n";
cin>>sg[b];
}
merging(rid,sg,size1,size2);

getch();
}

You might also like