Searching and Sorting
char ch;
clrscr( );
flg=0;
printf("Enter the size of the array \n");
scanf("%d",&n);
printf("Enter the elements of the array \n");
for (i=1; i<=n;i++)
{
scanf("%d", &a[i]);
}
do
{
printf("\nEnter the number which you want to search :");
scanf("%d", &m);
printf ("\nPress 'l' for linear search and 'b' for binary search :");
ch=getch( );
switch(ch)
{
case 'l':
for (i=1; i<=n;i++)
{
if(a[i]==m)
{
flg=1;
break;
}
}
if(flg== 1)
{
printf("\nThe element is found and the position is: %d",i);
printf("\n");
}
else
printf("\nElement is not found \n");
break;
case 'b':
for (i=1; i<=n-1;i++)
{
for (j=i+1;j<=n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nThe sorted list is \n");
for (i=1; i<=n;i++)
{
printf("%d ",a[i]);
}
low=1;
high=n;
while (low<=high)
{
middle=(low+high)/2;
if(a[middle] == m)
{
flg = 1;
break;
}
else
{
if(a[middle]<m)
{
low=middle+1;
}
else
{
high=middle-1;
}
}
}
if(flg == 1)
printf("\nThe element is found and the position is : %d", middle);
else
printf("\nThe element is not found \n");
break;
default:
printf("\nWrong selection \n");
break;
}
flushall();
printf("\nPress 'Y' to continue and any other key to discontinue : ");
ch=getch( );
}while (ch == 'Y');
getch( );
}
#include <stdio.h>
#include <conio.h>
void main( )
{
int i, j, temp, n, num, ar[100];
char ch;
clrscr( );
do
{
printf ("\nEnter the size of the array \n");
scanf ("%d", &n);
printf("Enter the elements of the array \n");
for (i=1; i<=n;i++)
{
scanf("%d", &ar[i]);
}
printf(" Select the option \n");
printf("1. Ascending order: \n");
printf("2. Descending order: \n");
scanf("%d",&num);
switch(num)
{
case 1:
for (i=1; i<=n-1;i++)
{
for (j=1; j<=n-i;j++)
{
if(ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
}
printf("Sorted array in ascending order is \n");
for (i=1; i<=n;i++)
{
printf("%d ",ar[i]);
}
break;
case 2:
for (i=1; i<=n-1;i++)
{
for (j=1; j<=n-i;j++)
{
if(ar[j]<ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
}
printf("\nSorted array in descending order is \n");
for (i=1; i<=n;i++)
{
printf("%d ",ar[i]);
}
break;
default:
printf("Wrong selection \n");
break;
}
printf("\n Press 'Y' to continue and any other key to terminate : ");
flushall( );
ch=getch( );
}while (ch == 'Y');
getch( );
}
Output:
Handling of Character Strings << Previous
Next >> User Defined Functions
Our aim is to provide information to the knowledge seekers.