Hash Table Operations

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the purpose of step 1 in the partitioning strategy?

  • To sort the entire array
  • To compare the pivot with the first item
  • To swap the pivot with the last item (correct)
  • To initialize the pointers i and j

What happens when i and j cross in the partitioning strategy?

  • The array is partially sorted
  • The algorithm terminates
  • The pivot element is swapped with the element pointed to by i (correct)
  • The algorithm restarts from step 1

What is the purpose of steps 3 and 4 in the partitioning strategy?

  • To sort the entire array
  • To find the median of the array
  • To partition the array around the pivot (correct)
  • To count the number of elements in the array

What is the initial value of i in the partitioning strategy?

<p>The first item in the array (D)</p> Signup and view all the answers

What is the condition for swapping elements in step 4?

<p>If i is to the left of j (D)</p> Signup and view all the answers

What happens when i and j meet in the partitioning strategy?

<p>The pivot element is swapped with the element pointed to by i (B)</p> Signup and view all the answers

What is the requirement for the input in external sorting?

<p>The input must fit into main memory (A)</p> Signup and view all the answers

What is the purpose of the partitioning strategy?

<p>To partition the array around the pivot (B)</p> Signup and view all the answers

What happens in step 2 of the partitioning strategy?

<p>The pointers i and j are initialized (C)</p> Signup and view all the answers

What is the result of the partitioning strategy?

<p>An array with the pivot element in its final position (C)</p> Signup and view all the answers

Flashcards are hidden until you start studying

Study Notes

Hash Table and Hash Function

  • To insert an object:
    • Compute its hash code
    • Check if it exists in the appropriate list
    • If not, insert at the front of the list
  • Disadvantages of using linked lists:
    • Slows down the algorithm
    • Requires implementing another data structure
  • Open Addressing techniques:
    • Linear Probing: sequentially search the table until an empty location is found
    • Quadratic Probing: uses a collision resolution technique to avoid primary clustering
    • Double Hashing: uses a second hash function when the result of the hash function results in a collision

Linear Probing

  • When a collision occurs, sequentially search the table until an empty location is found
  • Example: suppose the hash function hashes the key 343-567 (SEU Dammam Branch) into index #4 which is already occupied by the key 343-567 (SEU Medina Branch)
  • Solution: sequentially search for an empty location (e.g., index #5 is occupied, so move to index #6)

Quicksort

  • Strategies for selecting the pivot:
    • First element
    • Last element
    • Arbitrary
    • Median of three values
  • Selecting the first value as a pivot is acceptable if the input is random
  • Selecting the pivot randomly is a safe strategy
  • The best strategy of selecting a pivot would be the median of the array items
  • Problems:
    • Hard to calculate
    • Slows down the quicksort
  • Solution: calculate the median of three items (first, last, and middle elements)

Heapsort

  • A heap is a binary tree that fulfills two conditions:
    • It is completely filled, with the possible exceptions of the last level
    • The tree fulfills the heap property (min-heap or max-heap)
  • Using min-heap to sort a set of elements yields decreasing sorted order
  • Using max-heap to sort a set of elements yields increasing sorted order
  • Steps to store a heap into an array:
    1. Arrays start at index 0, but we start at index 1 for binary heaps
    2. Compute the indices of item's parent, left child, and right child

Mergesort

  • Step 4: continue to merge parts until merging all parts into one sorted list
  • Example: merging two sorted arrays X{8, 12, 14, 17} and Y{9, 16, 19, 20} into array Z{}
  • Algorithm: mergesort(data[], firstItem, lastItem)
    • If firstItem < lastItem, then
      • middleItem = (firstItem + lastItem) / 2
      • mergesort(data[], firstItem, middleItem)
      • mergesort(data[], middleItem +1, lastItem)
      • merge(data[], firstItem, lastItem)

Quicksort (Partitioning Strategy)

  • Quicksort is a fast sorting algorithm in practice
  • Average running time for quicksort is O(N log N)
  • Worst-case running time for quicksort is O(N2)
  • Quicksort orders values by partitioning the list around one element, then sorting each partition
  • Partitioning strategy:
    • Step 1: Swap the pivot with the last item
    • Step 2: Let i pointing to the first item and j pointing to next-to-last item
    • Step 3: While i is to the left of j, move i right, skip over elements that are smaller than the pivot
    • Step 4: When i and j have stopped, i is pointing at a large element and j is pointing at a small element
    • Step 5: Repeat steps 3 and 4 until i and j crossed
    • Step 6: When i and j crossed, swap the pivot element with the element pointed to by i

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

More Like This

CS223: Data Structures - Hash Tables
24 questions
Sorting Algorithms and Hash Tables
25 questions
Hash Tables and Algorithm Analysis
5 questions
Use Quizgecko on...
Browser
Browser