0% found this document useful (0 votes)
203 views12 pages

Chapter II Searching Sorting

The document discusses various searching and sorting algorithms. It defines searching as finding a particular element in a set and describes linear and binary search approaches. Linear search sequentially checks each element while binary search divides the set in half at each step. The document also defines sorting as arranging data in a specific order and describes selection sort, bubble sort, insertion sort and merge sort. It provides pseudocode to demonstrate the algorithms and analyzes their time and space complexities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
203 views12 pages

Chapter II Searching Sorting

The document discusses various searching and sorting algorithms. It defines searching as finding a particular element in a set and describes linear and binary search approaches. Linear search sequentially checks each element while binary search divides the set in half at each step. The document also defines sorting as arranging data in a specific order and describes selection sort, bubble sort, insertion sort and merge sort. It provides pseudocode to demonstrate the algorithms and analyzes their time and space complexities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Chapter II

Searching and Sorting

Searching :-

 Searching is a process of locating a particular element present in a given set of elements.


 The element may be a record, a table, or a file.
 The search is said to be successful if the element is found i.e if the element is present in
set, otherwise it is unsuccessful.
 There are two simple approaches of searching:-
1.Linear Search
2.Binary Search

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.

How binary search works?

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

Here it is, (0+9) / 2 = 4 (integer value of 4.5). So 4 is the mid of array.

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

So we calculate the mid again. This time it is 5.

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:-

 Sorting is nothing but storage of data in sorted order, it can be in ascending or


descending order.
 Sorting arranges data in a sequence which makes searching easier.
 Every record which is going to be sorted will contain one key. Based on the key the
record will be sorted. For example, suppose we have a record of students, every such
record will have the following data:
Roll No.
Name
Age
Class
Here Student roll no. can be taken as key for sorting the records in ascending or descending
order.
 Types of sorting :-

1. Selection Sort
2. Bubble Sort
3. Insertion Sort
4. Merge Sort
5. Radix Sort

1.Selection sort :-

 Selection sorting is conceptually the most simplest sorting algorithm


 This algorithm first finds the smallest element in the array and exchanges it with the
element in the first position, then find the second smallest element and exchange it with
the element in the second position, and continues in this way until the entire array is
sorted.
 Consider ex. if you want to sort the elements of array in ascending order and if the first
element is greater than second then, you need to swap the elements but, if the first
element is smaller than second, leave the elements as it is.
 Then, again first element and third element are compared and swapped if necessary. This
process goes on until first and last element of an array is compared. This completes the
first step of selection sort.
 If there are n elements to be sorted then, the process mentioned above should be
repeated n-1 times to get required result.

Algorithm :-

1. Start
2. Accept Array
3. Compare elements as

for(i = 0; i < number; i++)


{
for(j = i + 1; j < number; j++)
{
if(a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
 Complexity Analysis of Selection Sorting

Worst Case Time Complexity : O(n2)


Best Case Time Complexity : O(n2)
Average Time Complexity : O(n2)
Space Complexity : O(1)

Advantages - Selection Sort

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.

Disadvantages - Selection Sort

Running time of Selection sort algorithm is very poor of 0 (n2).


Insertion sort algorithm is far better than selection sort algorithm.

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.

Example. Sort {5, 1, 12, -5, 16} using bubble sort.


Algorithm:-

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.

1. It has one of the simplest implementation


2. It is efficient for smaller data sets, but very inefficient for larger lists.
3. Insertion Sort is adaptive, that means it reduces its total number of steps if given a
partially sorted list, hence it increases its efficiency.
4. It is better than Selection Sort and Bubble Sort algorithms.
5. Its space complexity is less, like Bubble Sorting, inerstion sort also requires a single
additional memory space.
6. It is Stable, as it does not change the relative order of elements with equal keys

 How Insertion Sorting Works

 Sorting using Insertion Sort Algorithm :-

int a[6] = {5, 1, 6, 2, 4, 3};


int i, j, key;
for(i=1; i<6; i++)
{
key = a[i];
j = i-1;
while(j>=0 && key < a[j])
{
a[j+1] = a[j];
j--;
}
a[j+1] = key;
}
Advantages:-
 The main advantage of the insertion sort is its simplicity.
 It also exhibits a good performance when dealing with a small list.
 The insertion sort is an in-place sorting algorithm so the space requirement is
minimal.

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.

 Complexity Analysis of Insertion Sorting

Worst Case Time Complexity : O(n2)


Best Case Time Complexity : O(n)
Average Time Complexity : O(n2)
Space Complexity : O(1)

Merge Sort :-

 MergeSort is a Divide and Conquer algorithm.


 In merge sort the unsorted list is divided into N sublists, each having one element,
because a list of one element is considered sorted. Then, it repeatedly merge these
sublists, to produce new sorted sublists, and at lasts one sorted list is produced.
 Merge Sort is quite fast, and has a time complexity of O(n log n). It is also a stable sort,
which means the "equal" elements are ordered in the same order in the sorted list.

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.

Algorithm for Merge Sort :-

MERGE_SORT(arr, beg, end)

if beg < end


set mid = (beg + end)/2
MERGE_SORT(arr, beg, mid)
MERGE_SORT(arr, mid + 1, end)
MERGE (arr, beg, mid, end)
end of if

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

Worst Case Time Complexity : O(n log n)


Best Case Time Complexity : O(n log n)
Average Time Complexity : O(n log n)
Space Complexity : O(n)

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 algorithm is a non-comparative sorting algorithm in computer science. It avoids


comparison by creating and categorizing elements based on their radix. For elements with more
than one significant digit, it repeats the bucketing process for each digit while preserving the
previous step's ordering until all digits have been considered.
Radix sort is one of the linear sorting algorithms for integers. It functions by sorting the input
numbers on each digit, for each of the digits in the numbers. However, the process adopted by
this sort method is somewhat counterintuitive, in the sense that the numbers are sorted on the
least-significant digit first, followed by the second-least significant digit and so on till the most
significant digit.

What Is a Radix Sort Algorithm?

 Radix Sort is a linear sorting algorithm.

 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.

 It is not an in-place sorting algorithm because it requires extra space.

 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:

493 812 715 710 195 437 582 340 385

We should start sorting by comparing and ordering the one's digits:

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:

340 710 812 582 493 715 195 385 437

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

Now the sublists are gathered in order from 0 to 9:

710 812 715 437 340 582 385 493 195


Finally, the sublists are created according to the hundred's digit:

Digit Sublist
0
1 195
2
3 340 385
4 437 493
5 582
6
7 710 715
8 812
9

At last, the list is gathered up again:

195 340 385 437 493 582 710 715 812

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.

Advantages Radix Sort Algorithm

Following are some advantages of the radix sorting algorithm:

 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.

Disadvantages of Radix Sort Algorithm

Following are some disadvantages of the radix sorting algorithm:

 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 has a higher constant than other sorting algorithms.

 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:-

 The average case time complexity of Radix sort is θ(nk).

You might also like