Searching and sorting in array
Searching and sorting in array
Searching in array.
It is a process in which we search particular element in array.
example:
program for searching a element in array.
10
|
20
|
30
|
40
|
50
|
0 1 2 3 4
In above example we can find any element of array.
now i am going to write code----
#include <stdio.h>
#include <stdlib.h>
void search( int a[],int size, int element)
{
int i,flag=0;
for(i=0;i<size;i++)
{
if(a[i]==element)
{
flag=1;
break;
}
}
if(flag==1)
printf("serch is found....\nserching number is=%d\n",element);
else
printf("serching is not found.......");
}
void main()
{
int a[20],element,size,i;
printf("enter size of list=");
scanf("%d",&size);
printf("enter value of list=\n");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
printf("enter number for serching=");
scanf("%d",&element);
search(a,size,element) ;
}
output
Sorting in array
It is a process to sort element in particular order such as assending and descending order.
example:
10
|
30 | 50 |
40
|
20 |
0 1 2 3 4
10
|
20
|
30
|
40
|
50
|
0 1 2 3 4
50 |
40
|
30
|
20
|
10
|
0 1 2 3 4
#include<stdio.h>
#include<conio.h>
/*function of sort record in ascending order*/
void shorting(int a[],int size)
{
int i,j,temp;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("shorted record in ascending order:-\n");
for(i=0;i<size;i++)
{
printf("%d\t",a[i]);
}
}
/*function of sort record in descending order*/
void shortingd(int a[],int size)
{
int i,j,temp;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n shorted record in descending order:-\n");
for(i=0;i<size;i++)
{
printf("%d\t",a[i]);
}
}
void main()
{
int a[50],size,i;
printf("enter size of list=");
scanf("%d",&size);
printf("enter value of list=\n");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
shorting(a,size);
shortingd(a,size);
}
I hope this post is useful for you......
plz follow my blog..
thank u...............
you can also follw my facebook page.(let's_code)
link is give below....
Let's_code
Comments
Post a Comment