Python Merge sort count inversions

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












def mergeCount(left,right,invCount):
inversionCount = invCount
# print(left,right)
for right_item in right:
offset = 0
insertState = False
for i in range(len(left)):
if left[i] <= right_item:
offset += 1
elif left[i] > right_item:
inversionCount += len(left) - offset
left.insert(i,right_item)
# print("inserted",right_item)
insertState = True
break
if not insertState:
left.append(right_item)
# print("new array is",left)
return left,inversionCount


def mergeSortInversion(array):
if len(array) <= 1:
return array,0
else:
left_arr, left_count = mergeSortInversion(array[:len(array)//2])
right_arr, right_count = mergeSortInversion(array[len(array)//2:])
# print(left_arr,right_arr,left_count+right_count)
return mergeCount(left_arr,right_arr,left_count + right_count)


My merge sort code is taking the same amount of time as the brute force method for some reason. How can I fix the implementation / make it better?







share|improve this question



















  • Have you tried profiling it? Can you show the test cases and timing setup you're using?
    – Dannnno
    Jan 24 at 17:15










  • When you take a slice of a list in Python, like array[:len(array)//2], this has to copy out the contents of the slice, taking time proportional to the number of elements in the slice. This is what makes your code run slowly. You have to leave array unsliced and instead pass the indexes of the start and end of the slice as parameters.
    – Gareth Rees
    Jan 24 at 17:18











  • There's no need in inversionCount, you can manipulate invCount directly
    – Alex.S
    Jan 26 at 12:10

















up vote
1
down vote

favorite












def mergeCount(left,right,invCount):
inversionCount = invCount
# print(left,right)
for right_item in right:
offset = 0
insertState = False
for i in range(len(left)):
if left[i] <= right_item:
offset += 1
elif left[i] > right_item:
inversionCount += len(left) - offset
left.insert(i,right_item)
# print("inserted",right_item)
insertState = True
break
if not insertState:
left.append(right_item)
# print("new array is",left)
return left,inversionCount


def mergeSortInversion(array):
if len(array) <= 1:
return array,0
else:
left_arr, left_count = mergeSortInversion(array[:len(array)//2])
right_arr, right_count = mergeSortInversion(array[len(array)//2:])
# print(left_arr,right_arr,left_count+right_count)
return mergeCount(left_arr,right_arr,left_count + right_count)


My merge sort code is taking the same amount of time as the brute force method for some reason. How can I fix the implementation / make it better?







share|improve this question



















  • Have you tried profiling it? Can you show the test cases and timing setup you're using?
    – Dannnno
    Jan 24 at 17:15










  • When you take a slice of a list in Python, like array[:len(array)//2], this has to copy out the contents of the slice, taking time proportional to the number of elements in the slice. This is what makes your code run slowly. You have to leave array unsliced and instead pass the indexes of the start and end of the slice as parameters.
    – Gareth Rees
    Jan 24 at 17:18











  • There's no need in inversionCount, you can manipulate invCount directly
    – Alex.S
    Jan 26 at 12:10













up vote
1
down vote

favorite









up vote
1
down vote

favorite











def mergeCount(left,right,invCount):
inversionCount = invCount
# print(left,right)
for right_item in right:
offset = 0
insertState = False
for i in range(len(left)):
if left[i] <= right_item:
offset += 1
elif left[i] > right_item:
inversionCount += len(left) - offset
left.insert(i,right_item)
# print("inserted",right_item)
insertState = True
break
if not insertState:
left.append(right_item)
# print("new array is",left)
return left,inversionCount


def mergeSortInversion(array):
if len(array) <= 1:
return array,0
else:
left_arr, left_count = mergeSortInversion(array[:len(array)//2])
right_arr, right_count = mergeSortInversion(array[len(array)//2:])
# print(left_arr,right_arr,left_count+right_count)
return mergeCount(left_arr,right_arr,left_count + right_count)


My merge sort code is taking the same amount of time as the brute force method for some reason. How can I fix the implementation / make it better?







share|improve this question











def mergeCount(left,right,invCount):
inversionCount = invCount
# print(left,right)
for right_item in right:
offset = 0
insertState = False
for i in range(len(left)):
if left[i] <= right_item:
offset += 1
elif left[i] > right_item:
inversionCount += len(left) - offset
left.insert(i,right_item)
# print("inserted",right_item)
insertState = True
break
if not insertState:
left.append(right_item)
# print("new array is",left)
return left,inversionCount


def mergeSortInversion(array):
if len(array) <= 1:
return array,0
else:
left_arr, left_count = mergeSortInversion(array[:len(array)//2])
right_arr, right_count = mergeSortInversion(array[len(array)//2:])
# print(left_arr,right_arr,left_count+right_count)
return mergeCount(left_arr,right_arr,left_count + right_count)


My merge sort code is taking the same amount of time as the brute force method for some reason. How can I fix the implementation / make it better?









share|improve this question










share|improve this question




share|improve this question









asked Jan 24 at 16:29









Xuan

61




61











  • Have you tried profiling it? Can you show the test cases and timing setup you're using?
    – Dannnno
    Jan 24 at 17:15










  • When you take a slice of a list in Python, like array[:len(array)//2], this has to copy out the contents of the slice, taking time proportional to the number of elements in the slice. This is what makes your code run slowly. You have to leave array unsliced and instead pass the indexes of the start and end of the slice as parameters.
    – Gareth Rees
    Jan 24 at 17:18











  • There's no need in inversionCount, you can manipulate invCount directly
    – Alex.S
    Jan 26 at 12:10

















  • Have you tried profiling it? Can you show the test cases and timing setup you're using?
    – Dannnno
    Jan 24 at 17:15










  • When you take a slice of a list in Python, like array[:len(array)//2], this has to copy out the contents of the slice, taking time proportional to the number of elements in the slice. This is what makes your code run slowly. You have to leave array unsliced and instead pass the indexes of the start and end of the slice as parameters.
    – Gareth Rees
    Jan 24 at 17:18











  • There's no need in inversionCount, you can manipulate invCount directly
    – Alex.S
    Jan 26 at 12:10
















Have you tried profiling it? Can you show the test cases and timing setup you're using?
– Dannnno
Jan 24 at 17:15




Have you tried profiling it? Can you show the test cases and timing setup you're using?
– Dannnno
Jan 24 at 17:15












When you take a slice of a list in Python, like array[:len(array)//2], this has to copy out the contents of the slice, taking time proportional to the number of elements in the slice. This is what makes your code run slowly. You have to leave array unsliced and instead pass the indexes of the start and end of the slice as parameters.
– Gareth Rees
Jan 24 at 17:18





When you take a slice of a list in Python, like array[:len(array)//2], this has to copy out the contents of the slice, taking time proportional to the number of elements in the slice. This is what makes your code run slowly. You have to leave array unsliced and instead pass the indexes of the start and end of the slice as parameters.
– Gareth Rees
Jan 24 at 17:18













There's no need in inversionCount, you can manipulate invCount directly
– Alex.S
Jan 26 at 12:10





There's no need in inversionCount, you can manipulate invCount directly
– Alex.S
Jan 26 at 12:10
















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185887%2fpython-merge-sort-count-inversions%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185887%2fpython-merge-sort-count-inversions%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?