0% found this document useful (0 votes)
17 views4 pages

Document 1

The document contains 3 code snippets demonstrating binary search, factorial calculation, and quicksort algorithms. The first snippet shows recursive and iterative binary search functions. The second calculates factorials recursively and iteratively. The third implements quicksort using an Lomuto partition scheme.
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)
17 views4 pages

Document 1

The document contains 3 code snippets demonstrating binary search, factorial calculation, and quicksort algorithms. The first snippet shows recursive and iterative binary search functions. The second calculates factorials recursively and iteratively. The third implements quicksort using an Lomuto partition scheme.
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/ 4

DAA LAB 2 ASSIGNMENT

1)A

#include <bits/stdc++.h>
using namespace std;
int recursivebinarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return recursivebinarySearch(arr, l, mid - 1, x);
return recursivebinarySearch(arr, mid + 1, r, x);
}
return -1;
}
int main()
{
int arr[]={2,4,10,44,23,11,22};
int x = 10;
int n=sizeof(arr)/sizeof(arr[0]);
int result = recursivebinarySearch(arr, 0, n - 1, x);
(result == -1)
? cout << "Element is not present in array"
: cout << "Element is present at index " << result;
return 0;
}

1B)

#include <bits/stdc++.h>
using namespace std;
int binarySearch(int arr[], int l, int r, int x)
{
while (l <=r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
int main(void)
{
int arr[]={2,4,10,44,23,11,22};
int x = 10;
int n=sizeof(arr)/sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
(result == -1)
? cout << "Element is not present in array"
: cout << "Element is present at index " << result;
return 0;
}

Output:

2)

#include<bits/stdc++.h>
using namespace std;

int factorial(int n){


if(n == 0 || n == 1){
return 1;
}
return n*factorial(n-1);
}

int main(){

int n;
cin >> n;

long long fact = 1;


for(int i = 1;i<=n;i++){
fact *= i;
}

//Non-Recursive

//Time Complexity - O(n)


//Space Complexity - O(1)
//Auxiliary Space - O(1)

cout << "Factorial of " << n << " is: " << fact << endl;

//Recursive

//Time Complexity - O(n)


//Space Complexity - O(n)
//Auxiliary Space - O(n)

cout << "Factorial of " << n << " is: " << factorial(n) << endl;

Output:

3)

#include <iostream>
using namespace std;

int partition(int arr[], int start, int end)


{
int pivot = arr[start];
int count = 0;
for (int i = start + 1; i <= end; i++) {
if (arr[i] <= pivot)
count++;
}
int pivotIndex = start + count;
swap(arr[pivotIndex], arr[start]);
int i = start, j = end;

while (i < pivotIndex && j > pivotIndex) {

while (arr[i] <= pivot) {


i++;
}

while (arr[j] > pivot) {


j--;
}

if (i < pivotIndex && j > pivotIndex) {


swap(arr[i++], arr[j--]);
}
}
return pivotIndex;
}

void quickSort(int arr[], int start, int end)


{

if (start >= end)


return;
int p = partition(arr, start, end);
quickSort(arr, start, p - 1);
quickSort(arr, p + 1, end);
}

int main()
{
int n,arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
n=sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n - 1);

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


cout << arr[i] << " ";
}
return 0;
//space complexity best case o(1)
Worst case o(n)

Output:

You might also like