AAD Unit3
AAD Unit3
AAD Unit3
It operates on
Easy to Easy to Time
both sorted and
understand. implement complexity ?
unsorted list
Linear Search: Disadvantages
Linear search is not efficient when list is large
Step 1
• The size of the input array is 10. The smallest Fibonacci number greater than 10 is 13.
• Therefore, Fk = 13, Fk-1 = 8, Fk-2 = 5.
• We initialize offset = -1
Time Complexity
• Best Case: O(1), when the target element is at the Fibonacci
index being checked.
• Average and Worst Case: 𝑂(log𝑛), similar to Binary Search, as
Fibonacci numbers grow exponentially and the array is
divided logarithmically.
Fibonacci Search: Pros
Efficient for Large Arrays: Like Binary Search, it works well for
large sorted arrays, offering logarithmic time complexity.
Less Dependent on Uniform Distribution: Fibonacci Search
doesn't require the array to be evenly distributed, making it
more adaptable than Interpolation Search.
Better for Unknown or Large Array Sizes: It doesn’t require
knowledge of the array size in advance, which can be an
advantage in some scenarios.
Fibonacci Search: Cons
DEMO:
https://www.w3schools.com/dsa/dsa_algo_bubblesort.php
Bubble Sort: Properties
• Stable
• In-place
• Internal sort
• O(n2): Time complexity
Bubble Sort: Example
• Given the following array. Sort it using bubble sort and show
array contents after each pass.
Selection Sort
Selection Sort
Demo:
https://www.w3schools.com/dsa/dsa_algo_selectionsort.php
Selection Sort: Example
• Given the following array. Sort it using selection sort and
show array contents after each pass.
Selection Sort
• Time complexity: O(n2)
• Properties?
Insertion Sort
• Insertion sort is a simple sorting algorithm that works by
iteratively inserting each element of an unsorted list into its
correct position in a sorted portion of the list.
• It is a stable sorting algorithm, meaning that elements with
equal values maintain their relative order in the sorted
output.
• It is considered an ” in-place ” sorting algorithm, meaning it
doesn’t require any additional memory space beyond the
original array.
Insertion Sort
STEPS
1. We start with second element of the array as first element
in the array is assumed to be sorted.
2. Compare second element with the first element and check if
the second element is smaller then swap them.
3. Move to the third element and compare it with the second
element, then the first element and swap as necessary to
put it in the correct position among the first three
elements.
4. Continue this process, comparing each element with the
ones before it and swapping as needed to place it in the
correct position among the sorted elements.
5. Repeat until the entire array is sorted.
Insertion Sort: Cards Example
Insertion Sort
Insertion Sort
Shell Sort
• Shell sort is mainly a variation of Insertion Sort.
• In insertion sort, we move elements only one position ahead.
When an element has to be moved far ahead, many
movements are involved.
• The idea of Shell Sort is to allow the exchange of far items.
• In Shell sort, we make the array h-sorted for a large value of
h.
• We keep reducing the value of h until it becomes 1.
• An array is said to be h-sorted if all sublists of every h’th
element are sorted.
Shell Sort
Step 1 − Start
Step 2 − Initialize the value of gap size, say h.
Step 3 − Divide the list into smaller sub-part. Each must have
equal intervals to h.
Step 4 − Sort these sub-lists using insertion sort.
Step 5 – Repeat this step 2 until the list is sorted.
Step 6 – Print a sorted list.
Step 7 – Stop.
Shell Sort
The interval between the elements is reduced based on the
sequence used. Some of the optimal sequences that can be used
in the shell sort algorithm are:
Shell Sort: Example
Shell Sort: Example
Shell Sort: Example
Shell Sort: Example
Shell Sort
Shell Sort: Analysis
1. Best Case:
• Time Complexity: O(nlogn)
• Explanation:
• The best case occurs when the array is already sorted, or nearly sorted.
• In this situation, the algorithm requires fewer comparisons and shifts as the elements
are already in their correct positions.
• The insertion sort portion of Shell Sort will have a minimal number of operations, leading
to a logarithmic number of gap comparisons.
Shell Sort: Analysis
2. Average Case:
• Time Complexity: O(n3/2)
• Explanation:
• On average, the algorithm will perform more operations than in the best case but fewer than in the
worst case.
• The average case performance is generally better than quadratic time complexity (i.e.O(n^2)) because
the initial large gaps allow for efficient element movement early in the sorting process.
3. Worst Case:
• Time Complexity: O(n^2)
• Explanation:
• The worst case occurs in situations where the gap sequence doesn't adequately address the ordering of
elements, leading to many comparisons and shifts.
• An example of a worst-case scenario could be when the elements are in reverse order or in some specific
patterns that result in poor performance.
• In this case, the algorithm performs like an insertion sort with a time complexity of O(n^2), as the
smaller gaps (especially the final gap of 1) need to sort nearly the entire array.
Efficiency: Shell sort has faster average-case
performance than many other sorting algorithms such
as bubble sort and selection sort.
Disadvantages
It is not a good choice for small data sets. of Quick Sort:
• Time Complexity:
• Best Case: O(n log n), When the array is already sorted or nearly sorted.
• Average Case: O(n log n), When the array is randomly ordered.
• Worst Case: O(n log n), When the array is sorted in reverse order.
• Auxiliary Space: O(n), Additional space is required for the temporary
array used during merging.
Stability : Merge sort is a stable sorting algorithm,
which means it maintains the relative order of equal
elements in the input array.
Pass 1:
Radix Sort
Pass 2:
Radix Sort
Pass 3:
Final Sorted Array
Radix sort complexity
• Best Case Complexity –
It occurs when there is no sorting required, i.e. the array is
already sorted. The best-case time complexity of Radix sort is
Ω(n+k).
• Average Case Complexity –
It occurs when the array elements are in jumbled order that is
not properly ascending and not properly descending. The
average case time complexity of Radix sort is θ(nk).
• Worst Case Complexity –
It occurs when the array elements are required to be sorted in
reverse order. That means suppose you have to sort the array
elements in ascending order, but its elements are in descending
order. The worst-case time complexity of Radix sort is O(nk).
Counting sort
• Counting Sort is a non-comparison-based sorting
algorithm.
• It is particularly efficient when the range of input
values is small compared to the number of elements to
be sorted.
• The basic idea behind Counting Sort is to count the
frequency of each distinct element in the input array
and use that information to place the elements in their
correct sorted positions.
Counting sort: Algorithm
1. Declare an auxiliary array countArray[] of
size max(inputArray[])+1 and initialize it with 0.
2. Traverse array inputArray[] and map each element of inputArray[] as
an index of countArray[] array,
i.e.,execute countArray[inputArray[i]]++ for 0 <= i < N.
3. Calculate the prefix sum at every index of array inputArray[].
4. Create an array outputArray[] of size N.
5. Traverse array inputArray[] from end and update outputArray[
countArray[ inputArray[i] ] – 1] = inputArray[i]. Also,
update countArray[ inputArray[i] ] = countArray[ inputArray[i] ]- – .
Counting sort: Example
Bucket sort
• Bucket sort is a sorting technique that involves dividing
elements into various groups, or buckets formed by
uniformly distributing the elements.
• Once the elements are divided into buckets, they can be
sorted using any other sorting algorithm (scatter-gather
approach)
• Especially used in the case of floating-point numbers or
when the input is uniformly distributed
• Finally, the sorted elements are gathered in an ordered
fashion.
Bucket sort: Algorithm
Bucket Sort(A[])
1. Let B[0....n-1] be a new array
2. n=length[A]
3. for i=0 to n-1
4. make B[i] an empty list
5. for i=1 to n
6. do insert A[i] into list B[n*a[i]]
7. for i=0 to n-1
8. do sort list B[i] with insertion-sort
9. Concatenate lists B[0], B[1],........, B[n-1] together in order
End
Bucket sort: Example
Sorted Array:
Comparative Analysis