0% found this document useful (0 votes)
42 views7 pages

Searching and Sorting

The document describes three different sorting algorithms: linear search, bubble sort, and selection sort. It provides code examples and step-by-step explanations of how each algorithm works on sample input data. For linear search, it searches an array for a target value. Bubble sort iterates through the array, swapping adjacent elements that are out of order. Selection sort finds the minimum element on each pass and swaps it into the correct position.

Uploaded by

Abhinaypal Bolli
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)
42 views7 pages

Searching and Sorting

The document describes three different sorting algorithms: linear search, bubble sort, and selection sort. It provides code examples and step-by-step explanations of how each algorithm works on sample input data. For linear search, it searches an array for a target value. Bubble sort iterates through the array, swapping adjacent elements that are out of order. Selection sort finds the minimum element on each pass and swaps it into the correct position.

Uploaded by

Abhinaypal Bolli
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/ 7

Linear Search : Searching for an element in an array :

void main ( )
{
int i , n , x , a [8], flag = 0 ;
clrscr ( );
printf (“ Enter the number of values in the array \n“ ) ;
scanf ( “ % d” , &n) ;
printf ( “ Enter %d of values into the array \n“ , n) ;
for ( i = 0 ; i < n ; i + + )
scanf ( “ %d “ , & a [ i ] ) ;
printf ( “ The values in the array are \n“ ) ;
for ( i = 0 ; i < n ; i + + )
printf ( “ %2d \t“ , a [ i ] ) ;
printf ( “ Enter the value to be searched in the array \n “ ) ;
scanf ( “ % d” , & x ) ;
for (i = 0 ; i < n ; i + + )
If ( a [ i ] = = x )
{
flag = 1 ;
break ;

if ( flag = = 1 )
printf (“ Number Found “ );

else

printf (“ Element not found”);

getch ( ) ;

OUTPUT:

Enter the number of values in the array


5
Enter 5 of values into the array
The values in the array are
21 11 36 72 9
Enter the value to be searched in the array
11
Number Found
BUBBLE SORT:
void main ( )
{
int i , j, t, n , a [10] = { 0 } ;
clrscr ( );
printf (“ Enter the number of values in the array \n“ ) ;
scanf ( “ % d” , &n) ;
printf ( “ Enter %d of values into the array \n“ , n) ;
for ( i = 0 ; i < n ; i + + )
scanf ( “ %d “ , & a [ i ] ) ;
printf ( “ The values in the array are \n“ ) ;
for ( i = 0 ; i < n ; i + + )
printf ( “ %2d \t“ , a [ i ] ) ;
printf ( “ Bubble sorting the array \n “ ) ;
for (i = 0 ; i < n ; i + + )
{
for (j = 0 ; j < n - 1; j + + )
{
if ( a [ j ] > a [j+1] )
{
t=a[j];
a[j]=a[j+1];
a[j+1] =t;
}

}
printf ( “ the sorted array \n“ , n) ;
for ( i = 0 ; i < n ; i + + )
printf ( “ %2d \t“ , a [ i ] ) ;
getch ( ) ;

Example : input = 5 10 21 62 37 9 2 59

The sorting status for each value of i and j will be as follows: n = 8

The elements for comparison in jth loop are marked BOLD, the swapping of the elements is
indicated in RED
n i = 0 - First Pass ( Iteration )
1 j=0 10 5 21 62 37 9 2 59
2 j=1 5 10 21 62 37 9 2 59
3 j=2 5 10 21 62 37 9 2 59
4 j=3 5 10 21 37 62 9 2 59
5 j=4 5 10 21 37 9 62 2 59
6 j=5 5 10 21 37 9 2 62 59
7 j=6 5 10 21 37 9 2 59 62
i = 1 - Second Pass ( Iteration )
1 j=0 5 10 21 37 9 2 59 62
2 j=1 5 10 21 37 9 2 59 62
3 j=2 5 10 21 37 9 2 59 62
4 j=3 5 10 21 9 37 2 59 62
5 j=4 5 10 21 9 2 37 59 62
6 j=5 5 10 21 9 2 37 59 62
7 j=6 5 10 21 9 2 37 59 62
i = 2 - Third Pass ( Iteration )
1 j=0 5 10 21 9 2 37 59 62
2 j=1 5 10 21 9 2 37 59 62
3 j=2 5 10 9 21 2 37 59 62
4 j=3 5 10 9 2 21 37 59 62
5 j=4 5 10 9 2 21 37 59 62
6 j=5 5 10 9 2 21 37 59 62
7 j=6 5 10 9 2 21 37 59 62
i = 3 - Fourth Pass ( Iteration )
1 j=0 5 10 9 2 21 37 59 62
2 j=1 5 9 10 2 21 37 59 62
3 j=2 5 9 2 10 21 37 59 62
4 j=3 5 9 2 10 21 37 59 62
5 j=4 5 9 2 10 21 37 59 62
6 j=5 5 9 2 10 21 37 59 62
7 j=6 5 9 2 10 21 37 59 62
i = 4 - Fifth Pass ( Iteration )
1 j=0 5 9 2 10 21 37 59 62
2 j=1 5 2 9 10 21 37 59 62
3 j=2 5 2 9 10 21 37 59 62
4 j=3 5 2 9 10 21 37 59 62
5 j=4 5 2 9 10 21 37 59 62
6 j=5 5 2 9 10 21 37 59 62
7 j=6 5 2 9 10 21 37 59 62
i = 5 - Sixth Pass ( Iteration )
1 j=0 2 5 9 10 21 37 59 62
2 j=1 2 5 9 10 21 37 59 62
3 j=2 2 5 9 10 21 37 59 62
4 j=3 2 5 9 10 21 37 59 62
5 j=4 2 5 9 10 21 37 59 62
6 j=5 2 5 9 10 21 37 59 62
7 j=6 2 5 9 10 21 37 59 62
i = 6 - Seventh Pass ( Iteration )
1 j=0 2 5 9 10 21 37 59 62
2 j=1 2 5 9 10 21 37 59 62
3 j=2 2 5 9 10 21 37 59 62
4 j=3 2 5 9 10 21 37 59 62
5 j=4 2 5 9 10 21 37 59 62
6 j=5 2 5 9 10 21 37 59 62
7 j=6 2 5 9 10 21 37 59 62

The i loop terminates and the array is in the sorted order.

To also print the change of elements during each pass, the above program can be extended as
follows: (The changes are marked in RED in the program)

BUBBLE SORT:
void main ( )
{
int i , j, t, n , a [10] = { 0 } , k;
clrscr ( );
printf (“ Enter the number of values in the array \n“ ) ;
scanf ( “ % d” , &n) ;
printf ( “ Enter %d of values into the array \n“ , n) ;
for ( i = 0 ; i < n ; i + + )
scanf ( “ %d “ , & a [ i ] ) ;
printf ( “ The values in the array are \n“ ) ;
for ( i = 0 ; i < n ; i + + )
printf ( “ %2d \t“ , a [ i ] ) ;
printf ( “ Bubble sorting the array \n “ ) ;
for (i = 0 ; i < n ; i + + )
{
for (j = 0 ; j < n - 1; j + + )
{
if ( a [ j ] > a [j+1] )
{
t=a[j];
a[j]=a[j+1];
a[j+1] =t;
}

printf ( “ \n elements after %d pass is “ , i + 1 );

for ( k = 0; k < n ; k + + )

printf ( “ %3d” , a [ k ] );

}
printf ( “ the sorted array is \n“ , n) ;
for ( i = 0 ; i < n ; i + + )
printf ( “ %2d \t“ , a [ i ] ) ;
getch ( ) ;

SELECTION SORT:
This procedure selects the minimum first and then compares if true, if not true the values are
swapped. Since the process starts with a selection, it’s called a SELECTION SORT
void main ( )
{
int i , j, t, n , loc , min, a [10] = { 0 } ;
clrscr ( );
printf (“ Enter the number of values in the array \n“ ) ;
scanf ( “ % d” , &n) ;
printf ( “ Enter %d of values into the array \n“ , n) ;
for ( i = 0 ; i < n ; i + + )
scanf ( “ %d “ , & a [ i ] ) ;
printf ( “ The values in the array are \n“ ) ;
for ( i = 0 ; i < n ; i + + )
printf ( “ %2d \t“ , a [ i ] ) ;
printf ( “ Selection sort \n “ ) ;
for (i = 0 ; i < n ; i + + )
{
min = a [ i ] ;
loc = i ;
for (j = i + 1 ; j < n ; j + + )
{
if ( min > a [ j ] )
{
min = a [ j ] ;
loc = j ;
} // closure of IF
} // closure of jth loop
t=a[i];
a [ i ] = a [ loc ] ;
a [ loc ] = t ;
} // closure of ith loop

printf ( “ the sorted array \n“ , n) ;


for ( i = 0 ; i < n ; i + + )
printf ( “ %2d \t“ , a [ i ] ) ;
getch ( ) ;

Elements in the array are : 5 10 21 62 37 9 2 59

i = 0 - min = 5, loc = 0
INDEX 0 1 2 3 4 5 6 7 min loc
j=1 5 10 21 62 37 9 2 59 5 0
j=2 5 10 21 62 37 9 2 59 5 0
j=3 5 10 21 62 37 9 2 59 5 0
j=4 5 10 21 62 37 9 2 59 5 0
j=5 5 10 21 62 37 9 2 59 5 0
j=6 5 10 21 62 37 9 2 59 2 6
j=7 5 10 21 62 37 9 2 59 2 6
2 10 21 62 37 9 5 59 st
= Array after 1 pass
i = 1 - min = 10, loc = 1
j=2 2 10 21 62 37 9 5 59 10 1
j=3 2 10 21 62 37 9 5 59 10 1
j=4 2 10 21 62 37 9 5 59 10 1
j=5 2 10 21 62 37 9 5 59 9 5
j=6 2 10 21 62 37 9 5 59 5 6
j=7 2 10 21 62 37 9 5 59 5 6
2 5 21 62 37 9 10 59 nd
= Array after 2 pass
i = 2 - min = 21 loc = 2
j=3 2 5 21 62 37 9 10 59 21 2
j=4 2 5 21 62 37 9 10 59 21 2
j=5 2 5 21 62 37 9 10 59 9 5
j=6 2 5 21 62 37 9 10 59 9 5
j=7 2 5 21 62 37 9 10 59 9 5
2 5 9 62 37 21 10 59 rd
= Array after 3 pass
i = 3 - min = 62 loc = 3
j=4 2 5 9 62 37 21 10 59 37 4
j=5 2 5 9 62 37 21 10 59 21 5
j=6 2 5 9 62 37 21 10 59 10 6
j=7 2 5 9 62 37 21 10 59 10 6
2 5 9 10 37 21 62 59 th
= Array after 4 pass
i = 4 - min = 37 loc = 4
j=5 2 5 9 10 37 21 62 59 21 5
j=6 2 5 9 10 37 21 62 59 21 5
j=7 2 5 9 10 37 21 62 59 21 5
2 5 9 10 21 37 62 59 th
= Array after 5 pass
i = 5 - min = 37 loc = 5
j=6 2 5 9 10 21 37 62 59 37 5
j=7 2 5 9 10 21 37 62 59 37 5
2 5 9 10 21 37 62 59 = Array after 6th pass
i = 6 - min = 62 loc = 6
j=7 2 5 9 10 21 37 62 59 59 7
2 5 9 10 21 37 59 62 = final sorted Array

The changes in the array for each value of i can be printed, similar to the one in the bubble sort

You might also like