Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Searching and Sorting

Unit 2
Index

 Searching
Linear Search
Binary Search
 Sorting
 Bubble Sort
 Selection Sort
 Insertion Sort
 Quick Sort
 Radix Sort
Array

 Array is a container which can hold fix number of items and these items
should be of same type.
 Most of the data structure make use of array to implement their algorithms.
Following are Important terms in concepts of Array.
 Element − Each item stored in an array is called an element.
 Index − Each location of an element in an array has a numerical index which is
used to identify the element.
 Array Representation
Operations on array

Basic Operations Following are the basic operations supported by


an array.
 Traverse − print all the array elements one by one.
 Insertion − add an element at given index.
 Deletion − delete an element at given index.
 Search − search an element using given index or by value.
 Update − update an element at given index
Linear Search

 Linear search is a very simple search algorithm. In this type of


search, a sequential search is made over all items one by one. Every
item is checked and if a match is found then that particular item is
returned, otherwise the search continues till the end of the data
collection.
 Time complexity is O(n). At the most n comparisons are required to
search any data element in an array.
Linear Search
 Algorithm:
 Let Array[MAX] is array, N is size of array and item is element to be searched.
 Step 1: Repeat for i=0 to N-1.
 Step 2: If Array[i]==item.
Step 3: Print index and return from loop.
 Step 4: if i==N.
 Step 5: Print element not found
 Step 6: Exit
Binary Search
 This search algorithm works on the principle of divide and conquer.
For this algorithm to work properly, the data collection should be in
the sorted form.
 Binary search looks for a particular item by comparing the middle
most item of the collection. If a match occurs, then the index of item is
returned. If the middle item is greater than the item, then the item is
searched in the sub-array to the right of the middle item. Otherwise,
the item is searched for in the sub-array to the left of the middle item.
This process continues on the sub-array as well until the size of the
subarray reduces to zero.
 Binary search is a fast search algorithm with run-time complexity of
Ο(log n). At the most n/2 comparisons will be performed to search a
data element in an array.
Binary Search
 Let Array[MAX] is array, N is size of array and item is an element to be searched.
 Step 1: Set first=0, last=n, mid=(first=last)/2.
 Step 2: Repeat step 3&4 while first<=last and Array[mid]!=item.
 Step 3: If item>Array[mid] then
Set first=mid+1
Else if item==Array[mid] then
Print ”item found”
Else set Last=mid-1
 Step 4: Set mid=(first+last)/2
 Step 6: If first>last then print item not found.
Binary Search
 For a binary search to work, it is mandatory for the target array to
be sorted. The following is our sorted array and let us assume that
we need to search the location of value 31 using binary search.

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

 Now we compare the value stored at location 4, with the value being searched, i.e.
31.We find that the value at location 4 is 27, which is not a match. As the value is
greater than 27 and we have a sorted array, so we also know that the target value
must be in the upper portion of the array.
Binary Search
 We change our low to mid + 1 and find the new mid value again.
low = mid + 1. mid = low + (high - low) / 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 than what we are looking for. So, the
value must be in the lower part from this location.

 Hence, we calculate the mid again. This time it is 5.

 We compare the value stored at 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. Binary search halves the searchable
items and thus reduces the count of comparisons to be made to very less numbers.
Radix Sort

 Radix sort is one of the sorting algorithms used to sort a list of integer numbers in
order.
 In radix sort algorithm, a list of integer numbers will be sorted based on the digits of
individual numbers.
 Sorting is performed from least significant digit to the most significant digit..
 Radix sort algorithm requires the number of passes which are equal to the number of
digits present in the largest number among the list of numbers.
 For example, if the largest number is a 3 digit number then that list is sorted with 3
passes
Radix Sort
 Step by Step Process
 The Radix sort algorithm is performed using the following steps...
• Step 1 - Define 10 queues each representing a bucket for each digit from 0 to 9.
• Step 2 - Consider the least significant digit of each number in the list which is to be
sorted.
• Step 3 - Insert each number into their respective queue based on the least significant
digit.
• Step 4 - Group all the numbers from queue 0 to queue 9 in the order they have inserted
into their respective queues.
• Step 5 - Repeat from step 3 based on the next least significant digit.
• Step 6 - Repeat from step 2 until all the numbers are grouped based on the most
significant digit.
.
Bubble Sort Selection Sort Insertion Sort Quick Sort Radix sort
. It repeatedly compares  It selects the smallestIt is a simple It is a divide and Radix Sort is a non-
and swaps(if needed) element from unsorted comparison based conquer approach with comparative sorting
adjacent elements in subarray and places in sorting algorithm. It recurrence relation:  algorithm with
every pass. In i-th the first position of inserts every array The algorithm picks a asymptotic complexity
pass of Bubble Sort that subarray element into its proper pivot element, A[q] O(nd). It is one of the
(ascending order), last (ascending order). It position. In i-th and then rearranges most efficient and
(i-1) elements are repeatedly selects the iteration, previous (i-1) the array into two fastest linear sorting
already sorted, and i- next smallest elements (i.e. subarray subarrays A[p . . . . q- algorithms. Radix sort
th largest element is element.  Arr[1:(i-1)]) are 1] , such that all was developed to sort
placed at (N-i)-th already sorted, and the elements are less than large integers. As
position, i.e. i-th last i-th element (Arr[i]) is A[q] and A[q+1 . . . r] , integer is treated as a
position. inserted into its proper such that all elements string of digits so we
place in the previously are greater than or can also call it as string
sorted subarray.  equal to A[q]. sorting algorithm.
 A temporary variable A temporary variable is A temporary variable is in-place algorithm. Radix sort is not in-
is used in swapping [ used in swapping used in swapping place algorithm as it
auxiliary, O(1) ]. Hence {auxiliary, O(1) ]. In- {auxiliary, O(1) ]. In- requires extra memory
it is In-Place sort.  Place sort.  Place sort.  for storing buckets

Bubble sort is Time complexity in all it is generally used Regarded as best Known as fastest algo
comparatively slower cases is O(N2), no best when the value of N is sorting algo. But same for numbers and even
algorithm. case scenario. small. For larger values as other algo in worst for string of letters
of N, it is inefficient.  case
.
Radix sort is not in-place algorithm as it requires extra memory for
storing buckets and it is stable.

in-place means that the algorithm does not use extra space for manipulating the input but may require a
small though nonconstant extra space for its operation.
Stable means that is, a sorting algorithm is stable if whenever there are two records R and
S with the same key and with R appearing before S in the original list, R will appear before
S in the sorted list. Stable sorting algorithms maintain the relative order of records with equal

You might also like