You are on page 1of 24

IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

Practical 3
Aim:
Implement and analyze the algorithms given below. (Divide and Conquer Strategy)

Program: Merge Sort

#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;

void merge(int* arr, int s, int e){


counter++;
counter++;
counter++;
counter++;
int mid = s + (e-s)/2;

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++;

ID No: - D22IT184 Page 30


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

for(int i=0; i<len1; i++){


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++;

//Merge Two Sorted Arrays


counter++;
int i = 0;

counter++;
int j = 0;

counter++;
mainArrayIndex = s;

counter++;
while(i<len1 && j<len2){
counter++;
if(arr1[i]<arr2[j]){
counter++;

ID No: - D22IT184 Page 31


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

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;

void mergeSort(int* arr, int s, int e){

conquer++; // actual logic for sorting

ID No: - D22IT184 Page 32


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

//BaseCase
counter++;
if(s>=e){
counter++;
return;
}

counter++;
counter++;
counter++;
counter++;
int mid = s + (e-s)/2;

//Sorting left part


divide++;
counter++;
mergeSort(arr,s,mid);

//Sorting right part


divide++;
counter++;counter++;
mergeSort(arr,mid+1,e);

//Merge two Sorted Arrays


counter++;
combine++; // combining after sorting done
merge(arr,s,e);
}

void printArr(int* arr, int n){


cout << "Sorted Array: -"<< endl;
counter++;
for(int i=0; i<n; i++){
counter++;
cout << arr[i] << " ";
counter++;
}
counter++;
cout << endl;
}

int main(){

ID No: - D22IT184 Page 33


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

int n;
cout << "Enter the value of n:";
cin>>n;

counter++;
int* arr = new int[n];

cout << "Enter the array Elements:";


counter++;
for(int i=0; i<n; i++){
counter++;
// cin>>arr[i];
if(i<n/2){
// Best Case
arr[i] = i+1;
}
else{
// Worst Case
arr[i] = n-(i);
}
counter++;
}
counter++;

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;

ID No: - D22IT184 Page 34


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

Output:

Analysis Table:

Inpu
Best Case Average Case Worst Case
t
Instructio
Instruction Instruction
n

5 269 269 267


10 640 642 636
15 1032 1035 1029
20 1457 1462 1449
25 1888 1894 1878

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

ID No: - D22IT184 Page 35


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

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

Best Case Instruction Linear (Best Case Instruction)


Average Case Instruction Linear (Average Case Instruction)
Worst Case Instruction Linear (Worst Case Instruction)

Best Case - Merge Sort


160
140
120 f(x) = 6.32 x − 18.6
100 R² = 0.997084315213484
Cases

80
60
40 1
f(x) = 2 x − 2
20 R² = 1
0
0 5 10 15 20 25 30
INputs

Best Case divide Linear (Best Case divide)


Best Case conquer Linear (Best Case conquer)
Best Case combine Linear (Best Case combine)

ID No: - D22IT184 Page 36


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

Avg Case - Merge Sort


160
140
120 f(x) = 6.32 x − 18.6
100 R² = 0.997084315213484
80
Cases

60
40 1
f(x) = 2 x − 2
20 R² = 1
0
0 5 10 15 20 25 30
INputs

Average Case divide Linear (Average Case divide)


Average Case conquer Linear (Average Case conquer)
Average Case combine Linear (Average Case combine)

Worst Case - Merge Sort


160
140
120 f(x) = 6.32 x − 18.6
100 R² = 0.997084315213484
80
Cases

60
40 1
f(x) = 2 x − 2
20 R² = 1
0
0 5 10 15 20 25 30
Inputs

Worst Case divide Linear (Worst Case divide)


Worst Case conquer Linear (Worst Case conquer)
Worst Case combine Linear (Worst Case combine)

ID No: - D22IT184 Page 37


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

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)

Divide Conquer Combine


Best AverageCas Worst Best AverageCas Worst Best AverageCas Worst
Case e case Case e case Case e case

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.

ID No: - D22IT184 Page 38


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

Program: Quick Sort

#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;

int partition(int* arr, int s, int e){


//Pivot element
counter++;
int pivot = arr[s];

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]);

ID No: - D22IT184 Page 39


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

// ""<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;
}

void quickSort(int* arr, int s, int e){

conquer++;

//BaseCase
counter++;
if(s>=e){
counter++;
return;
}

ID No: - D22IT184 Page 40


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

//Partition
counter++;
int p = partition(arr,s,e);

//pivot ke Left wala Part


divide++;
counter++;counter++;
quickSort(arr,s,p-1);
combine++;

//Pivot ke right wala part


divide++;
counter++;counter++;
quickSort(arr,p+1,e);
combine++;
}

void printArr(int* arr, int n){


cout << "Sorted Array: -"<< endl;
counter++;
for(int i=0; i<n; i++){
counter++;
cout << arr[i] << " ";
counter++;
}
counter++;
cout << endl;
}

int main(){

int n;
cout << "Enter the value of n:";
cin>>n;

counter++;
int* arr = new int[n];

cout << "Enter the array Elements:";


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

ID No: - D22IT184 Page 41


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

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:

ID No: - D22IT184 Page 42


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

Analysis Table:

Inpu Average Worst


Best Case
t Case Case
Instructio Instructio
Instruction
n n

5 144 124 146


10 374 265 390
15 679 458 735
20 1059 699 1159
25 1514 874 1670

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:

Instruction - Quick Sort


1800
1600
1400 f(x) = 76.34 x − 325.1
1200 f(x)
R² = =0.981889269737206
68.5 x − 273.5
1000 R² = 0.983494026409558
Cases

800
f(x) = 38.68 x − 96.2000000000001
600 R² = 0.993290879058429
400
200
0
0 5 10 15 20 25 30
INput

Best Case Instruction Linear (Best Case Instruction)


Average Case Instruction Linear (Average Case Instruction)
Worst Case Instruction Linear (Worst Case Instruction)

ID No: - D22IT184 Page 43


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

Best Case - Quick Sort


60
50
1
f(x) = 2 x − 2
40
R² = 1
30
cases

20
10
0
0 5 10 15 20 25 30
inputs

Best Case divide Linear (Best Case divide)


Best Case conquer Linear (Best Case conquer)
Best Case combine Linear (Best Case combine)

Avg Case - Quick Sort


30
25 f(x)
f(x) =
= 1.08
1.08 xx +
+ 1.2
0.199999999999996
cases


R² =
= 0.974598930481283
0.974598930481283
20
15
10
5
0
0 5 10 15 20 25 30
inputs

Average Case divide Linear (Average Case divide)


Average Case conquer Linear (Average Case conquer)
Average Case combine Linear (Average Case combine)

ID No: - D22IT184 Page 44


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

Worst Case - Quick Sort


60
50
1
f(x) = 2 x − 2
40
R² = 1
30
cases

20
10
0
0 5 10 15 20 25 30
inputs

Worst Case divide Linear (Worst Case divide)


Worst Case conquer Linear (Worst Case conquer)
Worst Case combine Linear (Worst Case combine)

Complexity: -

INSTRUCTION
Best AverageCas Worst
Case e case

Practical O(n O(n


Complexity O(n logn)
logn) logn)
Theoretical O(n O(n
Complexity O(n logn)
logn) logn)

Divide Conquer Combine


Best AverageCas Worst Best AverageCas Worst Best AverageCas Worst
Case e case Case e case 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

ID No: - D22IT184 Page 45


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

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.

ID No: - D22IT184 Page 46


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

Program: Binary Search

#include<iostream>
using namespace std;

int divide = 0;
int conquer = 0; // when you sort something'
int combine = 0;
int counter = 0;

int binarySearch(int arr[], int size, int key){

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;

ID No: - D22IT184 Page 47


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

}
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];

cout << "Enter the array Elements:";


counter++;
for(int i=0; i<n; i++){
counter++;
cin>>arr[i];
counter++;
}
counter++;

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;
}

ID No: - D22IT184 Page 48


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

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:

Inpu Average Worst


Best Case
t Case Case
Instructio Instructio
Instruction
n n

5 26 42 49
10 36 52 67
15 46 54 77
20 56 80 95
25 66 90 105

ID No: - D22IT184 Page 49


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

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:

Instruction - Binary Search


120
100 f(x) = 2.8 x + 36.6
80 R²
f(x)= =0.990299110751819
2.48 x + 26.4
60 R² = 0.926711668273867
cases

f(x) = 2 x + 16
40 R² = 1
20
0
0 5 10 15 20 25 30
inputs

Best Case Instruction Linear (Best Case Instruction)


Average Case Instruction Linear (Average Case Instruction)
Worst Case Instruction Linear (Worst Case Instruction)

ID No: - D22IT184 Page 50


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

Best Case - Binary Search


1
0.8
Cases 0.6
0.4
0.2
0
0 5 f(x) = 0 10 15 20 25 30
R² = 0
Inputs

Best Case divide Linear (Best Case divide)


Best Case conquer Linear (Best Case conquer)
Best Case combine Linear (Best Case combine)

Avg Case - Binary Search


3.5
3
2.5 f(x) = 0.06 x + 1.3
2 R² = 0.321428571428571
Cases

1.5
1
0.5
0
0 5 f(x) = 0 10 15 20 25 30
R² = 0
Inputs

Average Case divide Linear (Average Case divide)


Average Case conquer Linear (Average Case conquer)
Average Case combine Linear (Average Case combine)

ID No: - D22IT184 Page 51


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

Worst Case - Binary Search


6
5
f(x) = 0.1 x + 2.7
Cases 4 R² = 0.892857142857143
3
2
1
0
0 5 f(x) = 0 10 15 20 25 30
R² = 0
Inputs

Worst Case divide Linear (Worst Case divide)


Worst Case conquer Linear (Worst Case conquer)
Worst Case combine Linear (Worst Case combine)

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

Divide Conquer Combine


Best Best Wors
Cas AverageCas Wors Cas AverageCas t Best AverageCas Worst
e e t case e e case Case e case

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 ) )

ID No: - D22IT184 Page 52


IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)

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.

ID No: - D22IT184 Page 53

You might also like