Chapter 25 - Sorting Flashcards

1
Q

Sorting is a classic subject in computer science. There are three reasons to study sorting algorithms. What are they?

A

First, sorting algorithms illustrate many creative approaches to problem solving, and these approaches can be applied to solve other problems.
Second, sorting algorithms are good for practicing fundamental programming techniques using selection statements, loops, methods, and arrays.
Third, sorting algorithms are excellent examples to demonstrate algorithm performance.

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

Where in the Java API can we find sorting methods?

A

in the java.util.Arrays and java.util.Collections classes.

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

How does the bubble sort work?

A

The bubble sort algorithm makes several passes through the array. On each pass, successive neighboring pairs are compared. If a pair is in decreasing order, its values are swapped; otherwise, the values remain unchanged. The technique is called bubble sort because the smaller values gradually “bubble” their way to the top.

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

What is the best-case time and the worst-case time for a bubble sort?

A

In the best-case analysis, the bubble sort algorithm needs just the first pass to find that the array is already sorted – no next pass is needed. Since the number of comparisons is n - 1 in the first pass, the best-case time for a bubble sort is O(n).
In the worst-case analysis, the bubble sort algorithm requires n - 1 passes. The first pass makes n - 1 comparisons; the second pass makes n - 2 comparisons; and so on; the last pass makes 1 comparison. Thus, the total number of comparisons is O(n^2).

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

How does the merge sort work?

A

The merge sort algorithm divides the array into two halves and applies a merge sort on each half recursively. The recursive call continues dividing the array into subarrays until each subarray contains only one element. The algorithm then merges these small subarrays into larger sorted subarrays until one sorted array results.

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

What is the time complexity of the merge sort algorithm?

A

The time complexity of a merge sort is O(n log(n))
This algorithm is better than selection sort, insertion sort, and bubble sort, because the time complexity of these algorithms is O(n^2).

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

How does the quick sort work?

A

Quicksort is a divide and conquer algorithm. Quicksort first divides a large list into two smaller sub-lists: the low elements and the high elements. Quicksort can then recursively sort the sub-lists.

  1. Pick an element, called a pivot, from the list.
  2. Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
  3. Recursively apply the above steps to the sub-list of elements with smaller values and separately the sub-list of elements with greater values.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the time complexity of the quick sort algorithm?

A

The worst-case time is O(n^2). The average-case time is O(n log(n)).

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

What is a binary tree?

A

A binary tree is a hierarchical structure. It either is empty, or it consists of an element, called the root, and two distinct binary trees, called the left subtree and the right subtree.
The length of a path is the number of edges in the path, the depth of a node is the length of the path from the root to the node.

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

What is a binary heap?

A

A binary heap is a binary tree with the following properties:
- Shape property: It is a complete binary tree.
- Heap property: Each node is greater than or equal to any of its children.
A binary tree is complete if each of its levels is full, except that the last level may not be full if all the leaves on the last level are placed leftmost.

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

How do you store a binary heap? (data structure)

A

A heap can be stored in an ArrayList or an array if the heap size is known in advance. Consider the array:
[62, 42, 59, 32, 39, 44, 13, 22, 29, 14, 33]
For a node at position i, its left child is at position 2i + 1, its right child is at position 2i + 2, and its parent is (i - 1)/2. For example, the node for element 39 is at position 4, so its left child (element 14) is at 9 (24+1), its right child (element 33) is at 10 (24+2), and its parent (element 42) is at 1((4-1)/2) (rounded down).

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

What is the algorithm for adding a new node to a binary heap?

A

To add a new node to the heap, first add it to the end of the heap and then rebuild the tree as follows:

let the last node be the current node;
while (current node > current node's parent) {
-   swap current node with its parent;
-   (current node is now one level up)
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the algorithm for removing the root in a binary heap?

A

Often you need to remove the maximum element, which is the root in a heap. After the root is removed, the tree must be rebuilt to maintain the heap property. The algorithm for rebuilding the tree can be described as follows:

move last node to replace root;
let the root be the current node;
while (current node has children and
- current node is smaller than one of them) {
- swap current node with largest of its children;
- (now current node is one level down)
}

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

What is the time complexity of a heap sort?

A

The worst-case time of heap sort is O(n log(n)).
Both merge sorts and heap sorts require O(n log(n)) time. A merge sort requires a temporary array for merging two subarrays; a heap sort does not need additional array space. Therefore, a heap sort is more space efficient than a merge sort.

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

What is bucket sort, and how does it work? What is its time complexity?

A

The lower bound for general sorting algorithms is O(n log(n)), so no sorting algorithms based on comparisons can perform better than O(n log(n)). However, if the keys are small integers, you can use bucket sort without having to compare the keys.
The bucket sort works as follows. Assume the keys are in the range from 0 to t. We need t + 1 buckets labeled 0, 1, …, and t. If an element’s key is i, the element is put into the bucket i. Each bucket holds the elements with the same key value. You can use a nested ArrayList to implement a bucket.
It takes O(n + t) time to sort the list and uses O(n +1) space, where n is the list size. Note that if t is too large, using the bucket sort is not desirable. Instead, use radix sort.

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

What is radix sort, and how does it work? What is its time complexity?

A

The radix sort is based on the bucket sort, but a radix sort uses only ten buckets.
Assume that the keys are positive integers. The idea for the radix sort is to divide the keys into subgroups based on their radix positions. It applies a bucket sort repeatedly for the key values on radix positions, starting from the least-significant position.
Radix sort takes O(dn) time to sort n elements with integer keys, where d is the maximum number of the radix positions among all keys.

17
Q

What is an external sort?

A

External sorting is a term for a class of sorting algorithms that can handle massive amounts of data. External sorting is required when the data being sorted do not fit into the main memory of a computing device (usually RAM) and instead they must reside in the slower external memory (usually a hard drive). External sorting typically uses a hybrid sort-merge strategy. In the sorting phase, chunks of data small enough to fit in main memory are read, sorted, and written out to a temporary file. In the merge phase, the sorted subfiles are combined into a single larger file.

18
Q

Suppose a list is {2, 9, 5, 4, 8, 1}. After the first pass of bubble sort, the list becomes

A. 2, 9, 5, 4, 8, 1
B. 2, 9, 5, 4, 1, 8
C. 2, 5, 9, 4, 8, 1
D. 2, 5, 4, 8, 1, 9
E. 2, 1, 5, 4, 8, 9
A

D. 2, 5, 4, 8, 1, 9

19
Q

The best-time complexity for bubble sort is ___

A. O(1)
B. O(logn)
C. O(n)
D. O(nlogn)
E. O(n*n)
A

C. O(n)

20
Q

The worst-time complexity for bubble sort is ___

A. O(1)
B. O(logn)
C. O(n)
D. O(nlogn)
E. O(n*n)
A

E. O(n*n)

21
Q

The time to merge two sorted lists of size n is __

A. O(1)
B. O(logn)
C. O(n)
D. O(nlogn)
E. O(n*n)
A

C. O(n)

22
Q

The worst-time complexity for merge sort is __

A. O(1)
B. O(logn)
C. O(n)
D. O(nlogn)
E. O(n*n)
A

D. O(nlogn)

23
Q

The average-time complexity for merge sort is __

A. O(1)
B. O(logn)
C. O(n)
D. O(nlogn)
E. O(n*n)
A

D. O(nlogn)

24
Q

What is correct about a pivot?

A. A pivot divides a list into two sublists of equal size.
B. A pivot can be chosen arbitrarily.
C. A pivot divides a list into two sublists, the elements in the first list are no larger than the pivot and the elements in the second list are larger than the pivot.
D. You should always choose a pivot that divides the list evenly.

A

B and D are correct

25
Q

The worst-time complexity for quick sort is ___

A. O(1)
B. O(logn)
C. O(n)
D. O(nlogn)
E. O(n*n)
A

E. O(n*n)

26
Q

The average-time complexity for quick sort is ___

A. O(1)
B. O(logn)
C. O(n)
D. O(nlogn)

A

D. O(nlogn)

27
Q

To remove the root, you need to start a process by first placing ___ to the place of the root and move it down to maintain the heap property.

A. one of the root’s children
B. the larger child of the root
C. the smaller child of the root
D. the last node in the heap

A

D. the last node in the heap

28
Q

A heap is represented using an array. Is the array {1 2 4 5 9 3} a heap?

A. Yes
B. No

A

B. No

29
Q

A heap is represented using an array. Is the array {64 42 59 32 39 44} a heap?

A. Yes
B. No

A

A. Yes

30
Q

The worst-time complexity for heap sort is __

A. O(1)
B. O(logn)
C. O(n)
D. O(nlogn)
E. O(n*n)
A

D. O(nlogn)

31
Q

The average-time complexity for heap sort is __

A. O(1)
B. O(logn)
C. O(n)
D. O(nlogn)
E. O(n*n)
A

D. O(nlogn)

32
Q

The most efficient algorithm for sorting integer keys is __

A. quick sort
B. merge sort
C. heap sort
D. radix sort

A

D. radix sort

33
Q

The __ algorithm does not compare keys.

A. quick sort
B. merge sort
C. heap sort
D. radix sort

A

D. radix sort