Professional Documents
Culture Documents
Practical 3
Aim:
Implement and analyze the algorithms given below. (Divide and Conquer Strategy)
#include<iostream>
using namespace std;
int divide = 0;
int conquer = 0; // when you sort something
int combine = 0; // when you merge something
int counter = 0;
counter++;
counter++;
counter++;
int len1 = mid - s + 1;
counter++;
counter++;
int len2 = e - mid;
counter++;
int* arr1 = new int[len1];
counter++;
int* arr2 = new int[len2];
counter++;
int mainArrayIndex = s;
counter++;
counter++;
counter++;
arr1[i] = arr[mainArrayIndex++];
counter++;
}
counter++;
counter++;
counter++;
mainArrayIndex = mid+1;
counter++;
for(int j=0; j<len2;j++){
counter++;
counter++;
counter++;
arr2[j] = arr[mainArrayIndex++];
counter++;
}
counter++;
counter++;
int j = 0;
counter++;
mainArrayIndex = s;
counter++;
while(i<len1 && j<len2){
counter++;
if(arr1[i]<arr2[j]){
counter++;
counter++;
counter++;
combine++;
arr[mainArrayIndex++] = arr1[i++];
}
else{
counter++;
counter++;
counter++;
combine++;
arr[mainArrayIndex++] = arr2[j++];
}
}
counter++;
while(i<len1){
counter++;
counter++;
counter++;
combine++;
arr[mainArrayIndex++] = arr1[i++];
}
counter++;
while(j<len2){
counter++;
counter++;
counter++;
combine++;
arr[mainArrayIndex++] = arr2[j++];
}
counter++;
delete[] arr1;
counter++;
delete[] arr2;
//BaseCase
counter++;
if(s>=e){
counter++;
return;
}
counter++;
counter++;
counter++;
counter++;
int mid = s + (e-s)/2;
int main(){
int n;
cout << "Enter the value of n:";
cin>>n;
counter++;
int* arr = new int[n];
counter++;counter++;
mergeSort(arr,0,n-1);
counter++;
printArr(arr,n);
cout << "The value of counter is:" << counter << endl;
cout << "The value of divide is :- " << divide << endl;
cout << "The value of conquer is :- " << conquer << endl;
cout << "The value of combine is :- " << combine << endl;
counter++;
return 0;
Output:
Analysis Table:
Inpu
Best Case Average Case Worst Case
t
Instructio
Instruction Instruction
n
Inpu
Best Case Average Case Worst Case
t
divid conque combin divid conque combin divid conque combin
e r e e r e e r e
5 8 9 16 8 9 16 8 9 16
10 18 19 43 18 19 43 18 19 43
15 28 29 73 28 29 73 28 29 73
20 38 39 107 38 39 107 38 39 107
25 48 49 142 48 49 142 48 49 142
Graph:
Instruction-Merge Sort
2000
f(x) = 81.4
80.7 x − 160.6
81.1 159.3
158.7
INstruction 1500 0.998975267719751
R² = 0.998957302310278
0.999035218127959
1000
500
0
0 5 10 15 20 25 30
Input
80
60
40 1
f(x) = 2 x − 2
20 R² = 1
0
0 5 10 15 20 25 30
INputs
60
40 1
f(x) = 2 x − 2
20 R² = 1
0
0 5 10 15 20 25 30
INputs
60
40 1
f(x) = 2 x − 2
20 R² = 1
0
0 5 10 15 20 25 30
Inputs
Complexity: -
INSTRUCTION
Best Worst
Case AverageCase case
Practical y=
Complexity y= 80.7x
80.7x - y = 80.7x - -
158.7 158.7 158.7
Theoretical O(n O(n
Complexity O(n logn)
logn) logn)
Practical y= y=
Complexit y = 2x y = 2x y = 2x y = 2x 6.32x y = 6.32x - 6.32x
y -1 y = 2x - 1 -1 -2 y = 2x - 2 -2 - 18.6 18.6 - 18.6
Theoretic
al
O(n) O(n) O(n) O(n) O(n) O(n) O(n) O(n) O(n)
Complexit
y
Conclusion: -
From this practical, we come to know that best case instruction, best case compare and best-case
combine, best case conquer, best case divide’s graph is linear in nature.
#include<iostream>
using namespace std;
int divide = 0;
int conquer = 0; // when you sort something
int combine = 0; // when you merge something
int counter = 0;
counter++;
int cnt = 0;
counter++;counter++;
for(int i=s+1; i<=e; i++){
counter++;
counter++;
if(arr[i]<=pivot){
counter++;
cnt++;
}
counter++;
}
counter++;
counter++; counter++;
int pivotIndex = s + cnt;
counter++;
swap(arr[pivotIndex],arr[s]);
// ""<e"......."e"...........">e"
counter++;counter++;
int i=s, j=e;
counter++;
while(i<pivotIndex && j>pivotIndex){
counter++;
while(arr[i]<=pivot){
counter++;
i++;
}
counter++;
while(arr[j]>pivot){
counter++;
j--;
}
counter++;
if(i<pivotIndex && j>pivotIndex){
counter++;counter++;counter++;
swap(arr[i++],arr[j--]);
}
}
counter++;
return pivotIndex;
}
conquer++;
//BaseCase
counter++;
if(s>=e){
counter++;
return;
}
//Partition
counter++;
int p = partition(arr,s,e);
int main(){
int n;
cout << "Enter the value of n:";
cin>>n;
counter++;
int* arr = new int[n];
counter++;
cin>>arr[i];
counter++;
}
counter++;
counter++;counter++;
quickSort(arr,0,n-1);
counter++;
printArr(arr,n);
cout << "The value of counter is:" << counter << endl;
cout << "The value of divide is :- " << divide << endl;
cout << "The value of conquer is :- " << conquer << endl;
cout << "The value of combine is :- " << combine << endl;
counter++;
return 0;
}
Output:
Analysis Table:
Inpu
Best Case Average Case Worst Case
t
divid conque combin divid conque combin divid conque combin
e r e e r e e r e
5 8 9 8 6 7 6 8 9 8
10 18 19 18 10 11 10 18 19 18
15 28 29 28 16 17 16 28 29 28
20 38 39 38 24 25 24 38 39 38
25 48 49 48 26 27 26 48 49 48
Graph:
800
f(x) = 38.68 x − 96.2000000000001
600 R² = 0.993290879058429
400
200
0
0 5 10 15 20 25 30
INput
20
10
0
0 5 10 15 20 25 30
inputs
R²
R² =
= 0.974598930481283
0.974598930481283
20
15
10
5
0
0 5 10 15 20 25 30
inputs
20
10
0
0 5 10 15 20 25 30
inputs
Complexity: -
INSTRUCTION
Best AverageCas Worst
Case e case
Practical
Complexity O(n) O(n) O(n) O(n) O(n) O(n) O(n) O(n) O(n)
Theoretical
O(n) O(n) O(n) O(n) O(n) O(n) O(n) O(n) O(n)
Complexity
Conclusion: -
From this practical, we come to know that best case instruction, best case compare and best-case
combine, best case conquer, best case divide’s graph is linear in nature.
#include<iostream>
using namespace std;
int divide = 0;
int conquer = 0; // when you sort something'
int combine = 0;
int counter = 0;
counter++;
int start =0;
counter++;counter++;
int end = size-1;
counter++;counter++;counter++;counter++;
int mid = start + ((end-start) / 2);
counter++;
while(start<=end){
counter++;
if(arr[mid]==key){
counter++;
return mid;
}
//Right part
counter++;
if(arr[mid]<key){
divide++;
counter++;counter++;
start = mid + 1;
}
//Left part
else{
divide++;
counter++;counter++;
end = mid-1;
}
counter++;counter++;counter++;counter++;
mid = start + ((end-start) / 2);
}
counter++;
return -1;
}
int main(){
int n;
cout << "Enter the value of n:";
cin>>n;
counter++;
int* arr = new int[n];
int k;
cout << "Enter the value of key:";
cin>>k;
counter++;counter++;
int ans = binarySearch(arr,n,k);
counter++;
if(ans!=-1){
cout << "The " << k << " is found at " << ans << endl;
}else{
cout << k << " is not found " << endl;
}
cout << "The value of counter is:" << counter << endl;
cout << "The value of divide is :- " << divide << endl;
cout << "The value of conquer is :- " << conquer << endl;
cout << "The value of divide is :- " << combine << endl;
counter++;
return 0;
}
Output:
Analysis Table:
5 26 42 49
10 36 52 67
15 46 54 77
20 56 80 95
25 66 90 105
Inpu
Best Case Average Case Worst Case
t
divid conque combin divid conque combin divid conque combin
e r e e r e e r e
5 0 0 0 2 0 0 3 0 0
10 0 0 0 2 0 0 4 0 0
15 0 0 0 1 0 0 4 0 0
20 0 0 0 3 0 0 5 0 0
25 0 0 0 3 0 0 5 0 0
Graph:
f(x) = 2 x + 16
40 R² = 1
20
0
0 5 10 15 20 25 30
inputs
1.5
1
0.5
0
0 5 f(x) = 0 10 15 20 25 30
R² = 0
Inputs
Complexity: -
INSTRUCTION
Bes
t
Cas AverageCa Worst
e se case
Practical
O(1 O(log
Complexi O(logn)
) n)
ty
Theoreti
cal O(1 O(log
O(logn)
Complexi ) n)
ty
Practical
O(n O(n
Complexit O(n) O(n) O(n) O(n) O(n) O(n) O(n)
) )
y
Theoretic O(n O(n) O(n) O(n O(n) O(n) O(n) O(n) O(n)
al ) )
Complexit
y
Conclusion: -
In this practical because it is not sorting algorithm hence value of combine and conquer is 0
AND
The value of Divide is based on cases we have.