Documente Academic
Documente Profesional
Documente Cultură
ers.com
Engaging Peers, Inspiring Careers!
Searching techniques
Searching :
It is a process to find whether a particular value with specified properties is present or not
among a collection of items.
If the value is present in the collection, then searching is said to be successful, and it
returns the location of the value in the array.
Otherwise, if the value is not present in the array, the searching process displays the
appropriate message and in this case searching is said to be unsuccessful.
1) Linear or Sequential Searching
2) Binary Searching
int main( ) {
int arr [ 50 ] , num , i , n , pos = -1;
printf ("How many elements to sort : ");
scanf ("%d", &n);
printf ("\n Enter the elements : \n\n");
for( i = 0; i < n; i++ ) {
printf (arr [%d ] : , i );
scanf( "%d", &arr[ i ] );
}
printf(\nEnter the number to be searched : );
scanf(%d,&num);
for(i=0;i<n;i++)
if( arr [ i ] == num ) {
pos = i ; break;
}
if ( pos == -1 ) printf( %d does not exist ,num);
else
printf( %d is found at location : %d, num , pos);
Binary Searching
Algorithm:
Before searching, the list of items should be sorted in ascending order.
We first compare the key value with the item in the position of the array. If there is a match, we
can return immediately the position.
if the value is less than the element in middle location of the array, the required value is lie in
the lower half of the array.
if the value is greater than the element in middle location of the array, the required value is lie
in the upper half of the array.
We repeat the above procedure on the lower half or upper half of the array.
Binary_Search (A [ ], U_bound, VAL)
Step 1 : set BEG = 0 , END = U_bound , POS = -1
Step 2 : Repeat while (BEG <= END )
Step 3 :
set MID = ( BEG + END ) / 2
Step 4 :
if A [ MID ] == VAL then
POS = MID
print VAL is available at , POS
GoTo Step 6
End if
if A [ MID ] > VAL then
set END = MID 1
Else
set BEG = MID + 1
End if
End while
Step 5 : if POS = -1 then
print VAL is not present
End if
Step 6 : EXIT
Sorting
Sorting is a technique to rearrange the elements of a list in ascending or
descending order, which can be numerical, lexicographical, or any user-defined order.
Ranking of students is the process of sorting in descending order.
EMCET Ranking is an example for sorting with user-defined order.
EMCET Ranking is done with the following priorities.
i) First priority is marks obtained in EMCET.
ii) If marks are same, the ranking will be done with comparing marks obtained in
the Mathematics subject.
iii) If marks in Mathematics subject are also same, then the date of births will be
compared.
Internal Sorting :
If all the data that is to be sorted can be accommodated
at a time in memory is called internal sorting.
External Sorting :
It is applied to Huge amount of data that cannot be
accommodated in memory all at a time. So data in disk
or file is loaded into memory part by part. Each part that
is loaded is sorted separately, and stored in an
intermediate file and all parts are merged into one single
sorted list.
Bubble Sort
Insertion Sort
Selection Sort
Quick Sort
Merge Sort
Bubble Sort
Bubbles up the highest
Sorted
Unsorted
10
54
54
54
54
54
47
10
47
47
47
47
12
47
10
23
23
23
54
12
23
10
19
19
19
23
12
19
10
12
23
19
19
12
12
10
After
Pass 2
After
Pass 3
After
Pass 4
After
Pass 5
Original After
List
Pass 1
Bubble_Sort ( A [ ] , N )
Step 1 : Repeat For P = 1 to N 1
Begin
Step 2 :
Repeat For J = 1 to N P
Begin
Step 3 :
If ( A [ J ] < A [ J 1 ] )
Swap ( A [ J ] , A [ J 1 ] )
End For
End For
Step 4 : Exit
Complexity of Bubble_Sort
The complexity of sorting algorithm is
depends upon the number of comparisons
that are made.
Total comparisons in Bubble sort is
n ( n 1) / 2 n 2 n
Complexity = O ( n 2 )
Bubble Sort
For pass = 1 to N - 1
For J = 1 to N - pass
A[ J 1 ] > A[ J ]
Return
Temp = A [ J 1 ]
A[ J 1 ] = A[ J ]
A [ J ] = Temp
Insertion Sort
TEMP
78
23 45
32
36
23
78
45
32
36
23
45
78
32
36
23
45
78
32
36
23
45
32
23
32
45
78
36
23
32
36
45
78
36
Insertion_Sort ( A [ ] , N )
Step 1 : Repeat For K = 1 to N 1
Begin
Step 2 :
Set Temp = A [ K ]
Step 3 :
Set J = K 1
Step 4 :
Repeat while Temp < A [ J ] AND J >= 0
Begin
Set A [ J + 1 ] = A [ J ]
Set J = J - 1
End While
Step 5 :
Set A [ J + 1 ] = Temp
End For
Step 4 : Exit
insertion_sort ( int A[ ] , int n ) {
int k , j , temp ;
for ( k = 1 ; k < n ; k++ ) {
temp = A [ k ] ;
j = k - 1;
while ( ( temp < A [ j ] ) && ( j >= 0 ) ) {
A[ j + 1 ] = A[ j ] ;
j--;
}
A [ j + 1 ] = temp ;
}
}
Smallest
23
78 45
32
56
23
78
45
23
32
56
32
23
45
78
32
56
45
23
32
78
45
56
56
23
32
45
78
56
23
32
45
56
78
Selection_Sort ( A [ ] , N )
Step 1 : Repeat For K = 0 to N 2
Begin
Step 2 :
Set POS = K
Step 3 :
Repeat for J = K + 1 to N 1
Begin
If A[ J ] < A [ POS ]
Set POS = J
End For
Step 5 :
Swap A [ K ] with A [ POS ]
End For
Step 6 : Exit
selection_sort ( int A[ ] , int n ) {
int k , j , pos , temp ;
for ( k = 0 ; k < n - 1 ; k++ ) {
pos = k ;
for ( j = k + 1 ; j <= n ; j ++ ) {
if ( A [ j ] < A [ pos ] )
pos = j ;
}
temp = A [ k ] ;
A [ k ] = A [ pos ] ;
A [ pos ] = temp ;
}
}
Insertion sort
k = 1; k < n ; k++
temp = a [ k ]
j=k-1
Selection sort
k = 0; k < n - 1 ; k++
pos = k
j = k + 1 ; j < n ; j++
a [ j + 1 ] = temp
pos = j
temp = a[ k ]
a [ k ] = a [ pos ]
a [ pos ] = temp
return
return
11 5
14 0
20
11 5
14 0
20
Sub-list of
lesser elements
11 9
14 20
Sub-list of
greater elements
Complexity of Quick Sort
Apply Quick-sort
recursively
on sub-list
Apply Quick-sort
recursively
on sub-list
9
9
3
12
16
25 10
12
16
25 10
12
16
25 10
16
25 10 12
16
3
3
25 10 12
16 25 10 12
A
F
a [ loc ] = a [ left ]
right = loc - 1 ;
loc = left;
Flag == 0
loc == left
flag = 1
right = right - 1
return loc
F
loc == right
a [ loc ] = a [ right ]
left = loc + 1 ;
loc = right;
T
flag = 1
loc == left
39 9
39 9
39 9
39
81 45 90 27 72 18
81 45
90 27 72 18
81 45
81
45
90 27
90
27
72 18
72
18
39
39
9
81
45
45 81
39 45 81
9
90
27
27 90
72
18
18 72
18 27 72 90
18 27 39 45 72 81 90
merge
a[ i ] < a[ j ]
c[ k ] =a [ i ] ;
k++ ; i++
c[ k ] =a [ j ] ;
k++ ; j++
c[ k ] =a [ j ] ; k++ ; j++
i = low ; i < k ; i ++
a[ i ] = c [ i ]
return
Return
FaaDoOEngine
ers.com
Engaging Peers, Inspiring Careers!