sorting algorithms Flashcards

1
Q

Insertion sort

  1. keys
  2. average runtime
  3. worst case run time
  4. additional space
  5. in place - yes/no
  6. stable - yes/no
A

Insertion sort

  1. any
  2. O (n2)
  3. O (n2)
  4. O (1)
  5. in place - yes
  6. stable - yes

note - insertion sort is really fast for small arrays, even more than quick sort for example

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

Bubble sort

  1. keys
  2. average runtime
  3. worst case run time
  4. additional space
  5. in place - yes/no
  6. stable - yes/no
A

Bubble sort

  1. any
  2. O (n2)
  3. O (n2)
  4. O (1)
  5. in place - yes
  6. stable - yes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Merge sort

  1. keys
  2. average runtime
  3. worst case run time
  4. additional space
  5. in place - yes/no
  6. stable - yes/no
A

Merge sort

  1. any
  2. O (nlogn)
  3. O (nlogn)
  4. O (n)
  5. in place - no
  6. stable - yes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Heap Sort

  1. keys
  2. average runtime
  3. worst case run time
  4. additional space
  5. in place - yes/no
  6. stable - yes/no
A

Heap Sort

  1. any
  2. O (nlogn)
  3. O (nlogn)
  4. O (1)
  5. in place - yes
  6. stable - no
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Quick sort

  1. keys
  2. average runtime
  3. worst case run time
  4. additional space
  5. in place - yes/no
  6. stable - yes/no
A

Quick sort

  1. any
  2. O (nlogn)
  3. O (n2) - depends on pivot selection (generally the mean should be picked)
  4. O (1)
  5. in place - yes
  6. stable - no
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Counting sort

  1. keys
  2. average runtime
  3. worst case run time
  4. additional space
  5. in place - yes/no
  6. stable - yes/no
A

counting sort

  1. integers [0…k]
  2. O (n+k)
  3. O (n+k)
  4. O (n+k)
  5. in place - no
  6. stable - yes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Radix sort

  1. keys
  2. average runtime
  3. worst case run time
  4. additional space
  5. in place - yes/no
  6. stable - yes/no
A

Radix sort

  1. integers with d digits in base b
  2. O (d(n+b)) - assuming we use counting sort, for b = O (n) we will get O(n)
  3. O (d(n+b)
  4. depends on the used stable sort
  5. in place - depends on the used stable sort
  6. stable - yes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Bucket sort

  1. keys
  2. average runtime
  3. worst case run time
  4. additional space
  5. in place - yes/no
  6. stable - yes/no
A

Bucket Sort

  1. numbers with a unioform distribution from [0,1)
  2. O(n)
  3. O(n2)
  4. O(n)
  5. in place - no
  6. stable - yes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Selection sort

  1. keys
  2. average runtime
  3. worst case run time
  4. additional space
  5. in place - yes/no
  6. stable - yes/no
A

Selection sort

  1. any
  2. O(n2)
  3. O(n2)
  4. O(1)
  5. in place - yes
  6. stable - no (could be implemented as stable for added overhead)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

sort according to insertion sort

[50, 80, 10, 30, 90, 60]

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

sort according to bubble sort

[9, 6, 2, 8, 3, 1, 7, 5, 4]

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

sort according to merge sort

[7, 3, 2, 16, 24, 4, 11, 9]

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

sort according to selection sort

[29, 72, 98, 13, 87, 66, 52, 51, 36]

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

sort according to heap sort

[6, 5, 3, 1, 8, 7, 2, 4]

A

https://en.wikipedia.org/wiki/Heapsort#/media/File:Heapsort-example.gif

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

sort according to quick sort (take 5 = pivot)

[8, 1, 6, 4, 0, 3, 9, 5]

A

[8, 1, 6, 4, 0, 3, 9, 5] (pivot = 5)

[1, 4, 0, 3, 5, 8, 6, 9] pivots = (3,6)

[1, 0, 3, 4, 5, 6, 8, 9] pivots = (1,8)

[0, 1, 3, 4, 5, 6, 8, 9]

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

sort according to counting sort

[2, 3, 0, 5, 2, 3, 0, 0, 5] (k=5)

A

count the number of appearances of each number:

A [2, 3, 0, 5, 2, 3, 0, 0, 5]

0 1 2 3 4 5

C [3, 0, 2, 2, 0, 2]

go over C and find number of elements smaller than each number

C [3, 3, 5, 7, 7, 9]

go over A from end to beginning and place each number correct amount of times

[0,0,0,2,2,3,3,5,5]

17
Q

sort according to radix sort

329

457

657

839

436

720

A

sort each digit from right to left using a stable sort

329 720 720 329

457 436 329 436

657 457 436 457

839 657 839 657

436 329 457 720

720 839 657 839

18
Q

sort according to bucket sort

[0.87, 0.71, 0.36, 0.62, 0.27, 0.94, 0.12, 0.21, 0.32]

A

Creat array B

[, 0.12 , 0.27 , 0.36, , 0.62, 0.71 , 0.84, 0.94]

0.21 , 0.32

than sort each sub list using insertion sort and concat them