Study Notes

Sorting Algorithms Exam - Cheatsheet 1

Dainius Rimavicius
0 imports

Free ยท 2 imports included

Study Notes Preview

4 sections locked
Section 1

Sorting Algorithms Exam - Cheatsheet 1

STUDY GUIDE

๐ŸŽ“ Sorting Algorithms Exam - Study Guide

๐Ÿ“‹ Course Structure

code
๐Ÿ“š Data Structures and Algorithms โ”œโ”€โ”€ ๐Ÿ“– Chapter 1: Bubble Sort โ”‚ โ”œโ”€โ”€ ๐Ÿ”น Bubble Sort Algorithm and Principle โ”‚ โ”œโ”€โ”€ ๐Ÿ”น Bubble Sort Pseudocode and Complexity โ”‚ โ””โ”€โ”€ ๐Ÿ”น Bubble Sort Stability โ”œโ”€โ”€ ๐Ÿ“– Chapter 2: Insertion Sort โ”‚ โ”œโ”€โ”€ ๐Ÿ”น Insertion Sort Algorithm and Principle โ”‚ โ”œโ”€โ”€ ๐Ÿ”น Insertion Sort Pseudocode and Complexity โ”‚ โ””โ”€โ”€ ๐Ÿ”น Insertion Sort Stability โ”œโ”€โ”€ ๐Ÿ“– Chapter 3: Quick Sort โ”‚ โ”œโ”€โ”€ ๐Ÿ”น Quick Sort Algorithm and Principle โ”‚ โ”œโ”€โ”€ ๐Ÿ”น Quick Sort Pseudocode and Complexity โ”‚ โ””โ”€โ”€ ๐Ÿ”น Quick Sort Stability โ”œโ”€โ”€ ๐Ÿ“– Chapter 4: Merge Sort โ”‚ โ”œโ”€โ”€ ๐Ÿ”น Merge Sort Algorithm and Principle โ”‚ โ”œโ”€โ”€ ๐Ÿ”น Merge Sort Pseudocode and Complexity โ”‚ โ””โ”€โ”€ ๐Ÿ”น Merge Sort Stability โ””โ”€โ”€ ๐Ÿ“– Chapter 5: Radix Sort โ”œโ”€โ”€ ๐Ÿ”น Radix Sort Algorithm and Principle โ”œโ”€โ”€ ๐Ÿ”น Radix Sort Pseudocode and Complexity โ””โ”€โ”€ ๐Ÿ”น Radix Sort Stability
Section 2

๐Ÿ“– Chapter 1: Bubble Sort

What this chapter covers: This chapter introduces the Bubble Sort algorithm, a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. It explains the basic principle, pseudocode, time and space complexity, and stability of Bubble Sort. The examples illustrate how the algorithm works by repeatedly comparing and swapping adjacent elements until the largest element "bubbles" to its correct position.

๐Ÿ”‘ Essential Concepts & Formulas

Concept/FormulaDefinition/EquationWhen to UseQuick Check
Basic PrinciplePairwise comparison and swap of adjacent elements.Sorting small lists where simplicity is more important than efficiency.Check if the list is sorted after n-1 passes.
Time Complexity (Worst/Avg)O(n2)O(n^2)Sorting large, unsorted lists.Verify the number of comparisons grows quadratically with the size of the list.
Time Complexity (Best)O(n)O(n)Sorting nearly sorted lists.Check if the list is sorted in one pass.
Space ComplexityO(1)O(1)Situations where memory usage is a constraint.Verify no additional memory is used beyond the input array.
StabilityStableWhen maintaining the original order of equal elements is important.Check if elements with equal values maintain their relative order after sorting.

๐Ÿ› ๏ธ Problem Types

Type A: Sorting a list of numbers using Bubble Sort.

Setup: "When you encounter a list of numbers that needs to be sorted in ascending or descending order."

Method: Iterate through the list, comparing adjacent elements and swapping them if they are in the wrong order. Repeat this process until no more swaps are needed.

Example: Sort the list [5, 1, 4, 2, 8] using Bubble Sort.

  1. First pass: [1, 5, 4, 2, 8] -> [1, 4, 5, 2, 8] -> [1, 4, 2, 5, 8] -> [1, 4, 2, 5, 8]
  2. Second pass: [1, 4, 2, 5, 8] -> [1, 2, 4, 5, 8] -> [1, 2, 4, 5, 8] -> [1, 2, 4, 5, 8]
  3. Third pass: [1, 2, 4, 5, 8] -> [1, 2, 4, 5, 8] -> [1, 2, 4, 5, 8] -> [1, 2, 4, 5, 8]
  4. Fourth pass: [1, 2, 4, 5, 8] -> [1, 2, 4, 5, 8] -> [1, 2, 4, 5, 8] -> [1, 2, 4, 5, 8] Sorted list: [1, 2, 4, 5, 8]

Type B: Determining the number of passes required for Bubble Sort to sort a list.

Setup: "If presented with a list of numbers and asked to determine the number of passes required to sort it using Bubble Sort."

Method: The maximum number of passes required is n-1, where n is the number of elements in the list. However, if the list is already sorted or becomes sorted before n-1 passes, the algorithm can terminate earlier.

Example: Determine the number of passes required to sort the list [1, 2, 3, 4, 5] using Bubble Sort. Since the list is already sorted, Bubble Sort will only require one pass to verify that no swaps are needed.

๐Ÿงฎ Solved Example

Problem: Sort the list [5, 1, 4, 2, 8] using Bubble Sort.

Given: Unsorted list: [5, 1, 4, 2, 8]

Steps:

  1. First pass: Compare 5 and 1, swap. Compare 5 and 4, swap. Compare 5 and 2, swap. Compare 5 and 8, no swap. Result: [1, 4, 2, 5, 8]
  2. Second pass: Compare 1 and 4, no swap. Compare 4 and 2, swap. Compare 4 and 5, no swap. Result: [1, 2, 4, 5, 8]
  3. Third pass: Compare 1 and 2, no swap. Compare 2 and 4, no swap. Result: [1, 2, 4, 5, 8]
  4. Fourth pass: Compare 1 and 2, no swap. Result: [1, 2, 4, 5, 8]
"
โœ…
Answer: Sorted list: [1, 2, 4, 5, 8]

โš ๏ธ Common Mistakes

โŒ Mistake 1: Incorrectly swapping elements during comparison.

โœ… How to avoid: Double-check the comparison and ensure that the elements are swapped only if they are in the wrong order.

โŒ Mistake 2: Not completing all necessary passes.

โœ… How to avoid: Ensure that the algorithm iterates through the list n-1 times, where n is the number of elements, or until no swaps are made in a pass.

๐Ÿ’ก Study Tip

Visualize the "bubbling" effect by manually tracing the algorithm on small lists. This helps understand how the largest elements move to the end of the list in each pass.

๐Ÿ“– Chapter 2: Insertion Sort

What this chapter covers: This chapter introduces the Insertion Sort algorithm, which builds a sorted sublist by iteratively inserting elements from the unsorted portion into their correct positions. It covers the algorithm's stability, pseudocode, time and space complexity, and provides examples to illustrate the sorting process. Insertion Sort is efficient for small datasets or nearly sorted data.

๐Ÿ”‘ Essential Concepts & Formulas

Concept/FormulaDefinition/EquationWhen to UseQuick Check
Basic PrincipleIteratively build a sorted sublist by inserting elements into their correct position.Sorting small lists or nearly sorted lists.Check if the left portion of the list is always sorted.
Time Complexity (Worst/Avg)O(n2)O(n^2)Sorting large, unsorted lists.Verify the number of comparisons grows quadratically with the size of the list.
Time Complexity (Best)O(n)O(n)Sorting already sorted lists.Check if the list is sorted in one pass.
Space ComplexityO(1)O(1)Situations where memory usage is a constraint.Verify no additional memory is used beyond the input array.
StabilityStableWhen maintaining the original order of equal elements is important.Check if elements with equal values maintain their relative order after sorting.

๐Ÿ› ๏ธ Problem Types

Type A: Sorting a list of numbers using Insertion Sort.

Setup: "When you encounter a list of numbers that needs to be sorted in ascending or descending order."

Method: Iterate through the list, taking one element at a time and inserting it into the correct position in the sorted sublist.

Example: Sort the list [5, 1, 4, 2, 8] using Insertion Sort.

  1. [5] -> [1, 5] -> [1, 4, 5] -> [1, 2, 4, 5] -> [1, 2, 4, 5, 8]

Type B: Determining the number of comparisons required for Insertion Sort to sort a list.

Setup: "If presented with a list of numbers and asked to determine the number of comparisons required to sort it using Insertion Sort."

Method: Count the number of comparisons made during the insertion process for each element.

Example: Determine the number of comparisons required to sort the list [1, 2, 3, 4, 5] using Insertion Sort. Since the list is already sorted, Insertion Sort will require n-1 comparisons.

๐Ÿงฎ Solved Example

Problem: Sort the list [5, 1, 4, 2, 8] using Insertion Sort.

Given: Unsorted list: [5, 1, 4, 2, 8]

Steps:

  1. Start with [5].
  2. Insert 1: [1, 5]
  3. Insert 4: [1, 4, 5]
  4. Insert 2: [1, 2, 4, 5]
  5. Insert 8: [1, 2, 4, 5, 8]
"
โœ…
Answer: Sorted list: [1, 2, 4, 5, 8]

โš ๏ธ Common Mistakes

โŒ Mistake 1: Incorrectly shifting elements to make space for insertion.

โœ… How to avoid: Carefully shift elements one position to the right to create space for the element being inserted.

โŒ Mistake 2: Not inserting the element into the correct position.

โœ… How to avoid: Ensure that the element is inserted into the correct position by comparing it with elements in the sorted sublist.

๐Ÿ’ก Study Tip

Use physical cards to simulate the insertion process. This helps visualize how elements are inserted into their correct positions in the sorted sublist.

๐Ÿ“– Chapter 3: Quick Sort

What this chapter covers: This chapter introduces the Quick Sort algorithm, a divide-and-conquer sorting algorithm that works by partitioning the array around a pivot element. It covers the algorithm's pseudocode, time and space complexity, and stability. Quick Sort is known for its efficiency in practice, especially for large datasets.

๐Ÿ”‘ Essential Concepts & Formulas

Concept/FormulaDefinition/EquationWhen to UseQuick Check
Basic PrincipleDivide-and-conquer algorithm that partitions the array around a pivot.Sorting large datasets where efficiency is important.Check if elements smaller than the pivot are to its left and larger elements to its right.
Time Complexity (Avg/Best)O(nlogโกn)O(n \log n)Sorting large datasets.Verify the number of comparisons grows logarithmically with the size of the list.
Time Complexity (Worst)O(n2)O(n^2)When the pivot is consistently the smallest or largest element.Avoid using Quick Sort in situations where the pivot is likely to be consistently poor.
Space ComplexityO(logโกn)O(\log n)Situations where memory usage is a constraint.Verify the depth of the recursion stack.
StabilityNot StableWhen maintaining the original order of equal elements is not important.Check if elements with equal values maintain their relative order after sorting.

๐Ÿ› ๏ธ Problem Types

Type A: Sorting a list of numbers using Quick Sort.

Setup: "When you encounter a list of numbers that needs to be sorted in ascending or descending order."

Method: Choose a pivot element, partition the array around the pivot, and recursively sort the sub-arrays on either side of the pivot.

Example: Sort the list [7, 2, 1, 6, 8, 5, 3, 4] using Quick Sort with the first element as the pivot.

Type B: Determining the pivot element in Quick Sort.

Setup: "If presented with a list of numbers and asked to determine the pivot element for Quick Sort."

Method: The pivot element can be chosen in several ways, such as the first element, the last element, or a random element. The choice of pivot can affect the performance of Quick Sort.

Example: Determine the pivot element for the list [1, 7, 3, 9, 1, 2, 4] using the middle value as the pivot.

๐Ÿงฎ Solved Example

Problem: Sort the list [7, 2, 1, 6, 8, 5, 3, 4] using Quick Sort with the first element as the pivot.

Given: Unsorted list: [7, 2, 1, 6, 8, 5, 3, 4]

Steps:

  1. Choose pivot: 7
  2. Partition: [2, 1, 6, 5, 3, 4, 7, 8]
  3. Recursively sort [2, 1, 6, 5, 3, 4] and [8]
"
โœ…
Answer: Sorted list: [1, 2, 3, 4, 5, 6, 7, 8]

โš ๏ธ Common Mistakes

โŒ Mistake 1: Incorrectly partitioning the array around the pivot.

โœ… How to avoid: Carefully compare elements with the pivot and place them in the correct sub-arrays.

โŒ Mistake 2: Not handling the base case for recursion.

โœ… How to avoid: Ensure that the recursion stops when the sub-array has only one element or is empty.

๐Ÿ’ก Study Tip

Experiment with different pivot selection strategies to see how they affect the performance of Quick Sort. A good pivot selection can help avoid the worst-case scenario.

4 more sections

Create a free account to import and read the full study notes โ€” all 6 sections.

No credit card ยท 2 free imports included

    Sorting Algorithms Exam - Cheatsheet 1 โ€” Cheatsheet | Evrika | Evrika Study