2.3.3 - Sorting Algorithms Flashcards

1
Q

What is the purpose of a sorting algorithm?

A

To take a number of elements in any order and output them in a logical order, normally numerical or lexographic.

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

How do most sorting algorithms output elements?

A

In ascending order. This can typically be altered by reversing output or switching an inequality.

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

What does bubble sort do?

A

Bubble sort makes comparisons and swaps between pairs of elements. The largest element in the unsorted part of the input is said to “bubble” to the top of the data with each iteration of the algorithm.

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

How does bubble sort work?

A

Start at first element, compare to second.
If in wrong order, swap, else move on.
Repeat process for every adjacent element pair in the array until end is reached.
This is one pass - for an array with n elements, n passes must be performed.

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

Basic bubble sort pseudocode

A

for i = 0 to A.length - 1:
for j = 0 to A.length - 2:
if A[j] > A[j+1]:
temp = A[j]
A[j] = A[j+1]
A[j+1] = temp
endif
return A

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

Why can basic bubble sort be inefficient?

A

If the algorithm goes on until it terminates, it could possibly make many pointless comparisons.

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

How can bubble sort be improved?

A

Add a noSwap variable, which is set to true and changed to False if a swap is made.
change second loop to “for j = 0 to A.length - (i+1)”

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

Describe the speed of bubble sort

A

Fairly slow, time complexity 0 (n^2)

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

What does insertion sort do?

A

Insertion sort places elements into a sorted sequence one by one. In i iterations, the first i elements are sorted. However, they aren’t necessarily the i smallest.

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

How does insertion sort work?

A

It starts at the second element in the input and compares it to the element to its left. If the two elements are in the wrong order, the smaller element is placed in the lowest position.
The third element is selected, then inserted in the correct position of the sorted portion to its left. This continues until the last element is correctly inserted.

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

Insertion sort pseudocode

A

for i = 1 to A.length - 1:
elem = A[i]
j = i - 1
while j > 0 and A[j] > elem:
A[j+1] = A[j]
j = j - 1
A[j+1] = elem

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

Why does insertion sort ignore the first element?

A

A single element is always a sorted sequence

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

What is merge sort?

A

Merge sort is an example of a class of algorithms known as “divide and conquer” algorithms.

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

How does merge sort work?

A

Merge sort is formed from two functions, MergeSort and Merge. MergeSort divides its input into two, recursively calling MergeSort on these two parts until they are length 1.
Merge then puts groups back together in a special way, ensuring they are sorted.

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

Merge Sort Pseudocode

A

MergeSort(A)
if A.length <= 1:
return A
else:
mid = A.length / 2
left = A[0…mid]
right = A[mid+1…A.length-1]
leftSort = MergeSort(left)
rightSort = MergeSort(right)
return Merge(leftSort, rightSort)

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

Describe speed of merge sort

A

More efficient than bubble and insertion, worst case time complexity of 0 (nlogn)

17
Q

How does quick sort work?

A

Quick sort works by selecting an element, often the central element (pivot) and dividing the input around it.
Elements smaller than the pivot are placed in a list to the left of the pivot, others are placed in a list to the right.

This process is repeated recursively on each new list until all elements in the input are old pivots, or form a list of length 1.

18
Q

Describe speed of quick sort.

A

Quick sort isn’t particularly fast, in fact, it’s time complexity is 0 (n^2)