Skip to content

written code on selection sort in c #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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.



76 changes: 76 additions & 0 deletions diagnoal_traversal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
WAP to print the lements of an array in diagonal traversal

#include<stdio.h>
void main()
{
int arr[8][8];
int put=1;
int i,j;
int m=7;
arr[0][0]=put;
for(i=0;i<=m;i++)
{
for(j=0;j<=m;j++)
{
printf("%2d ",put);

arr[i][j]=put++;
}printf("\n");
}

printf("elements in diagonal traversal \n");
int k;
for(k =m;k>=0;k--)
{
i=k;
j=0;
while(i<=m)
{
printf("%d ",arr[i][j]);
i++;
j++;
}
printf("\n");
}

for(k=1;k<=m;k++)
{
i=0;
j=k;
while(j<=m)
{
printf("%d ",arr[i][j]);
i++;
j++;}
printf("\n");

}

}

output:
1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64
elements in diagonal traversal
57
49 58
41 50 59
33 42 51 60
25 34 43 52 61
17 26 35 44 53 62
9 18 27 36 45 54 63
1 10 19 28 37 46 55 64
2 11 20 29 38 47 56
3 12 21 30 39 48
4 13 22 31 40
5 14 23 32
6 15 24
7 16
8

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