Introduction to Data Structure & Algorithms in Java - Part 3

Introduction to Data Structure & Algorithms in Java - Part 3

ExerciseFiles
https://www.linkedin.com/ambry/?x-li-ambry-ep=AQJuXwYvQivLLAAAAYRGHkXTfeCeLz3Ko5Psr99rchpRWRUBtEbma8wikGuGHLlPc1JfpWAA0y7FJdEzqoy4MwU-M8zzq2JKP_Sfwqy31Tn9MIt7uLt7MsbjYkW0NGuH2adQBZYMnVc5DWOI088FKndVcxaUq7fr6fSEISVs6iXRobZBIHrc7gpChAiOW3QKMfw-DGOA6KvC6phXeXymVGwqH82P_AQyt-zT87nlJZVd846T6dUez5BoIairA4gAHzjywckPnAiyi75WPeSz3vxntglImuuIBK-vDFHwJigv7IbXyUZ70FPXlwoJGG90KO5DfJMmKV84QViqJjmtRzqJcu3EcCru7c1gfFGjuF5rZnVfYaimKQr11b9h62wkM5-5TPIVR6uKmZs2qJbria4bTGXW57ro3dwxDcPXDNtf5ATg_DNrW9FBQGbd8s4yMCJS_T706fJ617aCGTfkVZybQcOIIs7_jn0m-ko1Zk8SEZxpm_ZWM7uKx-4x-2rmdt4i_mgk-F50Z4J2jHKKVmgD572k-JU4DMYPG9MRdhs1D8x2VXu1dinFN_lZZq4gv-TOZ6C6HZ8imVyWyGtJJr3XQaNYgj4qKf1bscct67t5ttg
Tags
Data Structure
Algorithm
Java
Published

Other Sorting Algorithms

Introduction

  • Quick short
  • Shell sort
  • Counting sort
  • Radix sort
  • Bucket sort

Quicksort

  • Worse case: O(
  • Average case: O()
notion image
notion image
notion image
Quicksort(A, start, end) if start < end pivot = Partition(A, start, end) Quicksort(A, start, pivot-1) Quicksort(A, pivot + 1, end)

Quicksort: The partitioning step

notion image
Partition(A, start, end) pivot = A[end] i = start for j = start to end - 1 if A[j] <= pivot exchange A[i] with A[j] i += 1 exchange A[i] with A[end] return i

Shell sort

notion image
  • Assume we are sorting this array using the Insertion sort
  • After a few steps, we have the left items sorted
  • Now, we need to insert 10 into the left-sorted items
  • We have to move all those sorted items to the right
notion image
  • Instead of sorting all items, we do sort a few items with a gap = 3
  • And continue sorting with other items with gap = 3 until gap times
  • How do we choose the gap?
notion image
  • we often choose a big gap, ex: 121
  • Then we reduce the gap until 1

Shell sort example

notion image
notion image
  • Assume we have array with 100 items need to be sorted
  • We choose gap = 40
  • So, there are 3 items at index 0, 40, and 80 will be sorted
  • Then loop them for other items with same gap 40
  • After first iteration, the result looks like this one on the left
notion image
  • Next iteration, we reduce the gap to 13
  • And do same for gap = 4
notion image
  • In the last iteration, the gap = 1
  • We will sort all items
  • We often choose insertion sort
  • shell sort time complexity: O() - O()

Counting sort

Sorting in Learning Time
  • Counting sort
  • Radix sort
  • Bucket sort or bin-sort
notion image
notion image
  • Assume we need to sort the marks of students
  • We know that the values of marks are only from 0 to 10
notion image
  • n is number of items of array need to be sorted
  • k is distinct values of items in the array and k very very small compared to n
  • O(n)

Radix sort

notion image
  • Assume we have 6 items
  • Each item have 3 digits
  • First, we sort items by unit digits
  • Then, we sort items by ten digits
  • Finally, we sort items by hundreds digits
  • O(n)

Bucket sort

notion image
  • we found that data need to sort are only up to hundreds
    notion image
    • Go through linked list to sort them

    Heaps

    Introduction

    notion image
    • This is complete binary tree
    notion image
    • This is an incomplete binary tree
    notion image
    • A heap is a binary tree which is
      • complete
      • every node’s key is larger than or equal to the keys of its children
      •  

    Delete the root

    notion image
    • Delete the root is very often if the heap is priority queue
    notion image
    notion image
    notion image
    notion image

    Inserting an item in a heap

    notion image
    notion image
    notion image
    notion image
    notion image
    notion image

    Heaps as priority queues

    notion image
    • Assume we change key from 10 to 25
    • We need to fix the heap
    • Time complexity to fix it is O()

    Representing heaps using arrays

    notion image

    Heap sort

    notion image
    notion image

    Building a heap

    notion image
    notion image

    Hashtables

    Introduction

     
    notion image
    notion image
    notion image

    Direct access tables

    notion image
    notion image

    Hashing

    notion image
    notion image

    Resolving collisions through chaining

    notion image
    notion image
    notion image

    The hash function

    notion image
    notion image
    notion image
    notion image

    Open addressing to resolve collisions

    notion image
    notion image
    notion image

    Strategies for open addressing

    notion image
    notion image
    notion image
    notion image

    Time complexity: Open addressing

    notion image
     
     

    References