cs126 sorting

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

1/24

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.

25 Terms

1
New cards

Describe the steps of an insertion sort.

  1. Starting with element e at index i

  2. Compare it to the element d at index i–1

  3. If e < f:

    • Swap the elements and repeat from (2) with i - 2

  4. If ef or index = 0:

    • Repeat from (1) with i + 1

  5. The list is sorted when a full pass is completed with no swaps

2
New cards

What is the best-case cost of an insertion sort of n elements?

ϴ(n)

3
New cards

What is the worst-case cost of an insertion sort of n elements?

ϴ(n2)

4
New cards

Describe the steps of a selection sort.

  • Find the smallest element and swap it with the first element of the array.

  • Repeat this with the subarray formed by taking the tail of the array.

5
New cards

What is the worst-case cost of a selection sort of n elements?

O(n2)

6
New cards

What is the theorem about sorting algorithms that exchanges adjacent elements?

Any sorting algorithm that exchanges adjacent elements requires Ω(n²) average time.

7
New cards

What is the theorem about sorting algorithms based on key comparisons?

No sorting algorithm based on key comparisons can possibly be faster than Ω(nlogn).

8
New cards

What is the divide and conquer strategy?

  • Divide: 

    • Divide the input data S into two independent subproblems S1 and S2

  • Recursive: 

    • Solve the subproblems S1 and S2

  • Conquer: 

    • Combine the solutions for S1 and S2 into a solution for S

  • The base case for the recursion are subproblems of size 0 or 1

9
New cards

Describe the steps of a merge sort.

  • Divide: 

    • Partition S into two sequences S1 and S2 of about n/2 elements each 

  • Recursive: 

    • Recursively sort S1 and S2

  • Conquer: 

    • Merge S1 and S2 into a sorted sequence

10
New cards

What is the cost of merging two sorted sequences, each with n/2 elements.

O(n)

11
New cards

What is the height h of a merge sort tree?

O(logn)

12
New cards

What is the worst-case time cost of a merge sort?

O(nlogn)

13
New cards

Describe the steps of a quick sort.

  1. Divide the array into two subarrays L and R around a pivot q, where each  element in L is less than or equal to q, and each element in R is greater than q

  2. Repeat (1) with the L and R subarrays

14
New cards

Name the (3) strategies for selecting a pivot in a quick sort.

  1. Pick the first element in the array (may lead to a worst-case scenario)

  2. Randomly select the pivot (generating random number is a costly operation)

  3. Median-of-three strategy: median of the first, last and middle value of the array

15
New cards

What is the best-case cost of a quick sort of n elements, and how is this result found?

  • partition() divides each array into two subarrays of equal size

  • So the best case time is ϴ(nlogn)

16
New cards

What is the worst-case cost of a quick sort of n elements, and how is this result found?

  • partition() divides each array into a subarray of size n-1 and a subarray of size

  • So the worst case time is ϴ(n2)

17
New cards

Describe the features of a selection sort.

  • Slow

  • In-place

  • For small data sets (<1K)

18
New cards

Describe the features of an insertion sort.

  • Slow

  • In-place

  • For small data sets (<1K)

19
New cards

Describe the features of a quick sort.

  • Fast (average-case)

  • In-place

  • For large data sets (>1M)

20
New cards

Describe the features of a merge sort.

  • Fast

  • Sequential data access

  • For huge data sets (>1M)

21
New cards

Describe the steps of a bucket sort.

  • Assume we have

    • A set S of n entries according to their keys k

    • An array B of size m

  • For each entry in S

    • Remove it and insert it at the end of bucket B[k]

  • For i in range 0 to m - 1

    • For each entry in B[i]

      • Remove it and insert it at the end of S

22
New cards

What is the worst-time cost of a bucket sort?

O(n+m)

  • n is the number of entries in the set

  • m is the size of the array B

23
New cards

What is stable sorting?

  • Sorting that preserves the order of key-value pairs

  • So if two entries have the same key, they will be ordered in the sorted list in the same order they were originally in.

24
New cards

What is a radix sort?

  • Sorts a sequence of key-value pairs by applying a stable bucket-sort approach.

  • Least Significant Digit (LSD) radix sort processes the integer representations starting from the LSD and moving towards the MSD.

25
New cards

What is the worst-time cost of a radix sort?

  • Its dependant on the number of digits of the keys d

  • O(d x n)