#include<stdio.h>
#include<conio.h>
int main(){
int limit,temp,x,y,a[20];
printf("Enter # elements: ");
scanf("%d",&limit);
printf("Enter %d numbers: ",limit);
for(x=0;x<limit;x++)
scanf("%d",&a[x]);
//Sorting Using BUBBLE SORT ALGORITH
for(x=limit-2;x>=0;x--){
for(y=0;y<=x;y++){
if(a[y]>a[y+1]){
temp=a[y];
a[y]=a[y+1];
a[y+1]=temp;
}
}
}
printf("After sorting: ");
for(x=0;x<limit;x++)
printf(" %d",a[x]);
getch();
}
QUICKSORT
#include<stdio.h>
#include<conio.h>
void quicksort(int [10],int,int);
int main(){
int x[20],size,y;
printf("Enter array size: ");
scanf("%d",&size);
printf("Enter %d numbers: ",size);
for(y=0;y<size;y++)
scanf("%d",&x[y]);
quicksort(x,0,size-1);
printf("Sorted numbers: ");
for(y=0;y<size;y++)
printf(" %d",x[y]);
getch();
}
void quicksort(int x[10],int first,int last){
int pivot,y,temp,z;
if(first<last){
pivot=first;
z=first;
y=last;
while(z<y){
while(x[z]<=x[pivot]&&z<last)
z++;
while(x[y]>x[pivot])
y--;
if(z<y){
temp=x[z];
x[z]=x[y];
x[y]=temp;
}
}
temp=x[pivot];
x[pivot]=x[y];
x[y]=temp;
quicksort(x,first,y-1);
quicksort(x,y+1,last);
}
}
ALTERNATE SORT
#include<stdio.h>
#include<conio.h>
main()
{
int x,y,temp,nums[10];
printf("Enter 10 nos.: ");
for(x=0;x<10;x++)
scanf("%d", &nums[x]);
printf("UNSORTED: ");
for(x=0;x<10;x++)
printf(" %d ", nums[x]);
for(x=0;x<10;x++)
{for(y=0;y<10;y++)
if(nums[x]<nums[y])
{
temp=nums[x];
nums[x]=nums[y];
nums[y]=temp;
}
}
printf("\nSORTED ASCENDING: ");
for(x=0;x<10;x++)
printf(" %d ", nums[x]);
getch();
}
SELECTION SORT
#include<stdio.h>
#include<conio.h>
main()
{
int i, b=5,d=0,j,temp,a=5,c,num[5],sorted[5];
printf("Enter 5 #s: ");
for(i=0;i<5;i++)
scanf("%d", &num[i]);
for(i=0;i<5;i++)
sorted[i]=num[i];
for (i=0 ; i<b-1 ; i++)
{
c=d;
for (j=0 ; j<a-1 ; j++)
{
if ( sorted[i]>sorted[c+1] )
{
temp=sorted[i];
sorted[i]=sorted[c+1];
sorted[c+1]=temp;
}
c++;
}
a--;
d++;
}
printf("\nUnsorted: ");
for(i=0;i<5;i++)
printf("%d\t", num[i]);
printf("\nSorted: ");
for(i=0;i<5;i++)
printf("%d\t", sorted[i]);
getch();
}
INSERTION SORT
#include<stdio.h>
#include<conio.h>
main()
{
int i, j,tmp,num[5];
printf("Enter 5 #s: ");
for(i=0;i<5;i++)
scanf("%d", &num[i]);
for (i = 1; i < 5; i++)
{
j = i;
while (j > 0 && num[j - 1] > num[j])
{
tmp = num[j];
num[j] = num[j - 1];
num[j - 1] = tmp;
j--;
}
}
printf("\nSorted: ");
for(i=0;i<5;i++)
printf("%d\t", num[i]);
getch();
}
PASSWORD MASKING
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
int main()
{
int ch;
char pword[BUFSIZ];
int i = 0;
puts ("Enter your password");
fflush(stdout);
while ((ch = getch()) != EOF
&& ch != '\n'
&& ch != '\r'
&& i < sizeof(pword) - 1)
{
if (ch == '\b' && i > 0)
{
printf("\b \b");
fflush(stdout);
i--;
pword[i] = '\0';
}
else if (isalnum(ch))
{
putchar('*');
pword[i++] = (char)ch;
}
}
pword[i] = '\0';
printf ("\nYou entered >%s<", pword);
getche();
return 0;
}
//BINARY SEARCH IMPLEMENTED [string pointer array]
#include <stdio.h>
#include <string.h>
#include <conio.h>
static int binarysearch(char *str[], int max, char *value);
int main(void) {
//NOTE: ITEMS SHOULD BE SORTED FIRST FOR BINARY SEARCH
char *names[] = { "ana", "bob", "cathy", "denny", "earl" };
int x, arraysize, result;
x = arraysize = result = 0;
arraysize = sizeof(names) / sizeof(names[0]);
for(x = 0; x < arraysize; x++)
printf("%d: %s\n", x, names[x]);
printf("\n");
if((result = binarysearch(names, arraysize, "denny")) != 0)
printf("FOUND denny @ position: %d\n", result);
else
printf("NO RECORD FOUND FOR denny..\n");
if((result = binarysearch(names, arraysize, "nick")) != 0)
printf("FOUND carl @ position: %d\n", result);
else
printf("NO RECORD FOUND FOR carl..\n");
getch();
}
static int binarysearch(char *str[], int max, char *value) {
int position;
int begin = 0;
int end = max - 1;
int cond = 0;
while(begin <= end) {
position = (begin + end) / 2;
if((cond = strcmp(str[position], value)) == 0)
return position;
else if(cond < 0)
begin = position + 1;
else
end = position - 1;
}
return 0;
}
5 comments:
thanks dude. it really helped me.
can you post the binary search algo. thanks alot.
BINARY SEARCH ALGO posted as requested by Zypher....:)
Thanks dude..
how can i sort a multi-dimensional array with selection sort???
help mo pls :(
Post a Comment