Unit- 2
Unit- 2
Searching
• Searching is the process of locating given value position in a list of
values, i.e. search is a process of finding a value in a list of values.
• Search is said to be successful if the element being searched is found
or unsuccessful when the element being searched is not found.
• A search typically answers in either True or False as to whether the
item is present.
Searching Techniques
• Two techniques are used for searching:
1) Linear/ Sequential Search.
2) Binary Search.
Sequential/Linear Search
• A linear search is the basic and simple search algorithm.
• In Linear search, a sequential search is made over all elements one by
one.
• (In other words linear search searches an element or value from an
array till the desired element is not found in sequential order. Every
element is checked and if it is found then that particular item is
returned, otherwise the search continues till the end of the data
collection).
• Searching begins from first element and continues until the desired
element is found or end of the file is reached.
int linear(int[],int,int);
void main()
{
int i,key,a[20],n,loc=-1;
clrscr();
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the search element\n");
scanf("%d",&key);
loc=linear(a,n,key);
if(loc>=0)
printf("Element found at %d
position\n",loc+1);
/*Linear search recursive
else function*/
printf("Element not found\n");int linear(int a[10],int n,int
search)
getch();
{
} if(n<=0)
return(-1);
else if(a[n-1]==search)
return(n-1);
else
return(linear(a,n-
1,search));
Binary Search Algorithm
Number of comparisons made to search for an Number of comparisons made to search for an
element is more. element is less.
This method does not require list to be sorted. This method can work only on sorted list.
A linear search scans one item at a time A binary search cuts down the search to half as
How It Works
without skipping to any item. soon as the middle of a sorted list is found.
Linear searches may be implemented on any Binary searches can only be implemented on
Implementation linear container (vector, Single Linked list, data structures where two-way traversal is
double linked list). possible.
Dimensional array It can be implemented on both a single and It can be implemented only on a
multidimensional array. multidimensional array.
• example : Merge-sort
Stable and Not Stable Sorting
• If a sorting algorithm, after sorting the contents, does
not change the sequence of similar content in which
they appear, it is called stable sorting.
•
If a sorting algorithm, after sorting the contents,
changes the sequence of similar content in which they
appear, it is called unstable sorting.
Adaptive and Non-Adaptive Sorting Algorithm
• We know then that 10 is smaller 35. Hence they are not sorted. We
swap these values. We find that we have reached the end of the
array. After one iteration, the array should look like this −
• we are now showing how an array should look like after
each iteration. After the second iteration, it should look
like this −
• after each iteration, at least one value moves at the
end.
• And when there's no swap required, bubble sort learns
that an array is completely sorted.
Bubble Sort- Algorithm
Assume list is an array of n elements. The swap function
swaps the values of the given array elements.
begin BubbleSort(list)
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
end BubbleSort
• Complexity Analysis of Bubble Sort:
• Time Complexity: O(n2)
• Ms. Shiny T L
• Assistant Professor of Physical Sciences
• Kristu Jayanti College, Autonomous
SELECTION SORT