Algorithom Lab 5 Sawon
Algorithom Lab 5 Sawon
Algorithm:
1. Start
4. While index>0&&arr[index-1]<valu
7. End
Code:
#include<iostream>
for(int i=1;i<x;i++)
int valu=arr[i];
int index=i;
while(index>0&&arr[index-1]<valu)
arr[index]=arr[index-1];
index--;
arr[index]=valu;
}
}
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
inserts(arr,n);
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
Output:
Discussion: Then I take temporary variable named velu where the valu induction is then checked if
index is zero and value of index-one is greater than velu then put man of index-1 at index.
Problem Statement:-2
Implement Bubble Sort Algorithm .
Algorithm:
1. Start
swap
6. End
Code:
#include<iostream>
for(int i=0;i<Size-1;i++)
for(int j=0;j<Size-1;j++)
if(arry[j]>arry[j+1])
arry[j]=arry[j]+arry[j+1];
arry[j+1]=arry[j]-arry[j+1];
arry[j]=arry[j]-arry[j+1];
}
}
int main()
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
bubblesort(arr,n);
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
Output:
Discussion: In this program, we take input arry and arry size, Each index must be compared with all
other indices and index 0>1 swap this index .
Problem Statement:- 03
Suppose you have an unsorted array of n element and n can be at most 10 Now you have 5 . to
implement bubble sort algo and find the 2nd biggest element. But here is a twist. The
maximum time limit is 10 (Assume each comparison takes 1 unit time). So you need to find a
9 way to only find the 2nd biggest element within the time range. Note: You only need to find
2nd biggest element.
Algorithm:
1. Start
swap
6. End
Code:
#include<iostream>
using namespace std;
int bubblesort(int arry[],int Size){
for(int i=0;i<2;i++)
{
for(int j=0;j<Size-1;j++)
{
if(arry[j]>arry[j+1])
{
arry[j]=arry[j]+arry[j+1];
arry[j+1]=arry[j]-arry[j+1];
arry[j]=arry[j]-arry[j+1];
}
}
}
}
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
bubblesort(arr,n);
cout<<arr[n-2];
}
Output:
Discussion: If our algorithm also runs twice then we get the second highest number, So I have
pinned n-2 index by running the itaration twice.
Problem Statement:- 04
4. Maruf has got three quiz marks. quiz 1, quiz 2 and quiz 3. He keeps these numbers in secret,
but he writes down four numbers on a board in arbitrary order — their pairwise sums (three
numbers) and sum of all three numbers (one number). So, there are four numbers on a board in
random order: quiz1+quiz2, quiz2+quiz3, quiz1+quiz3 and quiz1+quiz2+quiz3
You have to guess three numbers quiz1, quiz2 and quiz3 using given numbers. Print three
guessed integers in any order (Using Sorting Algorithm)
Pay attention that some given numbers quiz1, quiz2 and quiz3 can be equal (it is also possible
that quiz1=quiz2=quiz3).
Algorithm:
1. Start
5. Print answer
6. end
Code:
#include<iostream>
for(int i=1;i<x;i++)
int valu=arr[i];
int index=i;
while(index>0&&arr[index-1]<valu)
arr[index]=arr[index-1];
index--;
arr[index]=valu;
int main()
int ar[4];
for(int i=0;i<4;i++)
cin>>ar[i];
inserts(ar,4);
cout<<ar[0]-ar[1]<<" ";
cout<<ar[0]-ar[2]<<" ";
cout<<ar[0]-ar[3]<<endl;
Output:
Discussion: In this program I have taken 4 number ,when a+b,b+c,c+a and a+b+c ,At frist we
sort Descending Order,we get index 0 valu is gretest valu among 4 valu and subtruc big valu to
other valu.