Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int cont = 0,c = 0;
- void marge(int arr[],int beg,int mid,int end)
- {
- int l = mid - beg+1, r = end - mid;
- int left[l],right[r];
- for( int i = 0 ; i< l; i++)
- {
- left[i] = arr[beg+i];
- }
- for( int i = 0 ; i< l; i++)
- {
- right[i] = arr[mid+1+i];
- }
- int i = 0,j = 0,k=beg;
- while( i< l && j < r)
- {
- if( left[i] <= right[j])
- {
- arr[k] = right[j];
- //i++;
- j++;
- }
- else
- {
- cont++;
- arr[k] = left[i];
- cout<<left[i] <<" "<<right[j]<<endl;
- //j++;
- i++;
- }
- k++;
- }
- while(i< l)
- {
- arr[k] = left[i];
- i++;
- k++;
- }
- while( j < r )
- {
- arr[k] = right[j];
- j++;
- k++;
- }
- }
- void margeSort(int arr[],int beg,int end)
- {
- if(beg < end)
- {
- int mid = (beg+end) / 2;
- margeSort(arr,beg,mid);
- margeSort(arr,mid+1,end);
- marge(arr,beg,mid,end);
- }
- }
- void display(int arr[])
- {
- for( int i = 0; i<5; i++)
- {
- cout<<arr[i]<< " ";
- }
- }
- int main()
- {
- freopen("input.txt","r",stdin);
- int arr[1000],left[1000],right[1000];
- for(int i = 0; i < 6; i++)
- {
- cin>>arr[i];
- }
- // for(int i = 0; i < 5; i++)
- // {
- // cout<<arr[i];
- // }
- // for(int i = 0; i < 5; i++)
- // {
- // marge(arr,left,right,l,r);
- // }
- margeSort(arr,0,5);
- //display(arr);
- cout<<endl;
- cout<<cont;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement