Sessions 26 and 27 - Data Structures &amp; Algorithm

# Combinedindex data leftindex 98 end else 99 100

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

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: amp; Algorithm 105 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 // split vector in half; sort each half (recursive calls) sortSubVector( low, middle1 ); // first half of vector sortSubVector( middle2, high ); // second half of vector // merge two sorted vectors after split calls return merge( low, middle1, middle2, high ); } // end if } // end function sortSubVector // merge two sorted subvectors into one sorted subvector void MergeSort::merge( int left, int middle1, int middle2, int right ) { int leftIndex = left; // index into left subvector int rightIndex = middle2; // index into right subvector int combinedIndex = left; // index into temporary working vector vector< int > combined( size ); // working vector // output two subvectors before merging cout << "merge: "; displaySubVector( left, middle1 ); cout << endl << " "; displaySubVector( middle2, right ); cout << endl; // merge vectors until reaching end of either while ( leftIndex <= middle1 && rightIndex <= right ) { Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 106 81 // place smaller of two current elements into result 82 // and move to next space in vector 83 if ( data[ leftIndex ] <= data[ rightIndex ] ) 84 combined[ combinedIndex++ ] = data[ leftIndex++ ]; 85 else 86 combined[ combinedIndex++ ] = data[ rightIndex++ ]; 87 } // end while 88 89 if ( leftIndex == middle2 ) // if at end of left vector 90 { 91 while ( rightIndex <= right ) // copy in rest of right vector 92 combined[ combinedIndex++ ] = data[ rightIndex++ ]; 93 } // end if 94 else // at end of right vector 95 { 96 while ( leftIndex <= middle1 ) // copy in rest of left vector 97 combined[ combinedIndex++ ] = data[ leftIndex++ ]; 98 } // end else 99 100 // copy values back into original vector 101 for ( int i = left; i <= right; i++ ) 102 data[ i ] = combined[ i ]; 103 104 // output merged vector 105 cout << " "; 106 displaySubVector( left, right ); 107 cout << endl << endl; 108 } // end function merge 109 Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 107 63 // merge two sorted subvectors into one sorted subvector 64 void MergeSort::merge( int left, int middle1, int middle2, int right ) 65 { 71 // output two subvectors before merging 72 cout << "merge: "; 7 110 // display elements in vector 111 void MergeSort::displayElements() const 112 { 113 displaySubVector( 0, size - 1 ); 114 } // end function displayElements 115 116 // display certain values in vector 117 void MergeSort::displaySubVector( int low, int high ) const 118 { 119 // output spaces for alignment 120 for ( int i = 0; i < low; i++ ) 121 cout << " "; 122 123 // output elements left in vector 124 for ( int i = low; i <= high; i++ ) 125 cout << " " << data[ i ]; 126 } // end function displaySubVector Sharif University of Technology...
View Full Document

## This note was uploaded on 09/01/2013 for the course CE CE230 taught by Professor Arash during the Fall '13 term at Sharif University of Technology.

Ask a homework question - tutors are online