You are on page 1of 7

Sorting,Searching,String

Sorting :
Sorting is the process of arranging element either in ascending/descending order.
Selection sort :
Find the smallest element in the list &swap it with element present at the head of the
list.
Eg : 0 1 2 3 4

2 6 5 3 1

Cmp 1st & 2nd element. To compare consider 1st element as a[i] and next element as a[j].
0 1 2 3 4

2 6 5 3 1
A[i] A[j]

Check condition if A[i]<A[j] Do nothing


Else swap the element.
0 1 2 3 4

2 6 5 3 1

To swap we need temporary variable.


A[i] A[j] temp
2 1
temp=a[i]
a[i]=a[j]
a[j]=temp
Result  A[i] A[j]
1 2
1 6 5 3 2

Next compare 6 & 5:


1 5 6 3 2
Compare 5 & 3 :
1 3 6 5 2
Compare 3 &2 :
1 2 6 5 3

1 2 5 6 3

1 2 3 6 5

1 2 3 5 6

Program :
#include<stdio.h>
#include<conio.h>
void main()
{
int n, A[10],i.j ,temp;
clrscr():
printf(“Enter total no.of elements”);
scanf(“%d”,&n);
printf(“enter array elements”);
for(i=0;i<n;i++)
scanf(“%d”,&A[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(A[i]>A[j])
{
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
}
printf(“the sorted array is”);
for(i=0;i<n;i++)
printf(“%d\t”,A[i]);
getch();
}
Linear search :
0 1 2 3 4 5 6 7

3 2 18 33 21 5 99 42

Key=33
Compare key value with all value in the array.
Once key found it print “key found”.
If key not found search till end of the array.
Eg:
#include<stdio.h>
void main()
{
int n,a[20],key,i,found=0;
printf("enter the number");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
printf("enter the key element to be searched");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(key==A[i])
{
printf("key found");
found=1;
break;
}
}
if(found==0)
printf("key not found");
getch();
}
Binary search :
 Array should be arranged in ascending order
i/p array =1,3,4,6,8,9,11 key=4
it compare key element with middle element ,if key=mid(search found)
if not match it check whether it is greater than or less than.
if it key element is less than mid element search left side otherwise search right side.
Then take mid element in left side ,then again check with key element.
Same process repeated.
Program :
#include<stdio.h>
void main()
{
int first,last,middle;
int n,A[20],key,i,found=0;
printf("enter the value of n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
printf("enter the key element to be searched");
scanf("%d",&key);
first=0;
last=n-1;
middle=(first+last)/2;
while(first<=last)
{
if(key==A[middle])
{
printf("key found");
break;
}
else if(key<A[middle])
last=middle-1;
else
first=middle+1;
}
if(first>last)
printf("key not found");
getch();
}
Strings in C :
Array of character.
Syntax :
Datatype arrayname [size]
char stringname[size]
eg: char name[10];
string always ended with null character.
Name=200

0 1 2

3 4 5 6 7 8 9

200 1 2 3 4 5 6 7 8 9

The character will be store as ASCII values.


Initialization :
1) char name[10]=”jenny”;
0 1 2 3 4 5 6 7 8 9

j e n n y \0

2) char name[10]={‘j’,’e’,’n’,’n’,’y’,’\0’}
3) char name[ ]={‘c’,’s’,’e’,’\0’}
space allowed and special symbol.
for string %s format specifier.
Program : o/p :
#include<stdio.h> Enter name :
void main() CSE
{
char name[10];
printf(“Enter name:”):
scanf(“%s”,name);
printf(“%s”, name);
getch(); }
If we enter “CSE department” output will be CSE only, because the scanf function
doesn’t ready white space.
scanf consider space as end of string & automatically put ‘\0’.
To read full string we can use “gets”.
Eg:
char name[30]; o/p:
printf(“enter name “); Enter name :
gets(name ); CSE dept
printf(“%s”,name ); CSE dept
Drawback :
Buffer overflow
Eg: char name [5]; o/p: CSE dept
The o/p will be “CSE dept “-it will try to print all character ,it over write the m/y.

You might also like