Sessions 26 and 27 - Data Structures &amp; Algorithm

# 3 include iostream 4 using stdcout 5 using stdendl 6 7

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: 6 and 27: Data Structures & Algorithm 100 Merge Sort 9 10 11 12 13 14 15 16 … 1 245712 2 345 L2457 ∞ i 3 1 2 R1 2 6 9 10 11 12 13 14 15 16 … 34 3 6 … 5 ∞ j 1 145712 2 L2457 1 125712 2 345 L2457 ∞ 3 1 2 R1 2 j i Sharif University of Technology ∞ 1 2 R1 2 i 6 … 34 3 6 5 ∞ 9 10 11 12 13 14 15 16 … 34 3 6 j 9 10 11 12 13 14 15 16 … 345 3 6 … 5 ∞ 1 122712 2 345 L2457 ∞ i Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 3 1 2 R1 2 6 … 34 3 6 j 101 5 ∞ Merge Sort 1 2 2 3 4 5 6 7 merge 2 4 5 7 1 2 merge 2 5 4 7 1 merge 2 6 merge merge 5 3 4 3 2 merge 7 1 3 6 merge 2 6 initial sequence Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 102 1 2 // Class that creates a vector filled with random integers. 3 // Provides a function to sort the vector with merge sort. 4 #include <vector> 5 using std::vector; 6 7 // MergeSort class definition 8 class MergeSort 9{ 10 public: 11 MergeSort( int ); // constructor initializes vector 12 void sort(); // sort vector using merge sort 13 void displayElements() const; // display vector elements 14 private: 15 int size; // vector size 16 vector< int > data; // vector of ints 17 void sortSubVector( int, int ); // sort subvector 18 void merge( int, int, int, int ); // merge two sorted vectors 19 void displaySubVector( int, int ) const; // display subvector 20 }; // end class SelectionSort Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 103 1 2 // Class MergeSort member-function definition. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 #include <vector> 8 using std::vector; 9 10 #include <cstdlib> // prototypes for functions srand and rand 11 using std::rand; 12 using std::srand; 13 14 #include <ctime> // prototype for function time 15 using std::time; 16 17 #include "MergeSort.h" // class MergeSort definition 18 19 // constructor fill vector with random integers 20 MergeSort::MergeSort( int vectorSize ) 21 { 22 size = ( vectorSize > 0 ? vectorSize : 10 ); // validate vectorSize 23 srand( time( 0 ) ); // seed random number generator using current time 24 Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 104 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 41 42 43 44 45 46 47 48 49 50 51 52 53 // fill vector with random ints in range 10-99 for ( int i = 0; i < size; i++ ) data.push_back( 10 + rand() % 90 ); } // end MergeSort constructor // split vector, sort subvectors and merge subvectors into sorted vector void MergeSort::sort() { sortSubVector( 0, size - 1 ); // recursively sort entire vector } // end function sort // recursive function to sort subvectors void MergeSort::sortSubVector( int low, int high ) { // test base case; size of vector equals 1 if ( ( high - low ) >= 1 ) // if not base case { int middle1 = ( low + high ) / 2; // calculate middle of vector int middle2 = middle1 + 1; // calculate next element over // output split step cout << "split: "; displaySubVector( low, high ); cout << endl << " "; displaySubVector( low, middle1 ); cout << endl << " "; displaySubVector( middle2, high ); cout << endl << endl; Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures &...
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