Chapter II Searching Sorting
Chapter II Searching Sorting
Searching :-
1.Linear Search :-
In Linear Search the list is searched sequentially and the position is returned if the key
element to be searched is available in the list, otherwise -1 is returned.
The search in Linear Search starts at the beginning of an array and move to the end,
testing for a match at each item.
All the elements preceding the search element are traversed before the search element is
traversed. i.e. if the element to be searched is in position 10, all elements form 1-9 are
checked before 10.
Here we are searching for the element 33 .To search 33 the item 33 is compared with the
element at A[0] then A[1] and so on .Until we find the key value or reach to the end of
array.
When the item is found it displays the location of an item else displays item not found
Algorithm :-
Algorithm
Linear Search ( Array A, Value x)
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Binary Search:-
Step 8: Exit
The binary search technique requires the list to be sorted in an ascending order.
Binary search is a fast search algorithm with run-time complexity of Ο(log n).
Binary search a particular item by comparing the middle most item of the collection.
If match occurs then index of item is returned.
If middle item is greater than item then item is searched in sub-array to the right of the
middle item otherwise item is search in sub-array to the left of the middle item..
This process continues on sub-array as well until the size of sub-array reduces to zero.
Features of Linear Search Algorithm :-
1. It is used for unsorted and unordered small list of elements.
2. It has a time complexity of O(n), which means the time is linearly dependent on the
number of elements, which is not bad, but not that good too.
3. It has a very simple implementation.
Consider for example the below given is our sorted array and assume that we need to search
location of value 31 using binary search.
First, we shall determine the half of the array by using this formula –
mid = (low+high)/2
Now we compare the value stored at location 4, with the value being searched i.e. 31. We find
that value at location 4 is 27, which is not a match. Because value is greater than 27 and we have
a sorted array so we also know that target value must be in upper portion of the array.
We change our low to mid + 1 and find the new mid value again.
low = mid + 1
mid = mid = (low + high)/2
Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.
The value stored at location 7 is not a match, rather it is less that what we are looking for. So the
value must be in lower part from this location.
high = mid -1 ie high= 6 now 31<33
We compare the value stored ad location 5 with our target value. We find that it is a match.
We conclude that the target value 31 is stored at location 5.
Algorithm:-
Binary_Search(a, low, high, val) // 'a' is the given array, low is the index of the first array
element, high is the index of the last array element, 'val' is the value to search
Step 1: set beg = low, end = high, pos = - 1
Step 2: repeat steps 3 and 4 while beg <=end
Step 3: set mid = (beg + end)/2
Step 4: if a[mid] = val
set pos = mid
print pos
go to step 6
else if a[mid] > val
set end = mid - 1
else
set beg = mid + 1
[end of if]
[end of loop]
Step 5: if pos = -1
print "value is not present in the array"
[end of if]
Step 6: exit
Time Complexity: The time complexity of binary search in a successful search is O(log n) and
for an unsuccessful search is O(log n).
Sorting:-
1. Selection Sort
2. Bubble Sort
3. Insertion Sort
4. Merge Sort
5. Radix Sort
1.Selection sort :-
Algorithm :-
1. Start
2. Accept Array
3. Compare elements as
Selection sort algorithm is 60% more efficient than bubble sort algorithm.
Selection sort algorithm is easy to implement.
Selection sort algorithm can be used for small data sets, unfortunately Insertion sort algorithm
best suitable for it.
Bubble Sort:-
Bubble Sort is an algorithm which is used to sort N elements that are given in a memory for eg:
an Array with N number of elements. Bubble Sort compares all the element one by one and sort
them based on their values.
It is called Bubble sort, because with each iteration the smaller element in the list bubbles up
towards the first place, just like a water bubble rises up to the water surface.
Sorting takes place by stepping through all the data items one-by-one in pairs and comparing
adjacent data items and swapping each pair that is out of order.
1. Start
2. Accept Array
3. Compare elements as
for(i=0;i<n-1;i++)
{
for(j=0;j<(n-(i+1));j++)
{
If(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
.
Advantages:-
The primary advantage of the bubble sort is that it is popular and easy to implement.
In the bubble sort, elements are swapped in place without using additional temporary storage.
The space requirement is at a minimum
Disadvantages :-
The main disadvantage of the bubble sort is the fact that it does not deal well with a list
containing a huge number of items.
The bubble sort requires n-squared processing steps for every n number of elements to be sorted.
The bubble sort is mostly suitable for academic teaching but not for real-life applications
Average and worst case complexity of bubble sort is O(n2). Also, it makes O(n2) swaps in the
worst case
Insertion Sort:-
It is a simple Sorting algorithm which sorts the array by shifting elements one by one. Following
are some of the important characteristics of Insertion Sort.
Disadvantages:-
The disadvantage of the insertion sort is that it does not perform as well as other, better
sorting algorithms.
With n-squared steps required for every n element to be sorted, the insertion sort does not
deal well with a huge list.
The insertion sort is particularly useful only when sorting a list of few items.
Merge Sort :-
Merge sort keeps on dividing the list into equal halves until it can no more be divided. By
definition, if it is only one element in the list, it is sorted. Then, merge sort combines the smaller
sorted lists keeping the new list sorted too.
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.
END MERGE_SORT
The important part of the merge sort is the MERGE function. This function performs the
merging of two sorted sub-arrays that are A[beg…mid] and A[mid+1…end], to build
one sorted array A[beg…end]. So, the inputs of the MERGE function are A[], beg,
mid, and end
Complexity Analysis of Merge Sort
Advantages:-
It can be applied to files of any size.
If heap sort is used for the in-memory part of the merge, its operation can be overlapped with I/O
Disadvantages:-
Requires extra space N
Merge Sort requires more space than other sort.
Radix Sort:-
Radix Sort's time complexity of O(nd), where n is the size of the array and d is the number of
digits in the largest number.
Radix Sort is a stable sort because it maintains the relative order of elements with equal
values.
Radix sort algorithm may be slower than other sorting algorithms such as merge sort
and Quicksort if the operations are inefficient. These operations include sub-inset lists and
delete functions, and the process of isolating the desired digits.
Because it is based on digits or letters, radix sort is less flexible than other sorts. If the type of
data changes, the Radix sort must be rewritten.
Algorithm :-
RadixSort (arr)
max = largest element in the given array
d = number of digits in the largest element (or, max)
Now, create d buckets of size 0 - 9
for i -> 0 to d
sort the array elements using counting sort (or any stable sort) according to the digits at
the ith place
Consider the following 9 numbers:
Digit Sublist
0 340 710
1
2 812 582
3 493
4
5 715 195 385
6
7 437
8
9
Notice that the numbers were added onto the list in the order that they were found, which is why
the numbers appear to be unsorted in each of the sublists above. Now, we gather the sublists (in
order from the 0 sublist to the 9 sublist) into the main list again:
The order in which we divide and reassemble the list is extremely important, as this is one of
the foundations of this algorithm.
Now, the sublists are created again, this time based on the ten's digit:
Digit Sublist
0
1 710 812 715
2
3 437
4 340
5
6
7
8 582 385
9 493 195
Digit Sublist
0
1 195
2
3 340 385
4 437 493
5 582
6
7 710 715
8 812
9
And now we have a fully sorted array! Radix Sort is very simple, and a computer can do it fast.
When it is programmed properly, Radix Sort is in fact one of the fastest sorting algorithms for
numbers or strings of letters.
Fast when the keys are short, i.e. when the array element range is small.
Radix Sort is a stable sort because it maintains the relative order of elements with equal
values.
The Radix Sort algorithm is less flexible than other sorts because it is based on digits or
letters. As a result, for each different type of data, it must be rewritten.
Radix sort may be slower than other sorting algorithms such as merge sort and Quicksort if
the operations are inefficient. These operations include sub-inset lists and delete functions, as
well as the process of isolating the desired digits.
Because it is based on digits or letters, the radix sort is less flexible than other sorts. If the
data type must be rewritten, so must the Radix sort.
Time Complexity:-