[go: up one dir, main page]

0% found this document useful (0 votes)
16 views2 pages

Sorting Searching

Programming in c, searching and sort programs

Uploaded by

LOKESH .Y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Sorting Searching

Programming in c, searching and sort programs

Uploaded by

LOKESH .Y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

BUBBLE SORT INSERTION SORT SELECTION SORT

#include<stdio.h> #include<stdio.h> #include<stdio.h>


int main() int main() int main()
{ { {
int n, A[20], i, j, temp; int n, A[10], i, j, temp=0; int n, A[20], i, j, temp, min;
printf("Enter total no.of elements"); printf("Enter total no.of elements"); printf("Enter total no.of elements");
scanf("%d", &n); scanf("%d", &n); scanf("%d", &n);
printf("Enter array elements"); printf("Enter array elements"); printf("Enter array elements");
for(i=0; i<n; i++) for(i=0; i<n; i++) for(i=0; i<n; i++)
scanf("%d", &A[i]); scanf("%d", &A[i]); scanf("%d", &A[i]);
for(i=0; i<n; i++) for(i=1; i<=n-1; i++) for(i=0; i<n-1; i++)
{ { {
for(j=i+1; j<n; j++) for(j=i; j>0 && A[j-1]>A[j]; j--) min=i;
{ { for(j=i+1; j<n; j++)
if(A[i] > A[j]) temp=A[j]; {
{ A[j]=A[j-1]; if(A[min] > A[j])
temp=A[i]; A[j-1]=temp; min=j;
A[i]=A[j]; } if(min!=i)
A[j]=temp; } {
} printf("The sorted array is"); temp=A[i];
} for(i=0; i<n; i++) A[i]=A[min];
} printf("%d", A[i]); A[min]=temp;
printf("The sorted array is"); } }
for(i=0; i<n; i++) }
printf("%d", A[i]); }
} printf("The sorted array is");
for(i=0; i<n; i++)
printf("%d", A[i]);
}
LINEAR SEARCH BINARY SEARCH
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int n, A[20], i, key, found=0; int first, last, middle;
printf("Enter total no.of elements"); int n, A[20], i, key, found=0;
scanf("%d", &n); printf("Enter total no.of elements");
printf("Enter array elements"); scanf("%d", &n);
for(i=0; i<n; i++) printf("Enter array elements");
scanf("%d", &A[i]); for(i=0; i<n; i++)
printf("Enter key element to be scanf("%d", &A[i]);
searched"); printf("Enter key element to be
scanf("%d", &key); searched");
for(i=0; i<n; i++) scanf("%d", &key);
{ first=0;
if(key==A[i]) last=n-1;
{ middle=(first+last)/2;
printf("Key Found"); while(first<=last)
found=1; {
break; if(key==A[middle])
} {
} printf("key found");
if(found==0) break;
printf("Key not Found"); }
} else if(key<A[middle])
last=middle-1;
else
first=middle+1;
middle=(first+last)/2;
}
if(first>last)
printf("Key not found");
}

You might also like