Skip to content
This repository has been archived by the owner on Apr 29, 2020. It is now read-only.

Counting the number of inversions added. #133

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

troublemagnet
Copy link

I added the counting inversions using merge sort algorithm. Please review.

Copy link
Member

@CuriousLearner CuriousLearner left a comment

Choose a reason for hiding this comment

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

Please format your code according to PEP8 guidelines.

merged , ans=_merge(L,R)
return merged , left+right+ans
return a , 0
def count_inversions(a):
Copy link
Member

Choose a reason for hiding this comment

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

Please use 4 spaces for indentation, and follow PEP8

@@ -0,0 +1,4 @@
from pydsa import count_inversions
def test_count_inversions():
Copy link
Member

Choose a reason for hiding this comment

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

Leave 2 lines before defining function

@troublemagnet
Copy link
Author

@CuriousLearner please review now. Apologies for the poor formatting initially.

Copy link
Member

@aktech aktech left a comment

Choose a reason for hiding this comment

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

Please don't rewrite the functions already present in the library.

Counts the number of inversions in an array.
Divide and conquer algorithm is used for same.
>>> from pydsa import count_inversions
>>> a = [1,20,6,4,5]
Copy link
Member

Choose a reason for hiding this comment

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

space after comma

Copy link
Author

Choose a reason for hiding this comment

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

@aktech the function there returns a sorted array while here it's supposed to return a count.

# Complexity : O(nlog(n))


def _merge(L, R):
Copy link
Member

Choose a reason for hiding this comment

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

return m, cnt


def merge_sort(a):
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

@aktech The function there returns the sorted array and not the count of inversions.

@troublemagnet
Copy link
Author

troublemagnet commented Jun 27, 2017

@aktech @CuriousLearner Could you please review?
EDIT: this hasn't been reviewed till now.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants