This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: int ii; for(i=0; i<n1; i++) { s1[i]=s[i]; } for(i=n1; i<n2; i++) { s2[i]=s[i]; } //call mergesort recursively mergesort(s1, n1); mergesort(s2, n2); //now s1 and s2 are sorted //put elements of s1 and s2 back into s int i1=0; //index s1 int i2=0; //index s2 //i is used as index for s //choose min element from s1 and s2 while(i1<n2 && i2<n2) { if(s1[i1] < s2[i2]) { s[i] = s1[i1]; i1++; } else { s[i] = s2[i2]; i2++; } i++; } //reached the end of either s1 or s2, copy the remaining elements for(/*empty*/ ; i1 < n1; i1++) { s[i] = s1[i1]; i++; } for(/*empty*/; i2 < n2; i2++) { s[i]=s2[i2]; i++; } //sorted //delete delete s1; delete s2; } //end mergesort...
View
Full Document
 Fall '08
 Staff
 Sort, Divide and conquer algorithm, //index s1 int, s2 int

Click to edit the document details