Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

count inversion.cpp #1865

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions Count_Inversion/count inversion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include<iostream>
using namespace std;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mam I couldn't get which line?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Third line


int mergefn(int arr[],int temp[],int left,int mid,int right)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep space after comma

{
// temp[] is the auxillary array to store the sorted elements
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this commend before the mergefn() function




Comment on lines +8 to +10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove these lines

int i=left;
int j=mid;
int k=left;
int inv_cnt=0;
Comment on lines +11 to +14
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep space before and after comma



Comment on lines +15 to +16
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove these two lines


while((i<=mid-1 ) && (j<=right))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep space before and after operator

{

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line

if(arr[i]<=arr[j])
temp[k++]=arr[i++];
Comment on lines +21 to +22
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep space before and after operator

else
{

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line

temp[k++]=arr[j++];
inv_cnt+=(mid-i);
Comment on lines +26 to +27
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep space before and after operator


/*
if arr[i] is greater than arr[j], then there are (mid – i) inversions. because left and right subarrays are sorted,
so all the remaining elements in left-subarray (arr[i+1], arr[i+2] … arr[mid]) will be greater than arr[j]*/
Comment on lines +29 to +31
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one line should only contain 80 characters. break the comment accordingly

}



Comment on lines +33 to +35
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the lines

}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line


while(i<=mid-1)
temp[k++]=arr[i++];
while(j<=right)
temp[k++]=arr[j++];
Comment on lines +39 to +42
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep space before and after operator




for(i=left;i<=right;i++)
arr[i]=temp[i];
Comment on lines +46 to +47
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep space before and after operator


return inv_cnt;


Comment on lines +50 to +51
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove these two line

}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line


int mergesort(int arr[],int temp[],int left,int right)
{

int inv_cnt=0;

if(right>left)
{

int mid=(left+right)/2;


inv_cnt+=mergesort(arr,temp,left,mid);// counting inversions in left subarray
inv_cnt+=mergesort(arr,temp,mid+1,right);//counting inversions in the right subarray
inv_cnt+=mergefn(arr,temp,left,mid+1,right);// count overall inversions while merging the left subarray and right subarray
Comment on lines +58 to +68
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep space before and after operator
correct the indentation
keep space after comma
take comments to previous line


}
return inv_cnt;
}



Comment on lines +73 to +75
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the lines


int main()
{
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];

int temp[n];
cout<<mergesort(arr,temp,0,n-1)<<endl;
}
return 0;
Comment on lines +79 to +91
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep space before and after operator
correct the indentation
keep space after comma


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line

}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line


/*
Sample Input
2
4
1 2 4 3
3
3 2 1
Sample Output
1
3
*/