{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CIS275LabProject2

# CIS275LabProject2 - CIS 275 Fall 2007 LAB PROJECT 2 Source...

This preview shows pages 1–3. Sign up to view the full content.

CIS 275, Fall 2007 LAB PROJECT 2 Source Code #include <iostream> #include <list> #include <algorithm> #include <iterator> using namespace std; int a[50]; void merge( int , int , int ); void merge_sort( int low, int high) { if (low==high) return ; //if the boundaries are the same, the sublist has only one element, and cannot be further split int middle; if (low<high) { middle=(low+high)/2; merge_sort(low,middle); //sort the first half merge_sort(middle+1,high); //sort the second half merge(low,middle,high); //merge both sublists } } void merge( int low, int middle, int high) { int l,i,j,b[50],k; l=low; //the first element in the first sublist i=low; j=middle+1; //the first element in the second sublist while ((l<=middle)&&(j<=high)) { if (a[l]<=a[j]) //if two lists exist and the current element in the low is smaller than the high, insert it into the next position in the output array and move to the next element in the low list. { b[i]=a[l]; l++; } else { b[i]=a[j]; j++; } i++; } if (l>middle) { for (k=j;k<=high;k++) {

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
b[i]=a[k]; i++; } } else { for (k=l;k<=middle;k++) { b[i]=a[k]; i++; } } for (k=low;k<=high;k++) a[k]=b[k]; } void main() { int num,i; cout<<" "<<endl; cout<<"
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 3

CIS275LabProject2 - CIS 275 Fall 2007 LAB PROJECT 2 Source...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online