0% found this document useful (0 votes)
27 views8 pages

Algorithom Lab 5 Sawon

The document discusses 4 problems related to sorting algorithms. Problem 1 explains insertion sort with pseudocode and an implementation. Problem 2 explains bubble sort with pseudocode and code. Problem 3 discusses implementing a modified bubble sort to find the second largest element within a time limit. Problem 4 explains how to use sorting to determine 3 quiz scores given their sums. For each problem, pseudocode and/or code is provided along with short discussions.

Uploaded by

Sanjidul Hasan
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)
27 views8 pages

Algorithom Lab 5 Sawon

The document discusses 4 problems related to sorting algorithms. Problem 1 explains insertion sort with pseudocode and an implementation. Problem 2 explains bubble sort with pseudocode and code. Problem 3 discusses implementing a modified bubble sort to find the second largest element within a time limit. Problem 4 explains how to use sorting to determine 3 quiz scores given their sums. For each problem, pseudocode and/or code is provided along with short discussions.

Uploaded by

Sanjidul Hasan
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/ 8

Problem Statement:-1

Implement Insertion Sort Algorithm

Algorithm:
1. Start

2. Take intput arry[] and size.

3. Set valu = index 1 and index = i

4. While index>0&&arr[index-1]<valu

5. Arry index = index-1

6. Arry index = valu

7. End

Code:
#include<iostream>

using namespace std;

inserts(int arr[],int x) // x is arry size

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;

cout<<"How Many number do you want to sort : ";

cin>>n;

int arr[n];

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

cout<<"Enter your element : ";

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

2. Take intput arry[] and size.

3. Each index must be compared with all other indices

4. For ascending order ,

If index [o] >[1]

swap

5. Print sorted arry

6. End

Code:
#include<iostream>

using namespace std;

int bubblesort(int arry[],int Size){

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;

cout<<"How Many number do you want to sort : ";

cin>>n;

int arr[n];

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

cout<<"Enter your element : ";

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

2. Take intput arry[] and size.

3. Each index must be compared with all other indices

4. For ascending order ,

If index [o] >[1]

swap

5. In this iteration run only 2 time

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

2. Take input arr[], and size x

3. Sort arr[] to Descending Order

4. Subtruction 0 index to other index

5. Print answer

6. end

Code:
#include<iostream>

using namespace std;

inserts(int arr[],int x){

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.

You might also like