How to merge k sorted arrays, solution didn't work allowing duplicates.!





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Given k sorted arrays of size n each, merge them and print the sorted output.



The algorithm I followed is




  • iterate of over each array


    • pick the ith index in k arrays


    • insert() in minheap


    • delMin() and append result array.






from heapq import heappop, heappush

def merge_k_arrays(list_of_lists):
result = #len(list_of_lists[0])*len(list_of_lists)
minHeap=
n, k=0,0

print(list_of_lists)
while n < len(list_of_lists[0]):
if n ==0:# initial k size heap ready
while k < len(list_of_lists):
element= list_of_lists[k][n]
heappush(minHeap ,element )
k+=1
result.append(heappop(minHeap))
else: # one at a time.
k =0
while k < len(list_of_lists):
element = list_of_lists[k][n]
heappush(minHeap , element)
result.append(heappop(minHeap))
k+=1
n += 1

# add the left overs in the heap
while minHeap:
result.append(heappop(minHeap))

return result


Input:



input = [   [1, 3, 5, 7],
[2, 4, 6, 8],
[0, 9, 10, 11],

]


Output:



[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]


input:



input = [   [1, 3, 5, 7],
[2, 4, 6, 8],
[3, 3, 3, 3],
[7, 7, 7,7]
]


output:



[0, 1, 2, 3, 3, 3, 4, 5, 6, 3, 7, 7, 7, 7, 3, 7, 8, 9, 10, 11]


Could anyone help me know what piece is missing from my algorithm in order to merge the duplicate arrays in the second input too?










share|improve this question

























  • Is there a reason you're doing this instead of just using heapq.merge, which already exists and performs this exact functionality? (Technically, it's a generator function, where your function returns a list, but list(heapq.merge(*list_of_lists)) would do your job for you)

    – ShadowRanger
    Nov 23 '18 at 2:45













  • @ShadowRanger, yes, I am curious to see how this common algorithm works without relying on the libraries.

    – anu
    Nov 23 '18 at 18:06











  • Gotcha. Just a heads up, heapq.merge is actually implemented in Python (no C accelerators), so if you want a reference implementation, it's available. If you use ipython for interactive work (everyone should), simply importing heapq, then typing heapq.merge?? will display the source code.

    – ShadowRanger
    Nov 23 '18 at 19:16


















0















Given k sorted arrays of size n each, merge them and print the sorted output.



The algorithm I followed is




  • iterate of over each array


    • pick the ith index in k arrays


    • insert() in minheap


    • delMin() and append result array.






from heapq import heappop, heappush

def merge_k_arrays(list_of_lists):
result = #len(list_of_lists[0])*len(list_of_lists)
minHeap=
n, k=0,0

print(list_of_lists)
while n < len(list_of_lists[0]):
if n ==0:# initial k size heap ready
while k < len(list_of_lists):
element= list_of_lists[k][n]
heappush(minHeap ,element )
k+=1
result.append(heappop(minHeap))
else: # one at a time.
k =0
while k < len(list_of_lists):
element = list_of_lists[k][n]
heappush(minHeap , element)
result.append(heappop(minHeap))
k+=1
n += 1

# add the left overs in the heap
while minHeap:
result.append(heappop(minHeap))

return result


Input:



input = [   [1, 3, 5, 7],
[2, 4, 6, 8],
[0, 9, 10, 11],

]


Output:



[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]


input:



input = [   [1, 3, 5, 7],
[2, 4, 6, 8],
[3, 3, 3, 3],
[7, 7, 7,7]
]


output:



[0, 1, 2, 3, 3, 3, 4, 5, 6, 3, 7, 7, 7, 7, 3, 7, 8, 9, 10, 11]


Could anyone help me know what piece is missing from my algorithm in order to merge the duplicate arrays in the second input too?










share|improve this question

























  • Is there a reason you're doing this instead of just using heapq.merge, which already exists and performs this exact functionality? (Technically, it's a generator function, where your function returns a list, but list(heapq.merge(*list_of_lists)) would do your job for you)

    – ShadowRanger
    Nov 23 '18 at 2:45













  • @ShadowRanger, yes, I am curious to see how this common algorithm works without relying on the libraries.

    – anu
    Nov 23 '18 at 18:06











  • Gotcha. Just a heads up, heapq.merge is actually implemented in Python (no C accelerators), so if you want a reference implementation, it's available. If you use ipython for interactive work (everyone should), simply importing heapq, then typing heapq.merge?? will display the source code.

    – ShadowRanger
    Nov 23 '18 at 19:16














0












0








0








Given k sorted arrays of size n each, merge them and print the sorted output.



The algorithm I followed is




  • iterate of over each array


    • pick the ith index in k arrays


    • insert() in minheap


    • delMin() and append result array.






from heapq import heappop, heappush

def merge_k_arrays(list_of_lists):
result = #len(list_of_lists[0])*len(list_of_lists)
minHeap=
n, k=0,0

print(list_of_lists)
while n < len(list_of_lists[0]):
if n ==0:# initial k size heap ready
while k < len(list_of_lists):
element= list_of_lists[k][n]
heappush(minHeap ,element )
k+=1
result.append(heappop(minHeap))
else: # one at a time.
k =0
while k < len(list_of_lists):
element = list_of_lists[k][n]
heappush(minHeap , element)
result.append(heappop(minHeap))
k+=1
n += 1

# add the left overs in the heap
while minHeap:
result.append(heappop(minHeap))

return result


Input:



input = [   [1, 3, 5, 7],
[2, 4, 6, 8],
[0, 9, 10, 11],

]


Output:



[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]


input:



input = [   [1, 3, 5, 7],
[2, 4, 6, 8],
[3, 3, 3, 3],
[7, 7, 7,7]
]


output:



[0, 1, 2, 3, 3, 3, 4, 5, 6, 3, 7, 7, 7, 7, 3, 7, 8, 9, 10, 11]


Could anyone help me know what piece is missing from my algorithm in order to merge the duplicate arrays in the second input too?










share|improve this question
















Given k sorted arrays of size n each, merge them and print the sorted output.



The algorithm I followed is




  • iterate of over each array


    • pick the ith index in k arrays


    • insert() in minheap


    • delMin() and append result array.






from heapq import heappop, heappush

def merge_k_arrays(list_of_lists):
result = #len(list_of_lists[0])*len(list_of_lists)
minHeap=
n, k=0,0

print(list_of_lists)
while n < len(list_of_lists[0]):
if n ==0:# initial k size heap ready
while k < len(list_of_lists):
element= list_of_lists[k][n]
heappush(minHeap ,element )
k+=1
result.append(heappop(minHeap))
else: # one at a time.
k =0
while k < len(list_of_lists):
element = list_of_lists[k][n]
heappush(minHeap , element)
result.append(heappop(minHeap))
k+=1
n += 1

# add the left overs in the heap
while minHeap:
result.append(heappop(minHeap))

return result


Input:



input = [   [1, 3, 5, 7],
[2, 4, 6, 8],
[0, 9, 10, 11],

]


Output:



[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]


input:



input = [   [1, 3, 5, 7],
[2, 4, 6, 8],
[3, 3, 3, 3],
[7, 7, 7,7]
]


output:



[0, 1, 2, 3, 3, 3, 4, 5, 6, 3, 7, 7, 7, 7, 3, 7, 8, 9, 10, 11]


Could anyone help me know what piece is missing from my algorithm in order to merge the duplicate arrays in the second input too?







python python-3.x sorting heap heapq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 3:02









georgexsh

10.9k11339




10.9k11339










asked Nov 23 '18 at 2:03









anuanu

6161021




6161021













  • Is there a reason you're doing this instead of just using heapq.merge, which already exists and performs this exact functionality? (Technically, it's a generator function, where your function returns a list, but list(heapq.merge(*list_of_lists)) would do your job for you)

    – ShadowRanger
    Nov 23 '18 at 2:45













  • @ShadowRanger, yes, I am curious to see how this common algorithm works without relying on the libraries.

    – anu
    Nov 23 '18 at 18:06











  • Gotcha. Just a heads up, heapq.merge is actually implemented in Python (no C accelerators), so if you want a reference implementation, it's available. If you use ipython for interactive work (everyone should), simply importing heapq, then typing heapq.merge?? will display the source code.

    – ShadowRanger
    Nov 23 '18 at 19:16



















  • Is there a reason you're doing this instead of just using heapq.merge, which already exists and performs this exact functionality? (Technically, it's a generator function, where your function returns a list, but list(heapq.merge(*list_of_lists)) would do your job for you)

    – ShadowRanger
    Nov 23 '18 at 2:45













  • @ShadowRanger, yes, I am curious to see how this common algorithm works without relying on the libraries.

    – anu
    Nov 23 '18 at 18:06











  • Gotcha. Just a heads up, heapq.merge is actually implemented in Python (no C accelerators), so if you want a reference implementation, it's available. If you use ipython for interactive work (everyone should), simply importing heapq, then typing heapq.merge?? will display the source code.

    – ShadowRanger
    Nov 23 '18 at 19:16

















Is there a reason you're doing this instead of just using heapq.merge, which already exists and performs this exact functionality? (Technically, it's a generator function, where your function returns a list, but list(heapq.merge(*list_of_lists)) would do your job for you)

– ShadowRanger
Nov 23 '18 at 2:45







Is there a reason you're doing this instead of just using heapq.merge, which already exists and performs this exact functionality? (Technically, it's a generator function, where your function returns a list, but list(heapq.merge(*list_of_lists)) would do your job for you)

– ShadowRanger
Nov 23 '18 at 2:45















@ShadowRanger, yes, I am curious to see how this common algorithm works without relying on the libraries.

– anu
Nov 23 '18 at 18:06





@ShadowRanger, yes, I am curious to see how this common algorithm works without relying on the libraries.

– anu
Nov 23 '18 at 18:06













Gotcha. Just a heads up, heapq.merge is actually implemented in Python (no C accelerators), so if you want a reference implementation, it's available. If you use ipython for interactive work (everyone should), simply importing heapq, then typing heapq.merge?? will display the source code.

– ShadowRanger
Nov 23 '18 at 19:16





Gotcha. Just a heads up, heapq.merge is actually implemented in Python (no C accelerators), so if you want a reference implementation, it's available. If you use ipython for interactive work (everyone should), simply importing heapq, then typing heapq.merge?? will display the source code.

– ShadowRanger
Nov 23 '18 at 19:16












1 Answer
1






active

oldest

votes


















-1














To fix your code, move the result.append(heappop(minHeap)) in your second nested while loop to the outside of the nested while loop, like in your first nested while loop. This will make your code work.



        else: # one at a time.
k =0
while k < len(list_of_lists):
element = list_of_lists[k][n]
heappush(minHeap , element)

k+=1
result.append(heappop(minHeap))
n += 1


If you have any space constraints, this is still problematic since you are adding nearly your entire input into the heap. If space is not an issue, there is a much cleaner way to write your solution:



def merge(A):
result =
heap = [e for row in A for e in row]
heapify(heap)
for i in range(len(heap)):
result.append(heappop(heap))
return result


Otherwise, you will need to use a smarter solution that only allows the heap to have k elements in total, with one element from each list, and the new element you add each step should come from the origin list of the element that was just popped.






share|improve this answer
























  • Thanks for the suggestion, but your algorithm is both time & space inefficient. The asymptotic analysis of your algorithm: 3rd line will take O(n*k), 4th line will run in O(n*k), 6th line will run in O(n*k log n*k), so total runtime of your algorithm is n*k log n*k which is not efficient. On the other hand, in my algo, I am trying to keep the heapsize fixed to k(number of given arrays to merge). in first loop, I build this k size heap in O(k log k), then I pop the element which takes O(k log k) and then adds text from input to heap that takes O(n log n) & repeat.

    – anu
    Nov 23 '18 at 20:10













  • so, my algo. worst case runtime would be O(n log n) with space only O(k), but what I am stuck at is my method is failing for duplicate input at the back because restoring heap doesn't sort the already present items which is barrier to allow duplicates. could you suggest a solution to this problem.?

    – anu
    Nov 23 '18 at 20:14












Your Answer






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: "1"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439850%2fhow-to-merge-k-sorted-arrays-solution-didnt-work-allowing-duplicates%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














To fix your code, move the result.append(heappop(minHeap)) in your second nested while loop to the outside of the nested while loop, like in your first nested while loop. This will make your code work.



        else: # one at a time.
k =0
while k < len(list_of_lists):
element = list_of_lists[k][n]
heappush(minHeap , element)

k+=1
result.append(heappop(minHeap))
n += 1


If you have any space constraints, this is still problematic since you are adding nearly your entire input into the heap. If space is not an issue, there is a much cleaner way to write your solution:



def merge(A):
result =
heap = [e for row in A for e in row]
heapify(heap)
for i in range(len(heap)):
result.append(heappop(heap))
return result


Otherwise, you will need to use a smarter solution that only allows the heap to have k elements in total, with one element from each list, and the new element you add each step should come from the origin list of the element that was just popped.






share|improve this answer
























  • Thanks for the suggestion, but your algorithm is both time & space inefficient. The asymptotic analysis of your algorithm: 3rd line will take O(n*k), 4th line will run in O(n*k), 6th line will run in O(n*k log n*k), so total runtime of your algorithm is n*k log n*k which is not efficient. On the other hand, in my algo, I am trying to keep the heapsize fixed to k(number of given arrays to merge). in first loop, I build this k size heap in O(k log k), then I pop the element which takes O(k log k) and then adds text from input to heap that takes O(n log n) & repeat.

    – anu
    Nov 23 '18 at 20:10













  • so, my algo. worst case runtime would be O(n log n) with space only O(k), but what I am stuck at is my method is failing for duplicate input at the back because restoring heap doesn't sort the already present items which is barrier to allow duplicates. could you suggest a solution to this problem.?

    – anu
    Nov 23 '18 at 20:14
















-1














To fix your code, move the result.append(heappop(minHeap)) in your second nested while loop to the outside of the nested while loop, like in your first nested while loop. This will make your code work.



        else: # one at a time.
k =0
while k < len(list_of_lists):
element = list_of_lists[k][n]
heappush(minHeap , element)

k+=1
result.append(heappop(minHeap))
n += 1


If you have any space constraints, this is still problematic since you are adding nearly your entire input into the heap. If space is not an issue, there is a much cleaner way to write your solution:



def merge(A):
result =
heap = [e for row in A for e in row]
heapify(heap)
for i in range(len(heap)):
result.append(heappop(heap))
return result


Otherwise, you will need to use a smarter solution that only allows the heap to have k elements in total, with one element from each list, and the new element you add each step should come from the origin list of the element that was just popped.






share|improve this answer
























  • Thanks for the suggestion, but your algorithm is both time & space inefficient. The asymptotic analysis of your algorithm: 3rd line will take O(n*k), 4th line will run in O(n*k), 6th line will run in O(n*k log n*k), so total runtime of your algorithm is n*k log n*k which is not efficient. On the other hand, in my algo, I am trying to keep the heapsize fixed to k(number of given arrays to merge). in first loop, I build this k size heap in O(k log k), then I pop the element which takes O(k log k) and then adds text from input to heap that takes O(n log n) & repeat.

    – anu
    Nov 23 '18 at 20:10













  • so, my algo. worst case runtime would be O(n log n) with space only O(k), but what I am stuck at is my method is failing for duplicate input at the back because restoring heap doesn't sort the already present items which is barrier to allow duplicates. could you suggest a solution to this problem.?

    – anu
    Nov 23 '18 at 20:14














-1












-1








-1







To fix your code, move the result.append(heappop(minHeap)) in your second nested while loop to the outside of the nested while loop, like in your first nested while loop. This will make your code work.



        else: # one at a time.
k =0
while k < len(list_of_lists):
element = list_of_lists[k][n]
heappush(minHeap , element)

k+=1
result.append(heappop(minHeap))
n += 1


If you have any space constraints, this is still problematic since you are adding nearly your entire input into the heap. If space is not an issue, there is a much cleaner way to write your solution:



def merge(A):
result =
heap = [e for row in A for e in row]
heapify(heap)
for i in range(len(heap)):
result.append(heappop(heap))
return result


Otherwise, you will need to use a smarter solution that only allows the heap to have k elements in total, with one element from each list, and the new element you add each step should come from the origin list of the element that was just popped.






share|improve this answer













To fix your code, move the result.append(heappop(minHeap)) in your second nested while loop to the outside of the nested while loop, like in your first nested while loop. This will make your code work.



        else: # one at a time.
k =0
while k < len(list_of_lists):
element = list_of_lists[k][n]
heappush(minHeap , element)

k+=1
result.append(heappop(minHeap))
n += 1


If you have any space constraints, this is still problematic since you are adding nearly your entire input into the heap. If space is not an issue, there is a much cleaner way to write your solution:



def merge(A):
result =
heap = [e for row in A for e in row]
heapify(heap)
for i in range(len(heap)):
result.append(heappop(heap))
return result


Otherwise, you will need to use a smarter solution that only allows the heap to have k elements in total, with one element from each list, and the new element you add each step should come from the origin list of the element that was just popped.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 2:41









samuelli97samuelli97

246




246













  • Thanks for the suggestion, but your algorithm is both time & space inefficient. The asymptotic analysis of your algorithm: 3rd line will take O(n*k), 4th line will run in O(n*k), 6th line will run in O(n*k log n*k), so total runtime of your algorithm is n*k log n*k which is not efficient. On the other hand, in my algo, I am trying to keep the heapsize fixed to k(number of given arrays to merge). in first loop, I build this k size heap in O(k log k), then I pop the element which takes O(k log k) and then adds text from input to heap that takes O(n log n) & repeat.

    – anu
    Nov 23 '18 at 20:10













  • so, my algo. worst case runtime would be O(n log n) with space only O(k), but what I am stuck at is my method is failing for duplicate input at the back because restoring heap doesn't sort the already present items which is barrier to allow duplicates. could you suggest a solution to this problem.?

    – anu
    Nov 23 '18 at 20:14



















  • Thanks for the suggestion, but your algorithm is both time & space inefficient. The asymptotic analysis of your algorithm: 3rd line will take O(n*k), 4th line will run in O(n*k), 6th line will run in O(n*k log n*k), so total runtime of your algorithm is n*k log n*k which is not efficient. On the other hand, in my algo, I am trying to keep the heapsize fixed to k(number of given arrays to merge). in first loop, I build this k size heap in O(k log k), then I pop the element which takes O(k log k) and then adds text from input to heap that takes O(n log n) & repeat.

    – anu
    Nov 23 '18 at 20:10













  • so, my algo. worst case runtime would be O(n log n) with space only O(k), but what I am stuck at is my method is failing for duplicate input at the back because restoring heap doesn't sort the already present items which is barrier to allow duplicates. could you suggest a solution to this problem.?

    – anu
    Nov 23 '18 at 20:14

















Thanks for the suggestion, but your algorithm is both time & space inefficient. The asymptotic analysis of your algorithm: 3rd line will take O(n*k), 4th line will run in O(n*k), 6th line will run in O(n*k log n*k), so total runtime of your algorithm is n*k log n*k which is not efficient. On the other hand, in my algo, I am trying to keep the heapsize fixed to k(number of given arrays to merge). in first loop, I build this k size heap in O(k log k), then I pop the element which takes O(k log k) and then adds text from input to heap that takes O(n log n) & repeat.

– anu
Nov 23 '18 at 20:10







Thanks for the suggestion, but your algorithm is both time & space inefficient. The asymptotic analysis of your algorithm: 3rd line will take O(n*k), 4th line will run in O(n*k), 6th line will run in O(n*k log n*k), so total runtime of your algorithm is n*k log n*k which is not efficient. On the other hand, in my algo, I am trying to keep the heapsize fixed to k(number of given arrays to merge). in first loop, I build this k size heap in O(k log k), then I pop the element which takes O(k log k) and then adds text from input to heap that takes O(n log n) & repeat.

– anu
Nov 23 '18 at 20:10















so, my algo. worst case runtime would be O(n log n) with space only O(k), but what I am stuck at is my method is failing for duplicate input at the back because restoring heap doesn't sort the already present items which is barrier to allow duplicates. could you suggest a solution to this problem.?

– anu
Nov 23 '18 at 20:14





so, my algo. worst case runtime would be O(n log n) with space only O(k), but what I am stuck at is my method is failing for duplicate input at the back because restoring heap doesn't sort the already present items which is barrier to allow duplicates. could you suggest a solution to this problem.?

– anu
Nov 23 '18 at 20:14




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439850%2fhow-to-merge-k-sorted-arrays-solution-didnt-work-allowing-duplicates%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?