AP CSA selection insertion and merge sort key terms

This study set covers key terms and concepts related to selection sort, insertion sort, and merge sort, as typically found in the AP Computer Science A curriculum.

WolfMia5·34 flashcards·34 questions
APcomputer_scienceprogramming
0
Known
1 / 34
0
Learning
Front

Selection sort definition?

Tap to flip
Back

A sorting algorithm that repeatedly selects the smallest or largest element from the unsorted portion and moves it to the sorted portion.

Tap to flip
Got it
Still learning

Quiz(34 questions)

Question 1 of 34

1. What is the primary advantage of insertion sort over selection sort?

Terms in this Study Set(34)

Sorting Algorithms Overview(10)

Selection sort definition?

A sorting algorithm that repeatedly selects the smallest or largest element from the unsorted portion and moves it to the sorted portion.

Insertion sort process?

Inserts each element into its correct position in a sorted sublist. Works by comparing and shifting elements until the correct spot is found.

True or false: Merge sort is an in-place sorting algorithm.

False. Merge sort requires additional space for merging, making it not in-place.

Best case time complexity of insertion sort?

O(n) when the array is already sorted. Efficiency improves with nearly sorted data.

Compare selection sort and insertion sort.

- Selection sort: O(n^2) worst case. - Insertion sort: O(n^2) worst case, but O(n) best case. - Insertion often faster for small or partially sorted arrays.

Fill in the blank: Merge sort divides the array into ____ parts.

Two parts, recursively sorts them, and then merges the sorted parts.

What is the worst case time complexity of merge sort?

O(n log n) due to the divide and conquer approach.

Example of insertion sort on: [5, 2, 4, 6, 1, 3]

Steps: 2 starts at index 1. 5 and 2 are compared. Shift 5, insert 2. Continue with 4, 6, 1, 3.

True or false: Selection sort is more efficient than insertion sort.

False. Insertion sort performs better on average for small datasets.

Define stable sorting algorithm.

A stable sorting algorithm maintains the relative order of records with equal keys. Both insertion and merge sort are stable.

Selection and Insertion Sorts(12)

What is selection sort?

A sorting algorithm that repeatedly selects the smallest element from the unsorted portion and moves it to the sorted portion.

True or False: Selection sort is efficient for large datasets.

False - It has an average and worst-case time complexity of O(n²), making it inefficient for large datasets.

Describe the main process of insertion sort.

Insertion sort builds a sorted array one element at a time by repeatedly taking an unsorted element and finding its correct position in the sorted part.

Selection sort vs. Insertion sort: Efficiency

Selection sort: O(n²) in all cases Insertion sort: O(n) best case (nearly sorted), O(n²) worst case.

How does selection sort find the minimum element?

It iterates through the entire unsorted array, comparing elements to find the smallest one.

Fill in the blank: The ___ of insertion sort is based on the idea of building a sorted array incrementally.

key concept

What happens in each pass of insertion sort?

An element is picked and compared with sorted elements, and it is placed in the correct position.

True or False: Insertion sort is typically faster than selection sort for small arrays.

True - Due to lower overhead and better performance on partially sorted data.

Explain the swap process in selection sort.

Every time the minimum is found, the algorithm swaps it with the first unsorted element.

Key characteristic of insertion sort:

Efficient for small or nearly sorted datasets. It has low overhead.

What are the steps in selection sort?

1. Start with the full array 2. Find the minimum 3. Swap minimum with the first unsorted element 4. Repeat until sorted.

Insertion sort working example: Sort [5, 2, 4, 6, 1, 3].

1. [2, 5, 4, 6, 1, 3] after first pass 2. [2, 4, 5, 6, 1, 3] after second pass 3. [1, 2, 4, 5, 6, 3] after last pass.

Merge Sort(12)

What is merge sort?

A divide-and-conquer sorting algorithm that divides an array into halves, sorts each half, and merges them back together.

How does merge sort work?

1. Divide the array into two halves. 2. Recursively sort each half. 3. Merge the sorted halves back together.

What is the time complexity of merge sort?

O(n log n) in all cases (best, average, worst). It is efficient for large datasets.

True or false: Merge sort is an in-place sorting algorithm.

False. Merge sort requires additional space for the temporary arrays used during merging.

Fill in the blank: Merge sort is a __________ algorithm.

Stable. It maintains the relative order of equal elements.

What is the main advantage of merge sort over others?

Merge sort is particularly efficient for large lists and linked lists.

True or false: Merge sort can be implemented iteratively.

True. Merge sort can be implemented using both recursive and iterative approaches.

Compare merge sort and quicksort.

- Merge sort: Stable, O(n log n), uses extra space. - Quicksort: Unstable, O(n log n) average, O(n^2) worst, in-place.

What is the merging process in merge sort?

Combining two sorted arrays into one sorted array, by comparing elements one by one.

How does merge sort handle large datasets?

It efficiently divides data into smaller chunks, sorts them, and merges them, minimizing comparisons.

Provide a simple example of merge sort.

Array: [38, 27, 43, 3, 9, 82, 10]. 1. Divide: [38, 27, 43] & [3, 9, 82, 10]. 2. Sort: [27, 38, 43] & [3, 9, 10, 82]. 3. Merge: [3, 9, 10, 27, 38, 43, 82].

What is the space complexity of merge sort?

O(n) due to the temporary arrays used for merging elements.

Questions in this Study Set(34)

1. What is the primary advantage of insertion sort over selection sort?

A.It has a better best case time complexity.
B.It requires less memory.
C.It is always faster regardless of the dataset.
D.It can sort in reverse order more easily.

2. What is the primary strategy used in selection sort?

A.Select the smallest element from the unsorted portion
B.Split the array in half
C.Sort by comparing only adjacent elements
D.Move the largest element to the end

3. What is the primary method by which merge sort organizes data?

A.By dividing and merging
B.By swapping elements
C.By comparing pairs
D.By inserting elements

4. Which sorting algorithm is NOT stable?

A.Insertion sort
B.Merge sort
C.Selection sort
D.Bubble sort

5. What is the average time complexity of insertion sort in the worst case?

A.O(n)
B.O(n log n)
C.O(n²)
D.O(1)

6. Which of the following best describes the time complexity of merge sort?

A.O(n log n)
B.O(n^2)
C.O(log n)
D.O(n)

7. What is the time complexity of selection sort in the worst case?

A.O(n log n)
B.O(n)
C.O(n^2)
D.O(1)

8. Which of the following describes a situation where insertion sort is most efficient?

A.The array is in random order
B.The array is already sorted
C.The array is sorted in reverse order
D.The array is nearly sorted

9. What characteristic does merge sort maintain when sorting equal elements?

A.In-place sorting
B.Stability
C.Randomness
D.Unsorted order

10. How does merge sort achieve its efficiency?

A.By sorting elements in place.
B.By dividing the array into smaller subarrays.
C.By using a single pass to sort.
D.By selecting the smallest element repeatedly.

11. What is the main difference between selection sort and insertion sort in terms of efficiency?

A.Selection sort is faster in all cases
B.Insertion sort can be faster on small datasets
C.Both are equally efficient
D.Insertion sort can only be used on small datasets

12. In which scenario would merge sort be especially beneficial?

A.Sorting a small array
B.Sorting linked lists
C.Sorting data in-place
D.Sorting with minimal space

13. Which of the following correctly describes insertion sort?

A.It works by repeatedly selecting the largest element.
B.It compares each element with the sorted part and shifts if necessary.
C.It divides the array into two halves recursively.
D.It sorts the array by swapping adjacent elements.

14. How does selection sort achieve its sorting?

A.By recursively splitting the array
B.By continuously swapping elements
C.By selecting the minimum element and placing it at the start
D.By sorting one element at a time

15. Which of the following is true regarding the space complexity of merge sort?

A.It requires O(n) additional space
B.It requires no additional space
C.It requires O(log n) additional space
D.It requires O(n^2) additional space

16. What is the best case time complexity of merge sort?

A.O(n log n)
B.O(n^2)
C.O(n)
D.O(1)

17. Which of the following is NOT a characteristic of insertion sort?

A.Efficient for small datasets
B.Maintains the relative order of equal elements
C.Always has a time complexity of O(n log n)
D.Builds the sorted array incrementally

18. How does merge sort differ from quicksort in terms of stability?

A.Merge sort is unstable
B.Quicksort is stable
C.Both are stable
D.Merge sort is stable

19. Which of the following scenarios illustrates a case where insertion sort would perform particularly well?

A.Sorting a large random dataset.
B.Sorting a nearly sorted array.
C.Sorting a dataset with many duplicate values.
D.Sorting data in reverse order.

20. During each pass of insertion sort, what does the algorithm do with the selected element?

A.Compares it with the smallest element
B.Finds its proper position in the sorted part
C.Removes it from the array
D.Sorts the entire array again

21. What is a disadvantage of merge sort compared to other sorting algorithms?

A.It is slower on small datasets
B.It requires extra space
C.It is not stable
D.It can't be implemented recursively

22. True or False: Merge sort can operate with less than O(n log n) time complexity in the average case.

A.True
B.False
C.True, but only on small datasets.
D.False, only for insertion sort.

23. What happens to the dataset after each complete pass in selection sort?

A.One more element is sorted
B.The entire dataset is sorted
C.The array is reversed
D.No changes occur

24. Which of the following statements about the merging process is true?

A.It involves merging sorted arrays by selecting the smallest elements
B.It randomly combines elements from both halves
C.It sorts elements only from one half
D.It does not require any comparisons

25. What key property distinguishes stable sorting algorithms from non-stable ones?

A.They sort in ascending order only.
B.They maintain the order of equal elements.
C.They are faster than non-stable algorithms.
D.They require more memory.

26. In which scenario is selection sort particularly inefficient?

A.When sorting a small array
B.When sorting a nearly sorted array
C.When sorting a large random array
D.When sorting a reverse-sorted array

27. Which step is NOT part of the merge sort algorithm?

A.Dividing the array into halves
B.Recursively sorting each half
C.Randomly shuffling the elements
D.Merging the sorted halves

28. In which sorting algorithm is the 'divide and conquer' strategy primarily used?

A.Insertion sort
B.Selection sort
C.Merge sort
D.Bubble sort

29. Which of the following statements is true about the swap process in selection sort?

A.Swaps are made only when the array is sorted
B.The minimum element is swapped with the last unsorted element
C.Swaps occur after each comparison
D.The minimum element is swapped with the first unsorted element

30. What would be the result of applying merge sort to the array [4, 2, 7, 1, 3]?

A.[1, 2, 3, 4, 7]
B.[4, 2, 7, 1, 3]
C.[7, 4, 3, 2, 1]
D.[1, 4, 2, 3, 7]

31. How many comparisons does selection sort make in total for an array of n elements?

A.n
B.n(n-1)/2
C.n²
D.2n

32. Can merge sort be implemented in an iterative manner?

A.Yes, it can be done iteratively
B.No, it can only be recursive
C.Yes, but it is inefficient
D.No, it requires recursion

33. What is the output of this insertion sort example with the array [3, 1, 2] after the first pass?

A.[1, 2, 3]
B.[1, 3, 2]
C.[2, 1, 3]
D.[3, 1, 2]

34. Which of the following best describes the stability of merge sort?

A.It is a stable sorting algorithm.
B.It is an unstable sorting algorithm.
C.It is only stable for certain data types.
D.It does not maintain order for equal elements.

Related Study Sets

Create Your Own Study Set

Upload a PDF, paste your notes, or describe a topic – AI generates flashcards, quizzes and more in seconds.