Sorting Flashcards

1
Q

How does Quick Sort work?

A
  1. Randomly select a pivot
  2. Using the left index and right index, increment the left index and decrement the right index until the left index finds a value that is greater than pivot, and the right index finds a value that is less than pivot. Swap those values. Continue until left index == right index.
  3. Swap the pivot with the left index when it is over.
  4. Recursively execute the quick sort on the left partition and right partition of the array.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the best and worst case time complexity of Quick Sort?

A

Best: O(nlogn)
Worst: O(n^2)

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

What is the best and worst case space complexity of Quick Sort?

A

Best: logn
Worst: n

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

How does Merge Sort work?

A

Divide and Conquer (Recursive algorithm)
1. Split the array into the left and right halves and keep on splitting until they reach their individual elements
2. Merge the individual elements into a sorted sublist -> 1 & 3 -> 1,3.
3. With each sorted sublist, compare the first element of each sublist and add the smaller element to a combined list. After adding, increment the index until one of the sublist runs out and add the remaining elements of the other list.

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

What is the best and worst case time complexity of Merge Sort?

A

O(nlogn)

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

What is the best and worst case space complexity of Quick Sort?

A

log n

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

How does Insertion Sort work?

A
  1. For each element from the second element of the list to the end, traverse each element to the left side of it and if that element is greater than the reference element, shift the element to the right. Once it finds an element smaller than it, stop and insert it there.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

Best: O(n)
Worst: O(n^2)

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

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

A

Best and Worst: O(1)

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

How does Bubble Sort work?

A
  1. For each element from the first to the second last, compare it to the element ahead of it. If it is greater, swap. Through the first iteration, the greatest number will be at the end of the list, and through the second iteration, the second greatest number will be the second last element of the list.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the best and worst case time complexity of Bubble Sort?

A

Best: O(n)
Worst: O(n^2)

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

What is the best and worst case space complexity of Bubble Sort?

A

Best and Worst: O(1)

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

How doe Selection Sort work?

A
  1. For each element from the first to the second last, compare it with every element to the right of it and find the minimum. Swap the minimum with the element.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

best case and worst case: O(n^2)

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

What is the best and worst case space complexity of Bubble Sort?

A

best & worst cases: O(1)

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

Is Selection Sort in-place and stable?

A

In-place but not stable

17
Q

Is Insertion Sort in-place and stable?

A

Both In-place and stable

18
Q

Is Bubble Sort in-place and stable?

A

Both In-place and stable

19
Q

Is Merge Sort in-place and stable?

A

Not In Place but Stable

20
Q

Is Radix Sort in-place and stable?

A

Not In Place but Stable

21
Q
A