Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

SEARCHING AND

SORTING TECHNIQUES
UNIT-2
Searching Algorithms are designed to check for an element or retrieve an element from any 
data structure where it is used. Based on the type of operations these algorithms are generally
classified into two categories: 
 Sequential Search: The Sequential Search is the basic and simple Searching Algorithm. Sequential
Search starts at the beginning of the list or array. It traversed the list or array sequentially and checks
for every element of the list or array. The Linear Search is an example of the Sequential Search. 
A Linear Search checks one by one each element of the array, without jumping to any item. It searches
the element in the array until a match is found. If the match is found then it returns the index of the
item otherwise it returns the -1. The worst-case complexity of the Linear Search Algorithm is O(N),
where N is the total number of elements in the list. How Linear Search works: 

Let’s understand with an example of how linear search works. Suppose, in this example, the task is to search an
element x in the array. For searching the given element, start from the leftmost element of the array and one by
one compare x with each element of the array. If the x matches with the an element it returns the index otherwise
it returns the -1. Below is the image to illustrate the same: 
5.1 Searching
Searching is an operation or a technique that helps finds the place of a given element or value in the list. Any search is
said to be successful or unsuccessful depending upon whether the element that is being searched is found or not. Some
of the standard searching technique that is being followed in the data structure is listed below:
1. Linear Search or Sequential Search
2. Binary Search
5.1.1 Linear Search
Linear search is a very basic and simple search algorithm. In Linear search, we search an element or value in a given
array by traversing the array from the starting, till the desired element or value is found. Linear Search is applied on
unsorted or unordered lists, when there are fewer elements in a list. It compares the element to be searched with all the
elements present in the array and when the element is matched successfully, it returns the index of the element in the
array, else it returns -1.
Example: Consider the following list of elements and the element to be searched.
Index 0 1 2 3 4 5 6
List 12 23 56 34 11 76 63
Search element = 11
Step 1: Search element (11) is compared with first element (12)
Index 0 1 2 3 4 5 6
List 12 23 56 34 11 76 63
11
Both are not matching. So move to next element.
Step 2: Search element (11) is compared with next element (23)
Index 0 1 2 3 4 5 6
List 12 23 56 34 11 76 63
11
Both are not matching. So move to next element.
Step 3: Search element (11) is compared with next element (56)
Index 0 1 2 3 4 5 6
List 12 23 56 34 11 76 63
11
Both are not matching. So move to next element.
Step 4: Search element (11) is compared with next element (34)
Index 0 1 2 3 4 5 6
List 12 23 56 34 11 76 63
11

Both are not matching. So move to next element.


Step 5: Search element (11) is compared with next element ( 11)
Index 0 1 2 3 4 5 6
List 12 23 56 34 11 76 63
11
Both are matching. So we stop comparing and display element found at index 4.
Linear search algorithm finds a given element in a list of elements with O(n) time complexity
where n is total number of elements in the list.
Interval Search: These algorithms are designed to searching for a given element in sorted data
structures. These types of searching algorithms are much more efficient than a Linear Search
Algorithm. The Binary Search is an example of the Interval Search. 
A Binary Search searches the given element in the array by dividing the array into two halves.
First, it finds the middle element of the array and then compares the given element with the
middle element of thearray, if the element to be searched is less than the item in the middle of
the array then the given element can only lie in the left subarray otherwise it lies in the right
subarray. It repeatedly checks until the element is found. The worst-case complexity of the
Binary Search Algorithm is O(log N), 
How Binary Search works: 
Let’s understand with an example of how Binary search works. Suppose, in this example we have to search a
element x in the array. For searching the given element, first we find the middle element of the array then
compare x with the middle element of the array. If x matches with the middle element we return the mid index
and if x is greater then the mid element then x can be only lie in the right half subarray after the mid element,
so we recur for the right half otherwise recur for the left half. It repeatedly checks until the element is found.
Below is the image to illustrate the same: 
 
NEED OF SORTING AND SEARCHING

 The arrangement of data in a preferred order is called sorting in the data structure. By sorting data, it is easier
to search through it quickly and easily.
 The simplest example of sorting is a dictionary. Before the era of the Internet, when you wanted to look up a
word in a dictionary, you would do so in alphabetical order. This made it easy.
 sorting greatly improves the efficiency of searching. If we were to open a phone book, and find that the names
were not presented in any logical order, it would take an incredibly long time to look up someone’s phone
number the same panic an engineer will go through if their data is not sorted and structured.
 Searching Algorithms are designed to check for an element or retrieve an element from any data
structure where it is stored.
GENERAL SORT CONCEPTS SORT ORDER

 Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to arrange data in a
particular order.

 Increasing Order
A sequence of values is said to be in increasing order, if the successive element is greater than the previous one.
For example, 1, 3, 4, 6, 8, 9

 Decreasing Order
A sequence of values is said to be in decreasing order, if the successive element is less than the current one.
For example, 9, 8, 6, 4, 3, 1
INTERNAL & EXTERNAL SORTING

There are two different categories in sorting:


 Internal sorting: If the input data is such that it can be adjusted in the main memory at once, it is called
internal sorting.
Bubble Sort.
Insertion Sort.
Quick Sort.
Heap Sort.
Radix Sort.
Selection sort.

 External sorting: If the input data is such that it cannot be adjusted in the memory entirely at once, it needs to
be stored in a hard disk, floppy disk, or any other storage device. This is called external sorting.
merge sort.
STABLE & NON STABLE SORTING

The stability of a sorting algorithm is concerned with how the algorithm treats equal (or repeated) elements.
Stable sorting algorithms preserve the relative order of equal elements, while unstable sorting algorithms don’t.
In other words, stable sorting maintains the position of two equals elements relative to one another.
Stable sorting maintains the order of the two equal balls numbered 20, whereas unstable sorting may invert the
relative order of the two 8s.

Stable sorting
So if we sort students
dataset of Student If we sort this data If on the other hand we
section wise too. But in
Names and their according to name only, used a stable sorting
doing so, if the sorting
respective class sections. then it is unsorted by algorithm, the result
algorithm is not stable, we
sections. would be-
may get
When equal elements are indistinguishable, such as with integers, or more generally, any data where the entire
element is the key, stability is not an issue.
Stability is also not an issue if all keys are different.

the sorting algorithm is not


WHEN STABILITY IS ISSUE? stable, the relative order is lost
for Eric and Alice
STABLE & NON STABLE SORT ALGORITHM

Stability is not a concern if:

equal elements are indistinguishable, or


all the elements in the collection are distinct

Algorithms are stable by nature: Bubble Sort, Insertion Sort, Merge Sort, Count Sort

Algorithms unstable by nature: Quick Sort, Heap Sort, and Selection Sort
A simple steps is to do linear search, i.e
1. Start from the leftmost element of a[] and one by one compare
m with each element of a[]
2. If m matches with an element, set flag=1 and return.
3. If m doesn’t match with any of elements, set flag=0.

LINEAR SEARCH
FUNCTION CODE FOR LINEAR SEARCH

void search(int a[],int n,int m) //n=total no of elements, m=no to search


{
int i,flag=0;
for(i=0;i<n;i++)
{
if(a[i]==m)
{
printf("Element found at position %d",i+1);
flag=1;
return;
}
}
if(flag==0)
{
printf("\nSorry! Element not found");
}
}
FUNCTION CODE FOR LINEAR SEARCH WITH MULTIPLE OCCURRENCE

void search(int a[],int n,int m)


{
int i,flag=0;
for(i=0;i<n;i++)
{
if(a[i]==m)
{
flag=1;
printf("Element found at position %d\n",i+1);
}
}
if(flag==0)
{
printf("Sorry! Element not found");
}
}
 The best-case complexity is O(1) if the element is found
in the first iteration of the loop.

 The worst-case time complexity is O(n), if the search


element is found at the end of the array, provided the
size of the array is n.

TIME COMPLEXITY OF LINEAR SEARCH


BINARY SEARCH-(DIVIDE & CONQUER)

Step 1 - Read the search element from the user.


Step 2 - Find the middle element in the sorted list.
Step 3 - Compare the search element with the middle element in the sorted list.
Step 4 - If both are matched, then display "Given element is found!!!" and terminate the function.
Step 5 - If both are not matched, then check whether the search element is smaller or larger than the middle element.
Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5 for the left sub list of the middle
element.
Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for the right sub list of the middle
element.
Step 8 - Repeat the same process until we find the search element in the list or until sub list contains only one element.
Step 9 - If that element also doesn't match with the search element, then display "Element is not found in the
list!!!" and terminate the function.
BINARY SEARCH-ITERATIVE
void binarysearch(int a[],int n,int m) //n=11 m=80
{
a[11]
int beg,end,mid,flag=0;
beg=0; 10 20 30 40 50 60 70 80 90 100 110
end=n-1;
while(beg<=end) //stopping cond. 0 1 2 3 4 5 6 7 8 9 10
{ end
mid
mid=(beg+end)/2; beg
if(a[mid]==m) // case-1
{ case 1: a[mid]==m
printf("\nElement found at position %d",mid); case 2: a[mid]>m
flag=1; case 3: a[mid]<m
getch ();
return;
}
if(a[mid]>m) // case-2 Search=m= 85
beg end mid Array
{
end=mid-1; 0 10 (0+10)/2=5 6-10
}
if(a[mid]<m) // case-3
6 10 (6+10)/2=8 6-7
{
beg=mid+1;
} 6 7 (6+7)/2=6 7-7
}
if(flag==0) 7 7 (7+7)/2=7
printf("\nElement not found");
BINARY SEARCH-ITERATIVE
void binarysearch(int a[],int n,int m)
{
int beg,end,mid,i,flag=0;
beg=0;
a[10]
10 20 30 40 50 60 70 80 90 100
end=n-1;
while(beg<=end) 0 1 2 3 4 5 6 7 8 10
{ index
mid=(beg+end)/2;
if(a[mid]==m)
{
printf("\nElement found at position %d",mid+1); case 1: a[mid]==m
flag=1; case 2: a[mid]>m
getch (); case 3: a[mid]<m
return;
}
if(a[mid]>m) Search= 80
{
beg end mid
end=mid-1;
}
if(a[mid]<m)
0 10 5(60) 0-4
{ 6-10
beg=mid+1;
}
6 10 8(90) 6-7
} 10
if(flag==0)
{
6 7 6(70) 7
printf("\nElement not found");
} 7 7 7(80)
}
TIME COMPLEXITY OF BINARY
SEARCH
Best case time complexity: O(1)
BINARY SEARCH-RECURSIVE

int bbinarysearch(int a[],int l,int n,int m)


{ Int main()
int beg=l, end=n; {
//accept array
if(beg<=end)
b=bbinarysearch(a,0,n-1,m);
{ if(b==-1)
int mid=(beg+end)/2; {
if(a[mid]==m) printf("\nSorry! Element not found");
{ }
return mid; else
{
}
printf("\nElement present at position
if(a[mid]>m) %d",b+1);
{ }
return bbinarysearch(a,l,mid-1,m); }
}
return bbinarysearch(a,mid+1,n,m);
}
return -1;
}
CONCEPT OF TIME COMPLEXITIES &
GROWTH CHART.
 disclaimer: Content of this presentation is not original and it has been
prepared from various sources for teaching purposes.

THANK YOU

You might also like