Data Structures Through C R20 - Unit-5
Data Structures Through C R20 - Unit-5
UNIT – V
SEARCHING
DEFINITION
It is a method of finding the given element in the given list of
elements.
or
It is technique to find the location where the element is available
or present.
or
It is an algorithm to check whether a particular element is
present in the given list or not.
Types of Searching
✓ Linear Search
✓ Binary Search
✓ Fibonacci Search
LINEAR SEARCH
✓ It is a very simple search algorithm when compared with the other
two search algorithms.
✓ It is also called as sequential search or indexed search.
✓ To perform linear search, the list of elements need not be sorted.
✓ An ordered or unordered list will be searched by comparing the search
element with one by one element from the beginning of the list until
the desired element is found or till the end of the list.
✓ If the desired element is found in the list then the search is successful
otherwise unsuccessful.
✓ The time complexity for linear search is O(n) where n is the number
of elements in the list.
✓ The time complexity increases with the increase of the input size n.
1
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 1
DATA STRUCTURES
2
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 2
DATA STRUCTURES
3
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 3
DATA STRUCTURES
BINARY SEARCH
✓ It is the fastest searching algorithm when compared with the other
two algorithms.
✓ It works on the principle divide – conquer strategy.
✓ To apply binary search algorithm the list of elements should be in
sorted order.
✓ The time complexity for binary search algorithm is O(log n).
✓ It is applied to very large set of elements
✓ The process carried by binary search algorithm is find the middle
element and compare it with search element it match return the index
of the element and say success otherwise see if the search element is
greater than or less than the middle element.
✓ If it is greater than the middle element then search the element in the
upper part of the list otherwise in the lower part of the list.
✓ Again find middle element and do the same process till the element is
found or not found.
✓ Using binary search algorithm we can reduce the number of
comparisons hence it is best.
4
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 4
DATA STRUCTURES
5
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 5
DATA STRUCTURES
6
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 6
DATA STRUCTURES
www.Jntufastupdates.com 7
DATA STRUCTURES
FIBONACCI SEARCH
✓ It was developed by Kiefer in 1953.
✓ In Fibonacci search we consider the indices as numbers from
fibonacci series.
✓ To apply fibonacci search algorithm the list that contains elements
should be in sorted order.
✓ The time complexity of fibonacci search algorithm is O(log n)
✓ It works on the principle divide - conquer strategy.
8
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 8
DATA STRUCTURES
www.Jntufastupdates.com 9
DATA STRUCTURES
scanf("%d", &ar[i]);
printf("Enter the element to be searched");
scanf("%d", &key);
search(ar, n, key, n, fib(n), fib(fib(n)));
return 0;
}
int fib(int n)
{
int a, b, f;
if(n<1)
return n;
a=0;
b=1;
while(b<n)
{
f=a+b;
a=b;
b=f;
}
return a;
}
void search(int ar[], int n, int key, int f, int b, int a)
{
if(f<1 || f>n)
printf("the number is not present");
else if(key<ar[f])
{
if(a<=0)
printf("The element is not present in the list");
else
search(ar, n, key, f-a, a, b-a);
}
10
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 10
DATA STRUCTURES
else if(key>ar[f])
{
if(b<=1)
printf("The element is not present in the list");
else
search(ar, n, key, f+a, b-a, a-b);
}
else
printf("Element is present %d", f);
}
11
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 11
DATA STRUCTURES
SORTING
DEFINITION
Sorting is a technique to rearrange the list of elements either in
ascending or descending order, which can be numerical, alphabetical or
any user-defined order.
Types of Sorting
Internal Sorting
✓ If the data to be sorted remains in main memory and also the sorting
is carried out in main memory then it is called internal sorting.
✓ Internal sorting takes place in the main memory of a computer.
✓ The internal sorting methods are applied to small collection of data.
✓ The following are some internal sorting techniques:
✓ Insertion sort
✓ Merge Sort
✓ Quick Sort
✓ Heap Sort
External Sorting
✓ If the data resides in secondary memory and is brought into main
memory in blocks for sorting and then result is returned back to
secondary memory is called external sorting.
✓ External sorting is required when the data being sorted do not fit into
the main memory.
✓ The following are some external sorting techniques:
✓ Two-Way External Merge Sort
✓ K-way External Merge Sort
INSERTION SORT
In this method, the elements are inserted at their appropriate place.
Hence the name insertion sort.
✓ This sorting is very simple to implement.
12
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 12
DATA STRUCTURES
The control moves to while loop as j>=0 and a[j] > temp is true, the
while is executed.
13
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 13
DATA STRUCTURES
The control moves to while loop as j>=0 and a[j] > temp is true, the
while is executed.
14
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 14
DATA STRUCTURES
The control moves to while loop as j>=0 and a[j] > temp is false, the
while is not executed.
15
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 15
DATA STRUCTURES
The control moves to while loop as j>=0 and a[j] > temp is false, the
while is not executed.
www.Jntufastupdates.com 16
DATA STRUCTURES
SELECTION SORT
✓ It is easy and simple to implement
✓ It is used for small list of elements
✓ It uses less memory
✓ It is efficient than bubble sort technique
✓ It is not efficient when used with large list of elements
17
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 17
DATA STRUCTURES
18
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 18
DATA STRUCTURES
SET pos = J
Step 4: Return pos
Step 5: Stop
19
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 19
DATA STRUCTURES
www.Jntufastupdates.com 20
DATA STRUCTURES
}
}
BUBBLE SORT
✓ It is known as exchange sort
✓ It is also known as comparison sort
✓ It is easiest and simple sort technique but inefficient.
✓ It is not a stable sorting technique.
✓ The time complexity of bubble sort is O(n2) in all cases.
✓ Bubble sort uses the concept of passes.
✓ The phases in which the elements are moving to acquire their proper
positions is called passes.
✓ It works by comparing adjacent elements and bubbles the largest
element towards right at the end of the first pass.
✓ The largest element gets sorted and placed at the end of the sorted
list.
✓ This process is repeated for all pairs of elements until it moves the
largest element to the end of the list in that iteration.
✓ Bubble sort consists of (n-1) passes, where n is the number of
elements to be sorted.
✓ In 1st pass the largest element will be placed in the nth position.
✓ In 2nd pass the second largest element will be placed in the (n-1)th
position.
✓ In (n-1)th pass only the first two elements are compared.
21
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 21
DATA STRUCTURES
22
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 22
DATA STRUCTURES
23
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 23
DATA STRUCTURES
www.Jntufastupdates.com 24
DATA STRUCTURES
printf("%d\t", a[i]);
}
QUICK SORT
✓ It is developed by C.A.R. Hoare.
✓ It is also known as partition exchange sort.
✓ This sorting algorithm uses divide and conquer strategy.
✓ In this method, the division is carried out dynamically.
✓ It contains three steps:
✓ Divide – split the array into two sub arrays so that each element in
the right sub array is greater than the middle element and each
element in the left sub array is less than the middle element. The
splitting is done based on the middle element called pivot. All the
elements less than pivot will be in the left sub array and all the
elements greater than pivot will be on right sub array.
✓ Conquer – recursively sort the two sub arrays.
✓ Combine – combine all the sorted elements in to a single list.
✓ Consider an array A[i] where i is ranging from 0 to n – 1 then the
division of elements is as follows:
A[0]……A[m – 1], A[m], A[m + 1] …….A[n]
✓ The partition algorithm is used to arrange the elements such that
all the elements are less than pivot will be on left sub array and
greater then pivot will be on right sub array.
✓ The time complexity of quick sort algorithm in worst case is O(n2),
best case and average case is O(n log n).
✓ It is faster than other sorting techniques whose time complexity is
O(n log n)
25
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 25
DATA STRUCTURES
26
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 26
DATA STRUCTURES
Now a[i] > pivot, so stop incrementing i. As a[j] > pivot we will decrement j
until it becomes false
Now we cannot decrement j because a[j] < pivot. Hence we swap a[i] and a[j]
since i < j.
Now a[i] > pivot so stop incrementing i and a[j] > pivot so decrement j
27
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 27
DATA STRUCTURES
Now a[j] < pivot so stop decrementing j. since i <j swap a[i] and a[j]
Now a[i] > pivot, so stop incrementing i. As a[j] > pivot we will decrement j
until it becomes false
.
As a[i] > pivot and a[j] < pivot and j crossed i we will swap a[low] and a[j]
We will now start left array to be sorted and then right sub array. The new
pivot for the left sub array is 20
28
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 28
DATA STRUCTURES
Now since a[i] > pivot stop incrementing i and a[j] > pivot so decrement j
Now a[i] > pivot so stop incrementing i and a[j] > pivot so decrement j
Since a[j] < pivot so j cannot be decremented and j crossed i so swap a[low]
and a[j]
There is one element in the left sub array hence all the elements in the right
sub array is to be sorted.
29
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 29
DATA STRUCTURES
Since a[i] > pivot and a[j] < pivot we stop incrementing I and decrementing j
and I < j we swap a[i] and a[j]
Since a[i] > pivot so stop incrementing i and a[j] > pivot so decrement j
Since a[j] < pivot so j cannot be decremented and j crossed i so swap a[low]
and a[j]
Now the left contains 70 and right contains 90 we cannot further subdivide
the array. Hence if we look at the array all the elements are sorted.
30
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 30
DATA STRUCTURES
www.Jntufastupdates.com 31
DATA STRUCTURES
flag =1;
else if(a[mid]>a[right])
{
temp = a[mid];
a[mid] = a[right];
a[right] = temp;
mid = right;
}
if(flag!=1)
{
while((a[mid] >= a[left]) && (mid!=left))
left++;
if(mid==left)
flag =1;
else if(a[mid] <a[left])
{
temp = a[mid];
a[mid] = a[left];
a[left] = temp;
mid = left;
}
}
}
return mid;
}
void quick_sort(int a[], int low, int high)
{
int mid;
if(low<high)
{
mid = partition(a, low, high);
quick_sort(a, low, mid -1);
32
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 32
DATA STRUCTURES
RADIX SORT
✓ It is a linear sorting algorithm.
✓ It is also known as bucket sort technique or binsort technique or card
sort technique since it uses buckets for sorting.
✓ It can be applied for integers as well as letters. For integers it used 10
buckets and for letters it uses 26 buckets.
✓ If the input is integers then we sort them from least significant digit to
most significant digit.
✓ The number passes used in radix sort depends on the number of
digits.
✓ The time complexity of radix sort in all cases is O(n log n)
✓ It takes more space compared to other sorting algorithms.
✓ It is used only for digits and letters
✓ It depends on the number of digits and letters.
33
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 33
DATA STRUCTURES
After the first pass the numbers are collected bucket by bucket. Thus
the new list for the second pass is
911, 472, 123, 654, 924, 345, 555, 567, 808
In the second pass the numbers are sorted according to the digit at
ten’s place.
34
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 34
DATA STRUCTURES
After the second pass the numbers are collected bucket by bucket.
Thus the new list for the third pass is
808, 911, 123, 924, 345, 654, 555,567, 472
In the third pass the numbers are sorted according to the digit at
hundred place.
After the third pass the numbers are collected bucket by bucket. Thus
the new list formed is the final result. It is
123, 345, 472, 555, 567, 654, 808, 911, 924
www.Jntufastupdates.com 35
DATA STRUCTURES
MERGE SORT
✓ This sorting algorithm uses divide and conquer strategy.
✓ In this method, the division is carried out dynamically.
✓ It contains three steps:
✓ Divide – split the array into two sub arrays s1 and s2 with each n/2
elements. If A is an array containing zero or one element, then it is
already sorted. But if there are more elements in the array, divide A
36
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 36
DATA STRUCTURES
into two sub-arrays, s1 and s2, each containing half of the elements of
A.
✓ Conquer – sort the two sub arrays s1 and s2.
✓ Combine – combine or merge s1 and s2 elements into a unique sorted
list.
✓ The time complexity of merge sort is O(n log n) in all cases.
37
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 37
DATA STRUCTURES
We then first make the two sublists and combine the two sorted sublists as
a unique sorted list.
38
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 38
DATA STRUCTURES
1, 3, 4, 5, 6, 0, 2, 7, 8, 9
39
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 39
DATA STRUCTURES
40
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 40
DATA STRUCTURES
Now again i is incremented. But the left sub list is completed then j is
incremented until the right sub list is completed
41
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 41
DATA STRUCTURES
42
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 42
DATA STRUCTURES
43
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 43
DATA STRUCTURES
index++;
}
}
for(k=low;k<index;k++)
a[k] = temp[k];
}
void merge_sort(int a[], int low, int high)
{
int mid;
if(low<high)
{
mid = (low+high)/2;
merge_sort(a, low, mid);
merge_sort(a, mid+1, high);
combine(a, low, mid, high);
}
}
HEAP SORT
✓ Heap is a complete binary tree and also a Max(Min) tree.
✓ A Max(Min) tree is a tree whose root value is larger(smaller) than its
children.
✓ This sorting technique has been developed by J.W.J. Williams.
✓ It is working under two stages.
Heap construction
Deletion of a Maximum element key
✓ The heap is first constructed with the given numbers. The maximum
key value is deleted for n -1 times to the remaining heap. Hence we
will get the elements in decreasing order. The elements are deleted one
by one and stored in the array from last to first. Finally we get the
elements in ascending order.
✓ The important points about heap sort technique are:
44
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 44
DATA STRUCTURES
45
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 45
DATA STRUCTURES
46
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 46
DATA STRUCTURES
47
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 47
DATA STRUCTURES
48
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 48
DATA STRUCTURES
for(i=0;i<n;i++)
scanf("%d", &a[i]);
makeheap(a, n);
heap_sort(a, n);
printf(" The elements after sorting are ");
for(i=0;i<n;i++)
printf("\t%d", a[i]);
return 0;
}
void makeheap(int a[], int n)
{
int i, val, j, parent;
for(i=1;i<n;i++)
{
val = a[i];
j = i;
parent = (j - 1) / 2;
while(j>0 && parent < val)
{
a[j] = a[parent];
j = parent;
parent = (j - 1) / 2;
}
a[j] = val;
}
}
49
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 49
DATA STRUCTURES
temp = a[i];
a[i] = a[0];
k = 0;
if(i == 1)
j = -1;
else
j = 1;
if(i > 2 && a[2] > a[1])
j = 2;
while(j >=0 && temp < a[j])
{
a[k] = a[j];
k = j;
j = 2 * k +1;
if(j+1 <= i-1 && a[j] < a[j+1])
j++;
if(j > i-1)
j = -1;
}
a[k] = temp;
}
}
50
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
www.Jntufastupdates.com 50