Sorting Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is the purpose of Bubble Sorting?

A

Repeatedly compare and swap adjacent elements in an array until the array is sorted.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the largest number of swaps performed by Bubble Sort per item in the list?

A

The largest number of swaps performed by Bubble Sort per item in the list is n-1, where n is the size of the list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the best case and worst case complexity of bubble sorting? Explain

A

The best case and worst case time complexity of Bubble Sort are O(n) and O(n^2), respectively. The best case occurs when the list is already sorted, and the algorithm only needs to make one pass through the list. The worst case occurs when the list is in reverse order, and the algorithm needs to make n-1 passes through the list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the result of applying one iteration (list traversal) of Bubble Sort if one wants to sort the list in increasing order and starts from the left?

  1. Example: [5, 20, -5, 10, 3]
  2. Example: [-5, -20, -5, -10, 3]
A
  1. [5, -5, 10, 3, 20]
  2. [-20, -5, -10, -5, 3]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When does Bubble Sort stop? Describe at least one way to improve Bubble Sort.

A

Bubble Sort stops when the list is sorted, which means that no swaps are made in the last pass through the list. One way to improve Bubble Sort is to use a flag variable to check if any swaps were made in the last pass. If no swaps were made, then the list is already sorted and the algorithm can stop. This can reduce the number of unnecessary passes through the list. Another way to improve Bubble Sort is to use a variable to keep track of the last position where a swap was made. This can reduce the size of the unsorted portion of the list, since all the elements after the last swap are already in their final positions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does it mean for a sorting algorithm to be incremental?

A

An algorithm is incremental if it does not need to re-compute everything after a small change (not make more iterations)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Is Bubble Sort incremental? Why and when?

A

Bubble Sort is incremental when an element is appended to the start of the list, (Bubble sort II).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does it mean for a sorting algorithm to be stable? Why?

A

A sorting algorithm is said to be stable if two objects with equal keys appear in the same order in sorted output as they appear in the input data set. Stability is mainly important when we have key-value pairs with duplicate keys possible (like people’s names as keys and their details as values). And we wish to sort these objects by keys. Stability is also useful when we want to sort a collection on multiple keys, such as first by name and then by age.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Is Bubble Sort stable?

A

Yes, but using => instead of > makes it non-stable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the main idea of Selection Sort?

A

The main idea of Selection Sort is to find the minimum element in the unsorted portion of the array and swap it with the first unsorted element. Repeat this process until the array is sorted.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the best case and worst case complexity of Selection Sort? Explain?

A

The best case and worst case time complexity of Selection Sort are both O(n^2), where n is the size of the list. This is because the algorithm always needs to make n-1 passes through the list, and each pass requires n-i comparisons, where i is the current pass number.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the result of applying one iteration of SelectionSort if one wants to sort the list in increasing order and starts from the left?

  1. Example: [5, 20, -5, 10, 3]
  2. Example: [6, 5, 4, 3, 2, 1]
A

One iteration of Selection Sort is to find the minimum element in the unsorted portion of the list and swap it with the first unsorted element. An example of applying one iteration of Selection Sort to sort the list [5, 20, -5, 10, 3] in increasing order is:

  • Find the minimum element in the unsorted portion of the list, which is -5.
  • Swap -5 with the first unsorted element, which is 5. The list becomes [-5, 20, 5, 10, 3].
  • The first iteration is done, and the smallest element -5 is in its final position.

An example of applying one iteration of Selection Sort to sort the list [6, 5, 4, 3, 2, 1] in increasing order is:

  • Find the minimum element in the unsorted portion of the list, which is 1.
  • Swap 1 with the first unsorted element, which is 6. The list becomes [1, 5, 4, 3, 2, 6].
  • The first iteration is done, and the smallest element 1 is in its final position.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Is Selection Sort stable?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Is Selection Sort incremental?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the main idea of Insertion Sort?

A

The main idea of Insertion Sort is to divide the array into two parts: a sorted part and an unsorted part. Initially, the sorted part contains only the first element of the array, and the unsorted part contains the rest of the elements. Then, one by one, take each element from the unsorted part and insert it into its correct position in the sorted part. Repeat this process until the array is sorted.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the best case and worst case complexity of Insertion Sort? Explain

A

The best case and worst case time complexity of Insertion Sort are O(n) and O(n^2), respectively, where n is the size of the list. The best case occurs when the list is already sorted, and the algorithm only needs to make one pass through the list. The worst case occurs when the list is in reverse order, and the algorithm needs to make n-1 passes through the list, and each pass requires n-i comparisons and shifts, where i is the current pass number.

17
Q

What is the result of applying one iteration of Insertion Sort if one wants to sort the list in increasing order and starts from the left?

  1. Example: [5, 20, -5, 10, 3]
  2. Example: [-7,-1,-4,4,5,6]
A

An example of applying one iteration of Insertion Sort to sort the list [5, 20, -5, 10, 3] in increasing order is:

  • Initially, the sorted part is [5], and the unsorted part is [20, -5, 10, 3].
  • Take the first element from the unsorted part, which is 20, and insert it into its correct position in the sorted part, which is after 5. The list becomes [5, 20, -5, 10, 3].
  • The first iteration is done, and the sorted part is [5, 20], and the unsorted part is [-5, 10, 3].

An example of applying one iteration of Insertion Sort to sort the list [-7,-1,-4,4,5,6] in increasing order is:

  • Initially, the sorted part is [-7], and the unsorted part is [-1, -4, 4, 5, 6].
  • Take the first element from the unsorted part, which is -1, and insert it into its correct position in the sorted part, which is after -7. The list becomes [-7, -1, -4, 4, 5, 6].
  • The first iteration is done, and the sorted part is [-7, -1], and the unsorted part is [-4, 4, 5, 6].
18
Q

Is Insertion Sort stable?

A

Yes, but using => instead of > makes it non-stable

19
Q

Is insertion Sort incremental?

A

Yes