






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







CPSC 223 -‐-‐ Fall 2010 2
CPSC 223 -‐-‐ Fall 2010 3
void Mergesort (Entry theArray[], int n, int first, int last) { if(first < last) { int mid = (first + last) / 2; Mergesort (theArray, n, first, mid); Mergesort (theArray, n, mid + 1, last); Merge (theArray, n, first, mid, last); } } CPSC 223 -‐-‐ Fall 2010 7
void Merge (Entry theArray[], int n, int first, int mid, int last) { Entry tmpArray[n]; int first1 = first, first2 = mid + 1, i = first1; for(; first1 <= mid && first2 <= last; i++) { if(theArray[first1] < theArray[first2]) tmpArray[i] = theArray[first1++]; else tmpArray[i] = theArray[first2++]; } for(; first1 <= mid; i++, first1++) // copy remaining from first half tmpArray[i] = theArray[first1]; for(; first2 <= last; i++, first2++) // copy remaining from sec. half tmpArray[i] = theArray[first2]; for(int j = first; j <= last; j++) // copy tmpArray to the array theArray[j] = tmpArray[j]; } CPSC 223 -‐-‐ Fall 2010 8
CPSC 223 -‐-‐ Fall 2010 13 Selec3on Sort Bubble Sort Best Case Average Case Worst Case O ( n^2 ) O ( n^2 ) O ( n^2 ) O ( n ) O ( n^2 ) O ( n^2 ) Inser3on Sort O ( n ) O ( n^2 ) O ( n^2 ) Quicksort Heapsort Treesort Mergesort O ( n log n ) O ( n log n ) O ( n log n )
CPSC 223 -‐-‐ Fall 2010 14
CPSC 223 -‐-‐ Fall 2010 16 29 10 32 13 Ini3al list ( n =4) Par22on 13 10 29 32 n = 2, n = 1 Par22on^ pivot 10 13 n^ = 1,^ n^ = 0 pivot
CPSC 223 -‐-‐ Fall 2010 21 Selec3on Sort Bubble Sort Best Case Average Case Worst Case O ( n^2 ) O ( n^2 ) O ( n^2 ) O ( n ) O ( n^2 ) O ( n^2 ) Inser3on Sort O ( n ) O ( n^2 ) O ( n^2 ) Quicksort O ( n log n ) O ( n log n ) O ( n^2 ) Heapsort Treesort Mergesort O ( n log n ) O ( n log n ) O ( n log n )