Ds Unit5 Notes
Ds Unit5 Notes
DS unit5 notes
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
UNIT 5
Introduction to SEARCHING
Searching means to find whether a particular value is present in an array or not. If the value is
present in the array, then searching is said to be successful and the searching process gives
the location of that value in the array. However, if the value is not present in the array, the
searching process displays an appropriate message and in this case searching is said to be
unsuccessful.
There are two popular methods for searching the array elements: linear search and binary
search. The algorithm that should be used depends entirely on how the values are organized
in the array. For example, if the elements of the array are arranged in ascending order, then
binary search should be used, as it is more efficient for sorted lists in terms ofcomplexity.
Linear Search
Linear search, also called as sequential search, is a very simple method used for searching an
array for a particular value. It works by comparing the value to be searched with every
element of the array one by one in a sequence until a match is found. Linear search is mostly
used to search an unordered list of elements (array in which data elements are not sorted). For
example, if an array A[] is declared and initialized as,
and the value to be searched is VAL = 7, then searching means to find whether the value 879
is present in the array or not. If yes, then it returns the position of its occurrence. Here, POS =
3 (index starting from0).
LINEAR_SEARCH(A, N, VAL)
Step 1: [INITIALIZE] SET POS = -1
Step 2: [INITIALIZE] SET I = 1
Step 3: Repeat Step 4 while I<=N
Step 4: IF A[I] = VAL
SET POS = I
PRINT POS
Go to Step 6
[END OF IF]
SET I = I + 1
[END OF LOOP]
Step 5: IF POS = –1
PRINT VALUE IS NOT PRESENT
IN THE ARRAY
[END OF IF]
Step 6: EXIT
STUDENTSFOCUS.COM
In Steps 1 and 2 of the algorithm, we initialize the value of POS and I. In Step 3, a while loop
is executed that would be executed till I is less than N (total number of elements in the array).
In Step 4, a check is made to see if a match is found between the current array element and
VAL. If a match is found, then the position of the array element is printed, else the value of I
is incremented to match the next element with
VAL. However, if all the array elements have been compared with VAL and no match is
found, then it means that VAL is not present in the array.
Binary search is a searching algorithm that works efficiently with a sorted list. The
mechanism of binary search can be better understood by an analogy of a telephone directory.
When we are searching for a particular name in a directory, we first open the directory from
the middle and then decide whether to look for the name in the first part of the directory or in
the second part of the directory. Again, we open some page in the middle and the whole
process is repeated until we finally find the right name. Take another analogy. How do we
find words in a dictionary? We first open the dictionary somewhere in the middle. Then, we
compare the first word on that page with the desired word whose meaning we are looking for.
If the desired word comes before the word on the page, we look in the first half of the
dictionary, else we look in the second half. Again, we open a page in the first half of the
dictionary and compare the first word on that page with the desired word and repeat the same
procedure until we finally get the word. The same mechanism is applied in the binary search.
Now, let us consider how this mechanism is applied to search for a value in a sorted array.
and the value to be searched is VAL = 9. The algorithm will proceed in the following
manner.
BEG = 0, END = 10, MID = (0 + 10)/2 = 5
STUDENTSFOCUS.COM
In this algorithm, we see that BEG and END are the beginning and ending positions of the
segment that we are looking to search for the element. MID is calculated as (BEG + END)/2.
Initially, BEG = lower_bound and END = upper_bound. The algorithm will terminate when
A[MID] = VAL. When the algorithm ends, we will set POS = MID. POS is the position at
which the value is present in the array.
However, if VAL is not equal to A[MID], then the values of BEG, END, and MID will be
changed depending on whether VAL is smaller or greater than A[MID].
(a) If VAL <A[MID], then VAL will be present in the left segment of the array. So, the value
of END will be changed as END = MID –1.
(b) If VAL >A[MID], then VAL will be present in the right segment of the array. So, the
value of BEG will be changed as BEG = MID +1.
Finally, if VAL is not present in the array, then eventually, END will be less than BEG.
When this happens, the algorithm will terminate and the search will beunsuccessful.
In Step 1, we initialize the value of variables, BEG, END, and POS. In Step 2, a while loop is
executed until BEG is less than or equal to END. In Step 3, the value of MID is calculated. In
Step 4, we check if the array value at MID is equal to VAL (item to be searched in the array).
If a match is found, then the value of POS is printed and
the algorithm exits. However, if a match is not found, and if the value of A[MID]
is greater than VAL, the value of END is modified, otherwise if A[MID] is greater
thanVAL, then the value of BEG is altered. In Step 5, if the value of POS = –1, then VAL is
not present in the array and an appropriate message is printed on the screen before the
algorithm exits.
STUDENTSFOCUS.COM
The complexity of the binary search can be expressed as f(n), where n is the number of
elements in the array. The complexity of the algorithm is calculated depending on the number
of comparisons that are made. In the binary search algorithm, we see that with each
comparison, the size of the segment where search has to be made is reduced to half. Thus, we
can say that, in order to locate a particular value in the array, the total number of comparisons
that will be made is given as 2f(n) > n or f(n) = log n.
INTRODUCTION TO SORTING
Sorting means arranging the elements of an array so that they are placed in some relevant
order which may be either ascending or descending. That is, if A is an array, then the
elements of A are arranged in a sorted order (ascending order) in such a way that A[0] <A[1]
<A[2] << A[N].
For example, if we have an array that is declared and initialized as
intA[] = {21, 34, 11, 9, 1, 0, 22};
Then the sorted array (ascending order) can be given as:
A[] = {0, 1, 9, 11, 21, 22, 34;
A sorting algorithm is defined as an algorithm that puts the elements of a list in a certain
order, which can be either numerical order, lexicographical order, or any user-defined order.
Efficient sorting algorithms are widely used to optimize the use of other algorithms like
search and merge algorithms which require sorted lists to work correctly. There are two types
ofsorting:
Internal sorting which deals with sorting the data stored in the computer9smemory
External sorting which deals with sorting the data stored in files. External sorting is
applied when there is voluminous data that cannot be stored in thememory.
BUBBLE SORT
Bubble sort is a very simple method that sorts the array elements by repeatedly moving the
largest element to the highest index position of the array segment (in case of arranging
elements in ascending order). In bubble sorting, consecutive adjacent pairs of elements in the
array are compared with each other. If the element at the lower index is greater than the
element at the higher index, the two elements are interchanged so that the element is placed
before the bigger one. This process will continue till the list of unsorted elements exhausts.
This procedure of sorting is called bubble sorting because elements 8bubble9 to the top of the
list. Note that at the end of the first pass, the largest element in the list will be placed at its
proper position (i.e., at the end of the list).
NOTE:
If the elements are to be sorted in descending order, then in first pass the smallest element is
moved to the highest index of the array.
Technique
The basic methodology of the working of bubble sort is given as follows:
(a) In Pass 1, A[0] and A[1] are compared, then A[1] is compared with A[2], A[2] is
compared with A[3], and so on. Finally, A[N–2] is compared with A[N–1]. Pass 1 involves
n–1 comparisons and places the biggest element at the highest index of thearray.
STUDENTSFOCUS.COM
(b) In Pass 2, A[0] and A[1] are compared, then A[1] is compared with A[2], A[2] is
compared with A[3], and so on. Finally, A[N–3] is compared with A[N–2]. Pass 2 involves
n–2 comparisons and places the second biggest element at the second highest index of the
array.
(c) In Pass 3, A[0] and A[1] are compared, then A[1] is compared with A[2], A[2] is
compared with A[3], and so on. Finally, A[N–4] is compared with A[N–3]. Pass 3 involves
n–3 comparisons and places the third biggest element at the third highest index of thearray.
(d) In Pass n–1, A[0] and A[1] are compared so that A[0]<A[1]. After this step, all the
elements of the array are arranged in ascendingorder.
Example
To discuss bubble sort in detail, let us consider an array A[] that has the following
elements:
A[] = {30, 52, 29, 87, 63, 27, 19, 54}
Pass 1:
(a) Compare 30 and 52. Since 30 < 52, no swapping isdone.
(b) Compare 52 and 29. Since 52 > 29, swapping is done.
30, 29, 52, 87, 63, 27, 19, 54
(c) Compare 52 and 87. Since 52 < 87, no swapping isdone.
(d) Compare 87 and 63. Since 87 > 63, swapping is done.
30, 29, 52, 63, 87, 27, 19, 54
(e) Compare 87 and 27. Since 87 > 27, swapping is done.
30, 29, 52, 63, 27, 87, 19, 54
(f) Compare 87 and 19. Since 87 > 19, swapping is done.
30, 29, 52, 63, 27, 19, 87, 54
(g) Compare 87 and 54. Since 87 > 54, swapping is done.
30, 29, 52, 63, 27, 19, 54, 87
Observe that after the end of the first pass, the largest element is placed at the highest index
of the array. All the other elements are still unsorted.
Pass 2:
(a) Compare 30 and 29. Since 30 > 29, swapping isdone.
29, 30, 52, 63, 27, 19, 54, 87
(b) Compare 30 and 52. Since 30 < 52, no swapping isdone.
(c) Compare 52 and 63. Since 52 < 63, no swapping isdone.
(d) Compare 63 and 27. Since 63 > 27, swapping is done.
29, 30, 52,27, 63, 19, 54, 87
(e) Compare 63 and 19. Since 63 > 19, swapping is done.
29, 30, 52, 27, 19, 63, 54, 87
(f) Compare 63 and 54. Since 63 > 54, swapping is done.
29, 30, 52, 27, 19, 54, 63, 87
Observe that after the end of the second pass, the second largest element is placed at the
second highest index of the array. All the other elements are still unsorted.
Pass 3:
(a) Compare 29 and 30. Since 29 < 30, no swapping isdone.
STUDENTSFOCUS.COM
Observe that after the end of the third pass, the third largest element is placed at the third
highest index of the array. All the other elements are still unsorted.
Pass 4:
(a) Compare 29 and 30. Since 29 < 30, no swapping isdone.
(b) Compare 30 and 27. Since 30 > 27, swapping is done.
29, 27, 30, 19, 52, 54, 63, 87
(c) Compare 30 and 19. Since 30 > 19, swapping is done.
29, 27, 19, 30, 52, 54, 63, 87
(d) Compare 30 and 52. Since 30 < 52, no swapping isdone.
Observe that after the end of the fourth pass, the fourth largest element is placed at the fourth
highest index of the array. All the other elements are still unsorted.
Pass 5:
(a) Compare 29 and 27. Since 29 > 27, swapping isdone.
27, 29, 19, 30, 52, 54, 63, 87
(b) Compare 29 and 19. Since 29 > 19, swapping is done.
27, 19, 29, 30, 52, 54, 63, 87
(c) Compare 29 and 30. Since 29 < 30, no swapping isdone.
Observe that after the end of the fifth pass, the fifth largest element is placed at the fifth
highest index of the array. All the other elements are still unsorted.
Pass 6:
(a) Compare 27 and 19. Since 27 > 19, swapping isdone.
19, 27, 29, 30, 52, 54, 63, 87
(b) Compare 27 and 29. Since 27 < 29, no swapping isdone.
Observe that after the end of the sixth pass, the sixth largest element is placed at the sixth
largest index of the array. All the other elements are still unsorted.
Pass 7:
(a) Compare 19 and 27. Since 19 < 27, no swapping isdone.
BUBBLE_SORT(A, N)
Step 1: Repeat Step 2 For 1 = 0 to N-1
Step 2: Repeat For J = 0 to N - I
Step 3: IF A[J] > A[J + 1]
SWAP A[J] and A[J+1]
[END OF INNER LOOP]
[END OF OUTER LOOP]
Step 4: EXIT
STUDENTSFOCUS.COM
In this algorithm, the outer loop is for the total number of passes which is N–1. The inner
loop will be executed for every pass. However, the frequency of the inner loop will decrease
with every pass because after every pass, one element will be in its correct position.
Therefore, for every pass, the inner loop will be executed N–I times, where N is the number
of elements in the array and I is the count of thepass.
INSERTION SORT
Insertion sort is a very simple sorting algorithm in which the sorted array (or list) is built one
element at a time. We all are familiar with this technique of sorting, as we usually use it for
ordering a deck of cards while playing bridge.
The main idea behind insertion sort is that it inserts each item into its proper place in the final
list. To save memory, most implementations of the insertion sort algorithm work by moving
the current data element past the already sorted values and repeatedly interchanging it with
the preceding value until it is in its correct place. Insertion sort is less efficient as compared to
other more advanced algorithms such as quick sort, heap sort, and mergesort.
Technique
Insertion sort works as follows:
The array of values to be sorted is divided into two sets. One that stores sorted values
and another that contains unsortedvalues.
The sorting algorithm will proceed until there are elements in the unsortedset.
Suppose there are n elements in the array. Initially, the element with index 0
(assuming LB = 0) is in the sorted set. Rest of the elements are in the unsortedset.
The first element of the unsorted partition has array index 1 (if LB =0).
During each iteration of the algorithm, the first element in the unsorted set is picked
up and inserted into the correct position in the sortedset.
Example
Consider an array of integers given below. We will sort the values in the array
using insertion sort.
STUDENTSFOCUS.COM
Initially, A[0] is the only element in the sorted set. In Pass 1, A[1] will be placed either
before or after A[0], so that the array A is sorted. In Pass 2, A[2] will be placed either before
A[0], in between A[0] and A[1], or after A[1]. In Pass 3, A[3] will be placed in its proper
place. In Pass N–1, A[N–1] will be placed in its proper place to keep the arraysorted.
To insert an element A[K] in a sorted list A[0], A[1], ..., A[K–1], we need tocompare
A[K] with A[K–1], then with A[K–2], A[K–3], and so on until we meet an element A[J] such
that A[J] <= A[K]. In order to insert A[K] in its correct position, we need to move elements
A[K–1], A[K–2], ..., A[J] by one position and then A[K] is inserted at the (J+1)th location.
INSERTION-SORT (ARR, N)
Step 1: Repeat Steps 2 to 5 for K = 1 to N – 1
Step 2: SET TEMP = ARR[K]
Step 3: SET J = K - 1
Step 4: Repeat while TEMP <= ARR[J]
SET ARR[J + 1] = ARR[J]
SET J = J - 1
[END OF INNER LOOP]
Step 5: SET ARR[J + 1] = TEMP
[END OF LOOP]
Step 6: EXIT
In the algorithm, Step 1 executes a for loop which will be repeated for each element in the
array. In Step 2, we store the value of the Kth element in TEMP. In Step 3, we set the Jth
index in the array. In Step 4, a for loop is executed that will create space for the new element
from the unsorted list to be stored in the list of sorted elements. Finally, in Step 5, the element
is stored at the (J+1)th location.
STUDENTSFOCUS.COM
unsorted set has to be compared with almost every element in the sorted set. Furthermore,
every iteration of the inner loop will have to shift the elements of the sorted set of the array
before inserting the next element. Therefore, in the worst case, insertion sort has a quadratic
running time (i.e., O(n2)). Even in the average case, the insertion sort algorithm will have to
make at least (K–1)/2 comparisons. Thus, the average case also has a quadratic running time.
SELECTION SORT
Selection sort is a sorting algorithm that has a quadratic running time complexity of O(n2),
thereby making it inefficient to be used on large lists. Although selection sort performs worse
than insertion sort algorithm, it is noted for its simplicity and also has performance
advantages over more complicated algorithms in certain situations. Selection sort is generally
used for sorting files with very large objects (records) and smallkeys.
Technique
Example
For the first position in the sorted list, the whole list is scanned sequentially. The first position
where 14 is stored presently, we search the whole list and find that 10 is the lowest value.
Step 1:
14, 33, 27, 10, 35, 19, 42, 44
So we replace 14 with 10. After one iteration 10, which happens to be the minimum value in
the list, appears in the first position of sorted list.
For the second position, where 33 is residing, we start scanning the rest of the list in linear
manner.
Step 2:
10, 33, 27, 14, 35, 19, 42, 44
STUDENTSFOCUS.COM
We find that 14 is the second lowest value in the list and it should appear at the second place.
We swap these values.
After two iterations, two least values are positioned at the the beginning in the sorted manner.
The same process is applied on the rest of the items in the array. We shall see an pictorial
depiction of entire sorting process −
Step 3:
10, 14, 27, 33, 35, 19, 42, 44
Now replace 19 by 27
Step 4:
Now replace 27 by 33
Step 5:
Now replace 33 by 35
Step 6:
Step 8:
STUDENTSFOCUS.COM
Algorithm:
if(iMin != j)
{
swap(a[j], a[iMin]);
}
}
RADIX SORT
Radix sort is a linear sorting algorithm for integers and uses the concept of sorting names in
alphabetical order. When we have a list of sorted names, the radix is 26 (or 26 buckets)
because there are 26 letters in the English alphabet. So radix sort is also known as bucketsort.
Observe that words are first sorted according to the first letter of the name. That is, 26 classes
are used to arrange the names, where the first class stores the names that begin with A, the
second class contains the names with B, and soon.
During the second pass, names are grouped according to the second letter. After the second
pass, names are sorted on the first two letters. This process is continued till the nth pass,
where n is the length of the name with maximum number ofletters.
While sorting the numbers, we have ten buckets, each for one digit (0, 1, 2, …, 9) and the
number of passes will depend on the length of the number having maximum number of digts.
Algorithm
STUDENTSFOCUS.COM
Example
Solution
After this pass, the numbers are collected bucket by bucket. The new list thus formed is used
as an input for the next pass. In the second pass, the numbers are sorted according to the digit
at the tens place. The buckets are pictured upside down.
STUDENTSFOCUS.COM
In the third pass, the numbers are sorted according to the digit at the hundreds place. The
buckets are pictured upside down.
The numbers are collected bucket by bucket. The new list thus formed is the final sorted
result.
After the third pass, the list can be given as
123, 345, 472, 555, 567, 654, 808, 911, 924.
To calculate the complexity of radix sort algorithm, assume that there are n numbers that
have to be sorted and k is the number of digits in the largest number. In this case, the radix
sort algorithm is called a total of k times. The inner loop is executed n times. Hence, the
entire radix sort algorithm takes O(kn) time to execute. When radix sort is applied on a data
set of finite size (very small set of numbers), then the algorithm runs in O(n) asymptotictime.
Radix sort is a very simple algorithm. When programmed properly, radix sort is one of the
fastest sorting algorithms for numbers or strings of letters.
But there are certain trade-offs for radix sort that can make it less preferable as compared to
other sorting algorithms. Radix sort takes more space than other sorting algorithms. Besides
the array of numbers, we need 10 buckets to sort numbers, 26 buckets to sort strings
containing only characters, and at least 40 buckets to sort a string containing alphanumeric
characters.
Another drawback of radix sort is that the algorithm is dependent on digits or letters. This
feature compromises with the flexibility to sort input of any data type. For every different
data type, the algorithm has to be rewritten. Even if the sorting order changes, the algorithm
has to be rewritten. Thus, radix sort takes more time to write and writing a general purpose
radix sort algorithm that can handle all kinds of data is not a trivial task. Radix sort is a good
choice for many programs that need a fast sort, but there are faster sorting algorithms
available. This is the main reason why radix sort is not as widely used as other sorting
algorithms.
STUDENTSFOCUS.COM