Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class merg{
- int arr[10] = {1, 44, 32, 12, 43, 89, 41, 78, 11, 10};
- public:
- void merge(int l, int m, int r) {
- int i, j, k, nl, nr;
- nl = m-l+1; nr = r-m;
- int leftarr[nl], rightarr[nr];
- for(i = 0; i<nl; i++)
- leftarr[i] = arr[l+i];
- for(j = 0; j<nr; j++)
- rightarr[j] = arr[m+1+j];
- i = 0; j = 0; k = l;
- while(i < nl && j<nr) {
- if(leftarr[i] <= rightarr[j]) {
- arr[k] = leftarr[i];
- i++;
- }else{
- arr[k] = rightarr[j];
- j++;
- }
- k++;
- }
- while(i<nl) {
- arr[k] = leftarr[i];
- i++; k++;
- }
- while(j<nr) {
- arr[k] = rightarr[j];
- j++; k++;
- }
- }
- void mergesort(int start, int end){
- if(start < end){
- int middle = (start + end)/2;
- mergesort(start, middle);
- mergesort(middle + 1, end);
- merge(start, middle, end);
- }
- else{
- return;
- }
- }
- void display(){
- cout << "Sorted:";
- for(int i = 0; i < 10; i++){
- cout << " " << arr[i];
- }
- }
- };
- int main(){
- merg n;
- n.mergesort(0, 9);
- n.display();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement