Data Structures and Algorithm

0.0(0)
studied byStudied by 9 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/43

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

44 Terms

1
New cards

Sorting Algorithms

  • Bubble

  • Selection

  • Insertion

  • Shell

  • Heap

  • Merge

  • Quick

  • Bucket

  • Radix

2
New cards

Divide and Conquer Algorithms

  • Merge

  • Quick

3
New cards

Bubble Sort

  • Simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order

  • Right side sorting

4
New cards

Bubble Sort Complexity

  • Best - O(n)

  • Worst - O(n^2)

  • Average - O(n^2)

5
New cards

Bubble Sort Algorithm

knowt flashcard image
6
New cards

Use Cases

  • Best - Ω

  • Worst - O

  • Average - Θ

7
New cards

Common Time Complexity Symbols

  • O(1) - constant

  • O(log n) - logarithmic time

  • O(n) - linear time

  • O(n log n) - log-linear time

  • O(n^2) - quadratic time

  • O(2^n) - exponential time

    O(n!) - factorial time

8
New cards

Selection Sort

  • Repeatedly finding the minimum element considering ascending order) from the unsorted part and putting it at the beginning

  • Left side sorting

9
New cards

Selection Sort Complexity

  • Best - O(n^2)

  • Worst - O(n^2)

  • Average - O(n^2)

10
New cards

Selection Sort Algorithm

knowt flashcard image
11
New cards

Insertion Sort

  • In-place comparison-based sorting algorithm

  • Left side sorting

  • Bubble sort, but only for one element at a time

12
New cards

Insertion Sort Complexity

  • Best - O(n)

  • Worst - O(n^2)

  • Average - O(n^2)

13
New cards

Insertion Sort Algorithm

knowt flashcard image
14
New cards

Shell Sort

  • Long-distance insertion sort

15
New cards

Shell Sort Complexity

  • Best - O(n log n)

  • Worst - O(n^2)

  • Average - O(n^1.5)

16
New cards

Shell Sort Algorithm


  1. Choose a gap sequence: Start with a large gap and progressively reduce it. A common sequence is n/2, n/4, ..., 1, where n is the size of the array

  2. Perform Insertion Sort for each gap: For each gap, perform an Insertion Sort where elements that are gap positions apart are compared and swapped

  3. Repeat until the gap becomes 1, and the array is sorted

17
New cards

Shell Sort Optimal Sequences for Gaps

  • Original - n/4, n/4, ..., 1

  • Knuth - (3^n - 1)/2

  • Sedgewick -1, 5, 19, 41, 109, ... (formula: 4^k + 3 * 2^k + 1)

  • Hibbard - 1 + 2^0, 1 + 2^2, 1 + 2^4, ...

  • Papernov & Stasevich - 2^k - 1

  • Pratt - 1, 3, 5, 7, 9, 11, ... (all numbers of the form 2^i * 3^j)

18
New cards

Heap Sort

  • Improved selection sort

  • Tree node for two child nodes

19
New cards

Heap Sort Complexity

  • Best - O(n log n)

  • Worst - O(n log n)

  • Average - O(n log n)

20
New cards

Heap Sort Algorithm

  1. Build a Max Heap: Rearrange the array into a binary heap structure, where the root node is the largest element

  2. Swap the root with the last element: Once the heap is built, swap the root (maximum) element with the last element in the array

  3. Reduce the heap size: After swapping, the last element is considered sorted, so reduce the heap size by one

  4. Heapify the root: Restore the heap property by heapifying the root element, ensuring the subtree rooted at the root is still a valid max heap

  5. Repeat until the heap size is 1

21
New cards

Merge Sort

  • Divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves

  • Uses a merge function to merge two halves

22
New cards

Merge Sort Complexity

  • Best - O(nLogn)

  • Worst - O(nLogn)

  • Average - O(nLogn)

23
New cards

Merge Sort Algorithm

knowt flashcard image
24
New cards

Quick Sort

  • Picks an element as a pivot and partitions the given array around the picked pivot

25
New cards

Quick Sort Complexity

  • Best - O(n log n)

  • Worst - O(n^2)

  • Average - O(n log n)

26
New cards

Quick Sort Algorithm

<p></p><p></p>
27
New cards

Quick Sort Pivots

  • First element as a pivot

  • Last element as the pivot

  • Random element as a pivot

  • Median as a pivot.

28
New cards

Bin/Bucket Sort

  • Divides the array into several buckets with ranges

29
New cards

Bin/Bucket Sort Algorithm

knowt flashcard image
30
New cards

Bin/Bucket Sort Complexity

  • Best - O(n + buckets)

  • Worst - O(n^2)

  • Average - O(n + buckets)

31
New cards

Queue

  • An n ordered list in which all insertions take place at one end, the rear, while all deletions take place at the other end, the front

  • Linear data structure

  • FIFO structure

32
New cards

Enqueue

  • Inserts queue element to the right (back, end)

33
New cards

Dequeue

  • Removes queue element from the left (front, start)

34
New cards

enqueue()

public void enqueue(T data)

35
New cards

dequeue()

public T dequeue()

36
New cards

peek()

public T peek()

37
New cards

isEmpty()

public boolean isEmpty()

38
New cards

size()

public int size()

39
New cards

Radix Sort

  • Used to sort a list of integer numbers in order

  • Counts digits, then compares per digit, starting from the ones digit

  • Replaces blanks with zeroes (Ex. 1, 12, 150 → 001, 012, 150)

40
New cards

Radix Sort Complexity

  • Best - O(n * digits)

  • Worst - O(n * digits)

  • Average - O(n * digits)

41
New cards

Radix Sort Algorithm

  1. Define 10 queues each representing a bucket for each digit from 0 to 9

  2. Consider the least significant digit of each number in the list which is to be sorted

  3. Insert each number into their respective queue based on the least significant digit

  4. Group all the numbers from queue 0 to queue 9 in the order they have inserted into their respective queues

  5. Repeat from step 3 based on the next least significant digit

  6. Repeat from step 2 until all the numbers are grouped based on the most significant digit

42
New cards

Queue

Implements Linked-List

43
New cards

Types of Queue

  • Queue

  • Priority Queue

44
New cards

Priority Queue

  • Binary heap queue