Docsity

Algorithmic Solutions: Bubble, Insertion, Selection, Merge, and Quick Sort, Slides of Physics of semiconductor devices

Partial preview of the text

Download Algorithmic Solutions: Bubble, Insertion, Selection, Merge, and Quick Sort and more Slides Physics of semiconductor devices in PDF only on Docsity!

Module - III Searching - Linear Search and binary search, Applications - Finding square root of ‘n’-Longest Common Prefix Sorting – Insertion sort - Selection sort – Bubble sort – (Counting Sort) - Quick sort- Merge sort , Analysis, Applications - Finding the ‘n’ closest pair’s

By

Dr Shola Usharani

SCOPE,VIT

Types of Searching

Binary Search

Applications of Searching

BY

Dr Shola Usha rani

SCOPE VIT

Finding square root of ‘n

  • (^) Input: x = 4
    • (^) Output: 2
  • (^) Explanation: The square root of 4 is 2.
  • (^) Input: x = 11
    • (^) Output: 3
  • (^) Explanation: The square root of 11 lies in

between 3 and 4 so floor of the square root is

Algorithm

  • (^) Create a variable (counter) i and take care of some base cases, i.e when the given number is 0 or 1.
  • (^) Run a loop until i*i <= n , where n is the given number. Increment i by 1.
  • (^) The floor of the square root of the number is i – 1 - (^) // Returns floor of square root of x - (^) int floorSqrt(int x) - (^) { // Base cases - if (x == 0 || x == 1) - (^) return x; - (^) // Staring from 1, try all numbers until - (^) // i*i is greater than or equal to x. - (^) int i = 1, result = 1; - (^) while (result <= x) - (^) { - i++; - (^) result = i * i; - (^) } - (^) return i - 1; - (^) }

Better Approach

  • (^) The idea is to find the largest integer i whose

square is less than or equal to the given

number. The idea is to use Binary Search to

solve the problem. The values of i * i is

monotonically increasing, so the problem can

be solved using binary search.

Sample Code

  • int floorSqrt(int x)
  • { if (x == 0 || x == 1)
  • (^) return x;
  • (^) // Do Binary Search for floor(sqrt(x))
  • (^) int start = 1, end = x, ans;
  • (^) while (start <= end)
  • (^) {
  • (^) int mid = (start + end) / 2;
  • (^) // If x is a perfect square
  • (^) if (mid*mid == x)
  • (^) return mid;
  • // Since we need floor, we update answer when mid*mid is // smaller than x, and move closer to sqrt(x)
  • (^) if (mid*mid < x)
  • {
  • (^) start = mid + 1;
  • (^) ans = mid;
  • }
  • (^) else // If mid*mid is greater than x
  • (^) end = mid-1;
  • } return ans; }

Longest Common Prefix

  • (^) Given a set of strings, find the longest

common prefix.

  • (^) Input : {“geeforgees”, “geekers”, “geek”, “geezer”}
    • (^) Output : "gee"
  • (^) Input : {"apple", "ape", "april"}
    • (^) Output : "ap"
  • (^) Input : {"abcd"}
    • (^) Output : "abcd"

Sorting algorithm classification

  • (^) Sorting by exchange
  • (^) Sorting by insertion
  • (^) Sorting by distribution
  • (^) Sorting by selection

Types of sorting

  • (^) Internal sorting
    • (^) Memory required is small
    • (^) The memory for sorting can be accommodated in the internal memory of the computer.
  • (^) External sorting
    • (^) Memory required is huge.
    • (^) The memory for sorting can be required in the external storage devices of the computer like disks.

Bubble Sort

  • (^) The family of sorting by exchange or transposition
  • (^) pairs of elements that are out of order are interchanged until the whole list is ordered - (^) L = {K1,K2, K3, ……Kn} bubble sort orders the elements in their ascending order (i.e.), L= { K1 ,K2 ,K 3 , ... Kn}, K 1 ≤ K2 ≤ ... Kn.
  • (^) L = {K1,K2, K3, ……Kn} unordered list
  • (^) bubble sort compares pairs of elements Ki and Kj swapping them if Ki > Kj.
  • (^) At the end of the first pass of comparisons, the largest element in the list L moves to the last position in the list.
  • (^) In the next pass, the sublist {K1 , K2 , K3 , ... Kn-1 } is considered for sorting.
  • (^) Once again the pair wise comparison of elements in the sub list results in the next largest element floating to the last position of the sublist.
  • (^) Thus in (n–1) passes where n is the number of elements in the list, the list L is sorted