Lab Exercise (Le) :: Algorithm Lab Report Section-Ecsc-2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

ALGORITHM LAB REPORT

Section-ECSc-2

EXERCISE NO: 6 DATE OF EXERCISE: 06.09.2021


ROLL NUMBER: 1930187 SUB-GROUP NO.: 3
NAME IN CAPITAL : Shubham Khuntia

LAB EXERCISE (LE):

6.1) The quick sort algorithm is an efficient and popular sorting technique that sorts
a list of keys recursively by choosing a pivot key. A pivot may be chosen as the first or
last or mean or median or any random element of the list. Write a program to
implement this sorting algorithm and execute the sorting programs for the following
sets of data.
i. Ordered List
ii. Reverse order List
iii. A list containing the same value through out
iv. Random List
v. 50% of the List sorted
Also measure CPU time, number of partitions and number of comparisons for data
sizes 1K, 50K, 1L, 1.5L, 2L, 2.5L, 3L, 3.5L, 4L, 4.5L and 1M. Present your results using
tables and graphs.
I. SOURCE CODE:
#include <bits/stdc++.h>
using namespace std;
int part=0;
int compare=0;

void resetCounter()
{
part=0;
compare=0;
}
int partition(int arr[], int l, int h)
{
int pivot=arr[l];
int i=l-1,j=h+1;
while(true){
do{

1
i++;
part++;
}while(arr[i]<pivot);
do{
j--;
part++;
}while(arr[j]>pivot);
if(i>=j)return j;
swap(arr[i],arr[j]);
}
}

void qSort(int arr[],int l,int h){


if(l<h){
compare++;
int p=partition(arr,l,h);
qSort(arr,l,p);
qSort(arr,p+1,h);
}
}
int main() {

int size;
cout<<"Enter the size of the array : ";
cin>>size;
int A[size];
for(int i=0;i<size;i++)
A[i]=rand()%200;
cout<<"1)Ordered List : ";
sort(A,A+size);
for(int i=0;i<size;i++)
cout<<A[i]<<" ";
cout<<endl;
qSort(A,0,size-1);
cout<<"Sorted List : ";
for(int i=0;i<size;i++)
cout<<A[i]<<" ";
cout<<endl;

cout<<"2)Reverse Ordered List : ";


sort(A,A+size,greater<int>());
for(int i=0;i<size;i++)
cout<<A[i]<<" ";
cout<<endl;
qSort(A,0,size-1);
cout<<"Sorted List : ";

2
for(int i=0;i<size;i++)
cout<<A[i]<<" ";
cout<<endl;

int same[size];
for(int i=0;i<size;i++)
same[i]=2;
cout<<"3)List containing the same value throughout : ";
for(int i=0;i<size;i++)
cout<<same[i]<< " ";
cout<<endl;
qSort(same,0,size-1);
cout<<"Sorted List : ";
for(int i=0;i<size;i++)
cout<<same[i]<<" ";
cout<<endl;
int random[size];
cout<<"4)Random List : ";
for(int i=0;i<size;i++)
random[i]=rand()%100;
for(int i=0;i<size;i++)
cout<<random[i]<<" ";
cout<<endl;
qSort(random,0,size-1);
cout<<"Sorted List : ";
for(int i=0;i<size;i++)
cout<<random[i]<<" ";
cout<<endl;
int random1[size];
cout<<"5)50% of the List Sorted : ";
for(int i=0;i<size;i++)
random1[i]=rand()%100;
sort(random1,random1+size/2);
for(int i=0;i<size;i++)
cout<<random1[i]<<" ";
cout<<endl;
qSort(random1,0,size-1);
cout<<"Sorted List : ";
for(int i=0;i<size;i++)
cout<<random1[i]<<" ";
cout<<endl;

int
arr[]={1000,50000,100000,150000,200000,250000,300000,350000,400000,1000000
};
time_t start, end;

3
double time_taken;
cout<<"\n \t\t\tQuick Sort "<<endl;
cout<<"Sl_No.\tSize\tCPU_Time\tNo of Partition\t No of Comparison"<<endl;
for(int j=0;j<10;j++)
{
int n=arr[j];
int a[n];
for(int i=0;i<n;i++)
a[i]=rand()%200;
start=clock();
qSort(a,0,n-1);
end=clock();
time_taken = double(end - start)/double(CLOCKS_PER_SEC);
cout<<j+1<<"\t"<<n<<"\t"<<time_taken<<"\t\t"<<part<<"\t\t
"<<compare<<endl;
}
}

II. SCREENSHOTS
Test Case Output-1

4
Test Case Output-2

6.2) A and B are playing a guessing game where B first thinks up an integer X
(positive, negative or zero, and could be of arbitrarily large magnitude) and A tries to
guess it. In response to A’s guess, B gives exactly one of the following three replies:
a) Try a bigger number
b) Try a smaller number or
c) You got it.
Write a program by designing an efficient algorithm to minimize the number of
guesses A has to make. An example (not necessarily an efficient one) below:
Let B thinks up the number 35
A’s guess
10
20
30
40
35

5
B’s response
Try a bigger number
Try a bigger number
Try a bigger number
Try a smaller number
You got it
I. SOURCE CODE:
#include<bits/stdc++.h>
using namespace std;

int main()
{
int number, guess;

int t=1;

number=1+rand()%100;
do
{
cout<<"Guess the number between 1 to 100 : ";
cin>>guess;
if(guess>number)
{
cout<<"Try a smaller number\n";
}
else if(guess<number)
{
cout<<"Try a bigger number\n";
}
else
{
cout<<"You got it.";
cout<<"\nRandom no. generated was : "<<number;
cout<<"\nNo. of attempts = "<<t;
}
t++;
} while(guess!=number);
return 0;
}

6
II. SCREENSHOTS
Test Case Output -

HOME EXERCISE(HE):

6.3) In a social gathering, there are b boys and g girls (b > g) of different ages. You
have two unsorted arrays giving their ages (one for the boys, the other for the girls).
Write a program by devising an efficient O(b log g) algorithm to find out the ages
that are common between both the boys and girls.
Example:
If Arrayboy = {10, 20, 11, 89, 23, 21} and Arraygirl = {12, 30, 11, 20},
Then Arraycommon = {11, 20}.
I. SOURCE CODE:
#include<bits/stdc++.h>
using namespace std;
int main()
{

int b,g;
cout<<"Enter the no. of boys : ";
cin>>b;
cout<<"Enter the no. of girls : ";
cin>>g;
int boy[b],girl[g];
cout<<"Enter the age of boys : ";
for(int i=0;i<b;i++)
{
cin>>boy[i];
}
cout<<"Enter the age of girls : ";
for(int i=0;i<g;i++)
{

7
cin>>girl[i];
}
sort(boy,boy+b);
sort(girl,girl+g);
int i=0,j=0;
vector<int> v;
while(i<b&&j<g)
{
if(boy[i]==girl[j])
{
v.push_back(boy[i]);
i++;
j++;
}
else if(boy[i]<girl[j])
i++;
else
j++;
}
if(v.size()==0)
cout<<"No common ages ";
else
{
cout<<"Common ages = ";
for(int t=0;t<v.size();t++)
cout<<v[t]<<" ";
}
}

II. SCREENSHOTS
Test Case Output-1

8
Test Case Output-2

6.4) Refer the following new sort algorithm for sorting an array A of n numbers. The
algorithm is described below:
(i) If there is only one number, return.
(ii) If there are two numbers, perform a single comparison to determine the order.
(iii) If there are more than two numbers, then first sort the top two-thirds of the
elements recursively. Follow this by sorting the bottom two-thirds of the
elements recursively and then sorting the top two-thirds of the elements again.
a) Write a program that uses a recursive algorithm to implement the above strategy.
b) What is the comparison complexity of this new-sort algorithm? Formulate a
recurrence relation and solve the same to justify your answer.
I. SOURCE CODE:
#include<bits/stdc++.h>
using namespace std;
void merge(int arr[], int l, int m, int h){

int n1=m-l+1, n2=h-m;


int left[n1],right[n2];
for(int i=0;i<n1;i++)
left[i]=arr[i+l];
for(int j=0;j<n2;j++)
right[j]=arr[m+1+j];
int i=0,j=0,k=l;
while(i<n1 && j<n2){
if(left[i]<=right[j])
arr[k++]=left[i++];
else
arr[k++]=right[j++];
}
while(i<n1)
arr[k++]=left[i++];
while(j<n2)

9
arr[k++]=right[j++];
}

void Sort(int arr[],int l,int r){


if(r>l){
int m=l+(r-l)/2;
Sort(arr,l,m);
Sort(arr,m+1,r);
merge(arr,l,m,r);
}
}
void new_sorting(int arr[],int n)
{
if(n==1)
return ;
else if(n==2)
{
if(arr[0]>arr[1])
swap(arr[0],arr[1]);
}
else
{
int pos=(2*n)/3;
int pos1=n-pos;
Sort(arr,0,pos-1);
Sort(arr,pos1-1,n-1);
Sort(arr,0,pos);
}
}
int main()
{
int n;
cout<<"Enter the size of the array : ";
cin>>n;
int a[n];
cout<<"Enter the elements of the array : ";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
new_sorting(a,n);
cout<<"Sorted array : ";
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
}

10
II. SCREENSHOTS
Test Case Output-1

Test Case Output-2

DECLARATION
I hereby declare that,

 I have written the assignment in my own handwritting as mentioned in Handwritten


Code Section.
 I have typed my source code in code editor and taken my own test case output after
running of code .

Full Signature of the Student

11

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