0% found this document useful (0 votes)
32 views

Assignment 1 DAA: - Binary Search (Iterative)

This document contains C code implementations of binary search, insertion sort, selection sort, and bubble sort algorithms. It also includes sample outputs for each algorithm showing them sorting an array of numbers.

Uploaded by

Nivedi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Assignment 1 DAA: - Binary Search (Iterative)

This document contains C code implementations of binary search, insertion sort, selection sort, and bubble sort algorithms. It also includes sample outputs for each algorithm showing them sorting an array of numbers.

Uploaded by

Nivedi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Assignment 1

DAA
Submitted By- Nivedi Singhal

189301049

• Binary Search(Iterative)

#include <stdio.h>

int binary_iterative(int a[], int l, int r, int x)


{
while (l <= r) {
int m = l + (r - l) / 2;

if (a[m] == x)
return m;

if (a[m] < x)
l = m + 1;

else
r = m - 1;
}
return -1;
}

int main(void)
{
int a[] = { 5,7,20,45,56};
int n = sizeof(a) / sizeof(a[0]);
int x = 20;
int result = binary_iterative(a, 0, n - 1, x);
(result == -1) ? printf("Element is not present"
" in array")
: printf("Element is present at "
"index %d",
result);
return 0;
}

Output: Element is present at index 2


• Binary Search(Recursive)
#include <stdio.h>

int binary_recursive(int a[], int l, int r, int x)


{
if (r >= l) {
int mid = l + (r - l) / 2;

if (a[mid] == x)
return mid;

if (a[mid] > x)
return binary_recursive(a, l, mid - 1, x);
return binary_recursive(a, mid + 1, r, x);
}
return -1;
}

int main(void)
{
int a[] = { 34,40,57,60,70};
int n = sizeof(a) / sizeof(a[0]);
int x = 57;
int result = binary_recursive(a, 0, n - 1, x);
(result == -1) ? printf("Element is not present in array")
: printf("Element is present at index %d",
result);
return 0;
}

Output: Element is present at index 2


• Insertion Sort
#include <stdio.h>

int main()
{
int i,j,n,a[100],value,hole;
printf("enter the no of elements");
scanf("%d",&n);
printf("enter array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<n;i++){
value=a[i];
j=i;
while(j>0 && value<a[j-1]){
a[j]=a[j-1];
j--;
}
a[j]=value;
}
printf("sorted array:");
for(i=0;i<n;i++){
printf("%d",a[i]);
}
return 0;
}

Output: enter the no of elements

enter array 56 78 3 45 2

sorted array: 2 3 45 56 78
• Selection Sort
#include <stdio.h>
void swap(int *i,int *j){
int temp;
temp=*i;
*i=*j;
*j=temp;

int main()
{
int i,j,n,a[100],mini;
printf("enter the no of elements");
scanf("%d",&n);
printf("enter array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-2;i++){
mini=i;
for(j=i+1;j<n-1;j++){
if(a[j]<a[mini]){
mini=j;
}
swap(&a[mini],&a[i]);
}
}
printf("the sorted array is");
for(i=0;i<n;i++){
printf("%d",a[i]);
}
return 0;
}

Output: enter the no of elements

enter array 34 89 5 45 12 56

the sorted array is 5 12 34 45 56 89


• Bubble Sort
#include <stdio.h>
void swap(int *i,int *j){
int temp;
temp=*i;
*i=*j;
*j=temp;

int main()
{
int i,j,n,a[100];
printf("enter the no of elements");
scanf("%d",&n);
printf("enter array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<n-1;i++){
for(j=0;j<n-2;j++){
if(a[j]>a[j+i]);
swap(&a[j],&a[j+1]);
}
}
printf("the sorted array is");
for(j=0;j<n;j++){
printf("%d",a[j]);
}
return 0;
}

Output: enter the no of elements

enter array 56 78 3 9

the sorted array is 3 9 56 78

You might also like