Sorting Flashcards

1
Q

An Ideal Sorting Algorithm

A

Stable: Equal keys aren’t reordered

Extra Space: Operates in place, requireing O(1) extra space

Worst Case Comparisons: O( n log n ) comparisons

Worst Case Moves: O( n ) moves

Adaptive: Speeds up to O( n ) when data is nearly sorted

** There is no algorithm that has all these properties

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

Insertion Sort Characteristics

A

Stable: Yes

Best Case: O(n)*

Average Case: O(n2)

Worst Case: O(n2)

* Iif list is already sorted

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

Insertion Sort Algorithm

A
insertionSort(data[], n) for i=1 to n-1 mov all elements data[j] greater than data[i] by one position; place data[i] in its proper position;
`
// C++ example
template< typename T >
void insertionSort(T data[], int n) 
{
    for (int i = 1,j; i < n; i++) 
    { 
        T intemToInsert = data[i];
        for (j = i; j > 0 && intemToInsert < data[j-1]; j--) 
            data[j] = data[j-1];
        data[j] = intemToInsert; 
    }
}
`
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Selection Sort Summary

A

Stable: No

Best Case: O(n2)

Average Case: O(n2)

Worst Case: O(n2)

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

Selection Sort Algorithm

A
// n-2 explanation on description card selectionSort(data[], n) for i=0 *to* n-2 *select the smallest el among* data[i],...,data[n-1] ; *swap it with* data[i];
`// C++ example
template< typename T >
void selectionSort(T data[], int n) 
{
   for (int i = 0,j,least; i < n-1; i++) 
   { 
      for (j = i+1, least = i; j < n; j++)
          if (data[j] < data[least])
              least = j;
      swap(data[least],data[i]); 
   }
}`
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Bubble Sort Summary

A

Stable: Yes*

Best Case: O(n2)

Average Case: O(n2)

Worst Case: O(n2)

* Implentations vary, and may affect stability

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

Bubble Sort Algorithm

A
bubbleSort(data[], n) for i=0 *to* n-2 for j = n-1 *down to* i+1 *swap elements in positions* j *and* j-1 *if they are out of order;*
// C++ example
template< typename T >
void bubbleSort(T data[], int n) 
{
   for (int i = 0; i < n-1; i++)
      for (int j = n-1; j > i; --j)
         if (data[j] < data[j-1]) 
            swap(data[j],data[j-1]);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Bubble Sort’s comparison to Insertion and Selection

A

In the average case

Bubble Sort

  • makes approximately twice as many comparisons as insertion sort
  • the same number of moves as insertion sort
  • as many comparisons as selection sort
  • and n times more moves than selection sort
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

_______ sort starts from the end of the array by finding the largest elements, wheras ________ sort starts from the beginning using the smallet elements.

A

Heap, Selection

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

Heap Sort Algorithm

A
heapSort(data[],n) *transform* data *into a heap*; for i = *down to* 2 *swap the root with the element in position* i; *restore the heap property for the tree* data[0], . . . , data[i-1];
// C++ example
template< typename T >
void heapsort(T data[], int n) 
{
   for (int i = n/2 - 1; i >= 0; --i) // create a heap
      heapDown(data,i,n-1);
   for (int i = n-1; i >= 1; --i) 
   {
      swap(data[0],data[i]); // move the largest item to data[i]; 
      heapDown(data,0,i-1); // restore the heap property;
   } 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Heap Sort Summary

A

Stable: No

Best Case: O( n log n)

Average Case: O( n log n )

Worst Case: O( n log n )

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

Mergesort Summary

A

Stable: Yes*

Extra Space: O( n )

Best Case: O( n log n)

Average Case: O( n log n )

Worst Case: O( n log n )

* Implentations vary, and may affect stability

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

Mersort Algorithm (dividing arrays)

A
mergeSort(data[], first, last) if first \< last mid = (first + last) / 2; mergeSort(data, first, mid); mergeSort(data, mid+1, last); merge(data, first, last);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Mergesort Algorithm (merging two arrays)

A
merge(array1[], first, last) mid = (first + last) / 2; i1 = 0; i2 = first; i3 = mid + 1; while *both left and right subarrays of* array1 *contains elements* if array1[i2] \< array[i3] temp[i1++] = array1[i2++]; else temp[i1++] = array[i3++]; *load into* temp *the remaining elements of* array1; *load to* array1 *the content of* temp;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Mergesort Algorithm Implementation

A
template < typename E, typename Comp >
void mergeSort(E A[], E temp[], int left, int right)
{
  if (left == right) return; // list of one element
  int mid = (left + right) / 2;
  mergeSort < E, Comp > (A, temp, left, mid);
  mergeSort < E, Comp > (A, temp, mid+1, right);
  for (int i=left; i <= right; i++) // copy subarray to temp
    temp[i] = A[i];
  // do the merge operation back to A
  int i1 = left; int i2 = mid + 1;
  for (int curr=left; curr <= right; curr++)
  {
    if (i1 == mid+1) // left sublist exhausted
      A[curr] = temp[i2++];
    else if (i2 > right) // right sublist exhausted
      A[curr] = temp[i1++];
    else if ( Comp::prior(temp[i1], temp[i2]) )
      A[curr] = temp[i1++];
    else A[curr] = temp[i2++];
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Quicksort Summary

A

Stable: No*

Best Case: O( n log n)

Average Case: between best and worst

Worst Case: O( n2 )

* Implentations vary, and may affect stability

17
Q

Quicksort Pivot Choices

A
  • Middle value in (sub)array
  • Median of first, middle, and last elements
  • Median of values at 3 random indices
18
Q

Quicksort Implementation

A
template < class T >
void quickSort(T data[], int first, int last)
{

}
19
Q

It is inapproriate to use ________ for small arrays. For arrays with fewer than 30 items, ________ ______ is more efficient.

A

quicksort, insertion sort

20
Q

Sorting algorithms that rely on the comparison of items require at least ______ time – This is a lower bound

A

n log n

21
Q

Counting Sort Summary

A

Stable: Yes

Best Case: O( M* + n )

Average Case: O( M* + n )

Worst Case: O( M* + n )

* M is maximum data + 1

22
Q

Radix Sort Summary

A

Stable: Yes

Best Case: O( p(N + b) )

Average Case: O( p(N + b) )

Worst Case: O( p(N + b) )

* p is # of passes; b is # of “buckets”

23
Q

Shell Sort Summary

A

Stable: No

Best Case: O( n log n )*

Average Case: O( n1.5 )

Worst Case: O( n2 )

* if list is already sorted

24
Q

External Sorts Efficiency

A

Measured by the number of I/O transactions to/from disk or tape