Lab Exercise (Le) :: Algorithm Lab Report Section-Ecsc-2
Lab Exercise (Le) :: Algorithm Lab Report Section-Ecsc-2
Lab Exercise (Le) :: Algorithm Lab Report Section-Ecsc-2
Section-ECSc-2
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]);
}
}
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;
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){
9
arr[k++]=right[j++];
}
10
II. SCREENSHOTS
Test Case Output-1
DECLARATION
I hereby declare that,
11