Professional Documents
Culture Documents
Unit2 Searching and Sorting
Unit2 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
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.
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