Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions binarysearch_pavithra.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
write a program to binary search an elemnt in c language
#include<stdio.h>
int main()
{
int arr[4];
int n;
int i;
int low;
int high;
int mid;
int x;
printf("no of elements in array\n");
scanf("%d",&n);
printf("elements are \n");
for (i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("enter x");
scanf("%d",&x);
low=0;
high= n-1;
mid =low+(high-low)/2 ;
while(low<=high)
{
if (arr[mid] < x)
low= mid + 1;
else if (arr[mid] == x)
{
printf("%d found at location %d.\n", x, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if (low > high)
printf("Not found! %d isn't present in the list.\n", x);
}

output
no of elements in array
7
elements are
1 2 4 6 8 9 11
enter x 8
8 found at location 5.



50 changes: 50 additions & 0 deletions selectionsort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//wap to do selection sort in linkedlist

#include<stdio.h>

int findMinIdx(int arr[], int start, int end);
void display(int arr[]);
void selectionsort(int arr[], int start, int end);

int findMinIdx(int arr[], int start, int end)
{
int min_idx=start;
int i;
for(i=start+1;i<=end;i++)
{
if(arr[min_idx]>arr[i])
min_idx=i;
}
return min_idx;
}
void display(int arr[])
{
int i;
for(i=0;i<=5;i++)
{
printf("%d ",arr[i]);
}
}
void swap(int* a, int* b)
{
int temp= *b;
*b=*a;
*a=temp;
}

void selectionsort(int arr[], int start, int end)
{
int i; int min_idx; int x;
for(i=start; i<=end;i++)
{
min_idx=i;
x=findMinIdx(arr,i,end);
swap(&arr[min_idx],&arr[x]);
}
}
void main()
{
int arr[6]={1,6,2,5,8,9};
selectionsort(arr,0,5);
display(arr);
}