Sorting Flashcards

1
Q

when does insertion sort run in linear time?

A

when the number of inversions is order of N

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

This is the algorithm of choice for up to moderately large input.

A

Shell sort

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

What is the mergeSort caller algorithm?

A
  • mergeSort(array [] a)
    • Array [] b = new Array[a.length];
    • Mergesort(a, b, 0, a.length-1);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Derive the runtime analysis of Mergesort

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

The number of comparisons is nearly optimal for these two algorithms.

A

heapsort, mergesort

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

What is the recurrence analysis of Mergesort?

A

T(N) = 2T(N/2) + N

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

Describe the partitioning algorithm for quicksort

A
  • The first step of the partitioning strategy gets the pivot elements out of the way by swapping it with the last element.
    • I starts at the first element and j starts at the second to last element.
    • While I is to the left of j, we move I right, skipping over elements that are smaller than the pivot.
    • We move j left, skipping over elements that are larger than the pivot.
    • When I and j have stopped, I is pointing at a large element and j is pointing at a small element (relative to the pivot).
    • If I is to the left of j, those elements are swapped.
      • We repeat this process until I and j cross.
    • The final part of the partitioning is to swap the pivot element with the element pointed to by i.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is the form of sedgewicks sequence?

A

9*4^i -9*2^i + 1, this is most easily implemented using an array.

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

Analysis of recursive algorithms always requires what?

A

Solving a recurrence relation

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

What is the recurrence relation for quicksort?

A

T(N) = T(i) +T(N-i-1) + cN, where i equal the size of the first partition

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

How many comparisons are used by heapsort in the absolute worst case?

A

2NlogN-O(N)

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

What is the worst possible choice for a pivot and why?

A

The first and last element, because if the input is already sorted, quicksort will take quadratic time to do nothing

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

Quicksort is commonly used in what language?

A

C++

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

Describe Radix sort

A

Radix sort assumes we know the range of integers to be sorted.

We choose a radix, say r = 10.

We create r temporary lists.

In the first pass we go through the original list, copying values into the linked lists, based on their least significant digit.

We concatenate the temporary lists and restart the process based on the second digit, and so on.

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

What is the worst case runtime and average run time of quickselect?

A

O(N^2) and O(N)

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

What is the merge algorithm?

A
  • Merge(array[] a, array[] b, int left, int rightPos, int rightEnd)
    • Int leftEnd = rightPos -1;
    • Int bcounter = left;
    • Int numElements = rightEnd - left + 1;
    • While(leftPos <= leftEnd && rightPos <= rightEnd)
      • If(a[leftPos] < a[rightPos]) b[bcounter++] = a[leftPos++];
      • Else b[bcounter++] = a[rightPos++];
    • While(leftPos<= leftEnd) b[bcounter++] = a[leftPos++];
    • While(rightPos<= rightEnd) b[bcounter++] = a[rightPos++];
    • For(int i = 0; i < numElements; i++, rightEnd–) a[rightEnd] = b[rightEnd];
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the mergesort algorithm?

A
  • Mergesort(array [] a, tempArray b[], int left, int right)
    • If(left < right) // this is the base case
      • Int center = (left+right)/2;
      • mergeSort(a, b, left, center)
      • mergeSort(a, b, center+1, right)
      • Merge(a, b, left, center+1, right)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is external sorting?

A

sorting that takes place on external memory because the items are too numerous to fit in main memory.

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

Using a priority queue, we can find the kth largest number in what time?

A

O(N + klogN)

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

Prove that the comparisons needed for any comparison based sorting algorithm is NlogN

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

Describe insertion sort:

A

Insertion sort consists of N-1 passes. For pass p =1 through N-1, insertion sort ensures that the elements in positions 0 through p are in sorted order. Insertion sort makes use of the fact that elements in positions 0 through p-1 are already known to be in sorted order.

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

Information-theoretic lower bound states what?

A

Information-theoretic lower bound- if there are P different possible cases to distinguish, and the questions are of the form Yes/No, then ceiling(log P) questions are always required to solve the problem.

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

What is a stable sorting algorithm?

A

Stable sorting algorithms maintain the relative order of records with equal keys (i.e. values). That is, a sorting algorithm is stable if whenever there are two records R and S with the same key and with R appearing before S in the original list, R will appear before S in the sorted list.

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

What is the best case scenario for quick sort?

A

In the best case, the pivot picked always belongs in the middle of the array creating two equal subarrays.

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

What is the Simple Quick Sort algorithm?

A
  • SimpleQuicksort(List items)
  • If(items.size() > 1)
    • List smaller = new ArrayList<>();
    • List same = new ArrayList<>();
    • List larger = new ArrayList<>();
    • Integer chosenItem = items.get(items.size()/2)
    • For(Integer I : items)
      • If(I < chosenItem)
        • Smaller.add(i)
      • Else if(I > chosenItem)
        • Larger.add(i)
      • Else
        • Same.add(i)
    • Items.clear();
    • Items.addAll(smaller);
    • Items.addAll(same);
    • Items.addAll(larger);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

any algorithm that sorts by exchanging adjacent elements requires what time on average?

A

N2

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

What kind of pivot selection can guarantee linear time of quickselect?

A

The median of median of five.

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

What was one of the first algorithms to break the quadratic barrier?

A

Shell Sort

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

Describe Shell sort

A
  • Shellsort compares elements that are distant; the distance between comparisons decreases as the algorithm runts until the last phase, in which adjacent elements are compared.
    • For this reason, Shellsort is sometimes referred to as diminishing increment sort.
    • Shellsort starts with an increment sequence. Any increment sequence will do so long has it ends with 1.
      • After a phase of some increment k, we have a[i] <= a[i + k].
30
Q

What is the average runtime of quicksort?

A

O(N log N)

31
Q

What is the worst case runtime of quicksort?

A

O(N^2)

32
Q

Analyze the best case runtime of Quicksort

A
33
Q

What algorithm uses the lowest number of comparisons?

A

mergesort

34
Q

When using quicksort, what should happen when elements equal to the pivot are encountered and why?

A
  • If an element is encountered that is equal to the pivot, I and j should both stop.
    • This will cause many swaps between identical objects, if all elements in the array are identical.
    • If neither stop, the pivot will swap with the last element I touched, which will be the next to last element, creating uneven subarrays and an O(N^2) runtime.
    • If both stop, and all items are identical, the sorting algorithm will have O(N log N) runtime.
35
Q

What is a good cutoff for quicksort?

A

N = 10;

36
Q

What is shellSort?

A

a simple to code sorting algorithm that runs in O(n^2), and is efficient in practice.

37
Q

the median of a group of N numbers is

A

The N/2th largest number

38
Q

Describe bucket sort

A

We keep an array called count of size M initialized to all zeros. Linearly run through the array[] incrementing count: ++count[array[i]]. We then scan the count array and print in order.

39
Q

What is the number of worst case comparisons for merging?

A

N-1

40
Q

what is the runtime of heapsort?

A

O(n log N)

41
Q

What are the four steps to quick sort?

A
  • If the number of elements in S is 0 or 1, then return;
  • Pick any element v in S. This is called the pivot.
  • Partition S- the pivot into two disjoint sets of respectively larger and smaller arrays.
  • Return quicksort on both disjoint sets.
42
Q

When is radix sort optimal?

A

When we know a range of the values M, and logr M < log2 N

43
Q

Describe the merging algorithm of mergesort

A
  • The basic merging algorithm takes to arrays A and B, and an output array with three counters. The first two counters progress up A and B comparing the two and placing the lesser in C. Whichever counter was placed in C is then incremented, and the process continues until either input list is exhausted, and the remainder of the other list is copied into C.
44
Q

Describe the quicksort algorithm

A
  • Arbitrarily choose any item, and then form three groups: those smaller than the chosen item, those equal to the chosen item, and those larger than the chosen item.
  • Recursively sort the first and third group, and then concatenate the three groups.
    • The result is a guaranteed sorted list.
45
Q

What is the main problem with heap sort and how can we correct it?

A
  • The main problem with heap sort is that it uses an extra array, but this can be avoided by taking advantage of the open spot in the original array after a deleteMin has occurred. Each object that is ejected after a deleteMin operation can then be placed in the last position of the original array.
    • We can create a maxHeap to have the original array end in increasing order, rather than decreasing order.
46
Q

What is an inversion?

A

an inversion in an array of numbers is any ordered pair (i, j) having the property that i < j, but array[i] > array[j].

47
Q

describe heapsort

A

First a binary heap of N elements is built in O(N) time, followed by N deleteMin operations, each of which take O(log N ) time.

48
Q

Any general-purpose sorting algorithm requires how many comparisons?

A

a simple to code sorting algorithm that runs in O(n^2), and is efficient in practice.

49
Q

After quickselect terminates, the kth largest/smallest element is in what position?

A

k-1

50
Q

What is the insertion sort algorithm?

A

○ insertionSort(array [])
§ Int j;
§ For (int p = 1; p < array.size(); p++)
□ Object Temp = array[p];
□ For(j = p; j > 0 && array[j] < array[j-1]; j–)
® a[j] = a[j-1]
□ a[j] = tmp;

51
Q

What is the runtime of bucket sort?

A
  • The runtime is O(M+N), and if M is O(N) then the total is O(N)
52
Q

What is the time to merge two sorted lists?

A

Linear

53
Q

What is the quickselect algorithm?

A
  • quickSelect(array [] a, int left, int right, int k)
    • If(left + cutoff < right)
      • Pivot = median3(a, left, right);
      • Int i = left, int j = right -1
      • For(;;)
        • While(a[++i] < pivot){}
        • While(a[–j] > pivot){}
        • If(i < j) swap(i, j)
        • Else break;
      • Swap(i, right -1);
      • If(k <= i)
        • Quickselect(a, left, i-1, k)
      • If(k>i+1)
        • Quickselect(a, i+1, right, k)
    • Else
      • InsertionSort(a, left, right)
54
Q
  • Sorting: 81, 94, 11, 96, 12, 35, 17, 95, 28, 58, 41, 75, 15 with a {1, 3, 5] increment sequence:
A
  • 35, 17, 11, 28, 12, 41, 75, 15, 96, 58, 81, 94, 95
  • 28, 12, 11, 35, 15, 41, 58, 17, 94, 75, 81, 96, 95
    • 11, 12, 15, 17, 28, 35, 41, 58, 75, 81, 94, 95, 96
55
Q

What are Hibbard’s suggested increments for Shellsort, and what is the running time using them?

A

O(N1.5), 1, 3, 7, ….2k-1

56
Q

List the inversions for {3, 1, 9, 5, 4, 2}

A

(3,1) (3,2) (9,5) (9,4) (9,2) (5,4) (5,2) (4,2)

57
Q

What is the worst case scenario for quick sort?

A

The worst case for quicksort: The pivot is the smallest element, all the time.

58
Q

compare quicksort to mergesort

A
  • Quicksort uses more comparisons with relatively fewer data movements than Mergesort.
59
Q

What is the average number of in version in an array of N distinct elements?

A

N(N-1)/4

60
Q

What conditions are needed to run a bucket sort?

A

the input must consist of only positive integers smaller than M, so we must know that all input is smaller than some number M.

61
Q

When does quicksort not perform as well as insertion sort?

A

when N ≤ 20.

62
Q

For what two functions does java uses mergesort currently?

A

Arrays.sort and collections.sort()

63
Q

What is the median-of-three algorithm?

A
  • Int median3(array [] a, int left, int right)
    • Int center = (left+right)/2;
    • If(a[center] < a.left) swap;
    • If(a[right] < a[left]) swap;
    • If(a[right] < a[center]) swap;
    • Swap center and right-1;
    • Return right-1
64
Q

What is the full quicksort routine?

A
  • Quicksort
    • Quicksort(array[] a, int left, int right)
      • If(left + cutoff <= right)
        • Int pivot = median3(a, left, right)
        • Int I = left, j = right -1;
        • For(; ; )
          • While(a[++i] < pivot){}
          • While (a[j–] > pivot){}
          • If(I < j) swap a[i] and a[j];
          • Else break
        • Swap a[i] and a[right-1]
        • Quicksort(a, left, i-1);
        • Quicksort(a, i+1, right);
      • Else do insertion sort
65
Q

Derive the worst case of Quicksort

A

Telescope the recurrence relation T(N-1) + cN to N^2

66
Q

What is comparison bases sorting?

A

the only operation allowed on the input data, besides assignments, is comparison.

67
Q

When should quicksort be used?

A
  • Quicksort is a better option for generic sorting in C++ because java is pass by reference, however the copying of generic objects can become expensive in C++.
68
Q

Shellsort runtime is dependent on what?

A

the choice of increment sequence.

69
Q

The number of inversions is exactly what the number of what in insertion sort?

A

swaps

70
Q

What are the four steps to quickselect?

A
  1. If |S| = 1, then k =1 and return the element in s as the answer. If a cutoff for small arrays is being used and |S| <= cutoff, then sort S using insertion sort and return the kth element.
  2. Pick a pivot element in S.
  3. Partition S into two other arrays just like quick sort.
  4. If k <= |, then the kth smallest element must be in S1, so we return quickselect(k, S1). If k = 1+|S1| then the pivot is the kth smallest element and we can return it as the answer. Otherwise the kth element is in S2 and it is the k -|s1|-1 smallest element, so we return quickselect(k, k-|s1|-1).