Sorting Algorithms Flashcards

1
Q

A sorting algorithm is ______________ if its sorts by comparing the items

A

Comparison based sorting

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

Bubble sort Best, Average, and Worst Case

A

O(n^2)

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

Selection Sort Best, Average, and Worst Case

A

O(n^2)

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

Heapsort Best, Average, and Worst Case

A

O(n log n)

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

divides the array to sorted and unsorted sections, and keeps on inserting elements to the sorted section from the unsorted part

A

Insertion Sort

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

An algorithm that doesn’t require extra proportional space.,

A

in-place algorithm (eg. Insertion Sort)

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

divide and conquer sorting algorithms

A

Merge Sort & Quick Sort

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

Insertion Sort Pseudo code

A

insertionsort(int a[], int n){

int i,j;
for(i=1;i<n;i++)
for(j=i: j>0; j–)
if(a[j-1] > a[j])
swap(&a[j-1],&a[j]_)
else
break;
}

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

Merge Sort Pseudo codeO(n log n)

A

mergesort(int a[], int
lower, int upper){

int mid;
if(upper-lower>0){
mid = (lower + upper)/2;
mergesort(a,lower,mid);
mergesort(a,mid+1,upper);
merge(a, lower,mid,upper);

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

Merge sort runs in ____________ time.

A

O(n log n)

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

T or F:

Merge sort sorts in-place.

A

False. Merge sort uses extra memory

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

QuickSort Pseudo code

A

QUICKSORT(A,start,pivot):

if start < pivot:
q = PARTITION(A,start,pivot)
QUICKSORT(A,start,q-1)
QUICKSORT(A,q+1,pivot)

PARTITION(A,start,pivot):
x = A[pivot]
i = start-1
for j = start to pivot-1:
if A[j] <= x:
i = i+1
swap A[i] with A[j]

swap A[i+1] with A[PIVOT]

return i+1

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

Quicksort runs in ______ time in the worst-case

A

O(n^2)

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

Quicksort runs in the ____________ time for best and average cases.

A

O(n log n)

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

T or F

Quicksort sorts in-place

A

T

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

Insertion sort Best, Average, and Worst Case Run Time

A

BEST: O(n)
AVERAGE: O(n^2)
WORST: O(n^2)

17
Q

Merge sort Best, Average, and Worst Case Run Time

A

O(n log n) for all

18
Q

Quicksort Best, Average, and Worst Case Run Time

A

BEST: O(n log n)
AVERAGE: O(n log n)
WORST: O(n^2)

19
Q

Examples of non comparison-based Sorting

A

Counting Sort
Radix Sort

20
Q

__________________ assumes that the elements are integers that range from 0 to k, for some integer k.

A

Counting sort

21
Q

Counting Sort Pseudo code

A

COUNTING-SORT(A,n,k):
let B[1:n] and C[0:k] be arrays

for i=0 to k: //init
C[i] = 0
for j=1 to n: //freq
C[A[j]] = C[A[j]] + 1
for i=1 to k: //position
C[i] = C[i] + C[i-1]
//copy A to B, start from end of A
for j=n downto 1:
B[C[A[j]]] = A[j]
C[A[j]] = C[A[j]]-1

return B

22
Q

Counting sort runtime

A

O(n+k) time

23
Q

A sorting algorithm is said to be _________, when elements with the same value appear in the output array in the same order as they did in the input array

A

Stable (ie. Counting Sort)

24
Q

Used by card-sorting machines for punch cards

A

Radix Sort

25
Q

Radix sort counterintuitively sorts on the ________________ digit first.

A

least significant

26
Q

For radix sort to work correct, the sort must be _________.

A

stable

27
Q

Radix sorts assumes that the elements in the array has d digits

A

TRUE

28
Q

Radix Sort pesudocode

A

Radix-Sort(A,n,d):
for i=1 to d:
use a stable sort to sort the array on digit i (egg. COUNTING-SORT)

29
Q
A