lecture-12

Last mergethearray n rst mid last cpsc 223 fall

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: rst, 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 Mergesort (based on textbook) 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 <= tmpArray[i] for(; first2 <= tmpArray[i] mid; i++, first1++) = theArray[first1]; last; i++, first2++) = theArray[first2]; for(int j = first; j <= last; j++) theArray[j] = tmpArray[j]; // copy remaining from first half // copy remaining from sec. half // copy tmpArray to the array } CPSC 223  ­ ­ Fall 2010 8 4 10/12/10 Mergesort The merge step •  Assume number of elements to be merged is n –  n = length of first half + length of second half •  The me...
View Full Document

This document was uploaded on 03/18/2014 for the course CPSC 223 at Gonzaga.

Ask a homework question - tutors are online