mergesort

Merge Sort Java

笑着哭i 提交于 2019-12-06 06:20:24
问题 I am trying to make a merge sort method, but it keeps on giving the wrong sorts. Where do I have change to make it actually sort the array? What part of the code has to be different? Thank you for your time. public static void mergeSort(int[] array, int left, int lHigh, int right, int rHigh) { int elements = (rHigh - lHigh +1) ; int[] temp = new int[elements]; int num = left; while ((left <= lHigh) && (right <= rHigh)){ if (a[left] <= array[right]) { temp[num] = array[left]; left++; } else {

Why is insertion sort always beating merge sort in this implementation?

霸气de小男生 提交于 2019-12-06 05:31:33
问题 I don't understand: why is my insertion sort implementation beating merge sort every time, for any size of n ? public List<Int32> InsertionSort(List<Int32> elements, Boolean ascending = true) { for (Int32 j = 1; j < elements.Count; j++) { Int32 key = elements[j]; Int32 i = j - 1; while (i >= 0 && (elements[i].CompareTo(key) > 0) == ascending) elements[i + 1] = elements[i--]; elements[i + 1] = key; } return elements; } public List<Int32> MergeSort(List<Int32> elements, Boolean ascending = true

Example how to use predsort(:Compare, +List, -Sorted) in prolog

不打扰是莪最后的温柔 提交于 2019-12-06 04:24:45
问题 I want to order a custom list. The list I want to order will be in this form... [n(_,2,_),n(_,1,_),n(_,3,_)] I have wrote a comparator cheaper(n(_,C1,_),n(_,C2,_)) :- C1>C2. How do I use this with predsort. I wrote a sorting algorithm using bubble sort, but I have very large lists so it very slow. Is it possible to do predsort(cheaper, [n(_,2,_),n(_,1,_),n(_,3,_)] , X). Thank you :) 回答1: Try this : cheaper(>, n(_,C1,_),n(_,C2,_)) :- C1>C2. cheaper(<, n(_,C1,_),n(_,C2,_)) :- C1<C2. cheaper(=,

Merge sort running time

匆匆过客 提交于 2019-12-05 20:23:41
I know that the running time of merge sort is O(n*lg(n)) and that merge sort is a comparision sort, which also means that it takes Ω(n logn) in the worst case to sort a list. Can I therefore conclude that the running time of merge sort is theta(n*lg n)? If something is O(X) and Omega(X) , this implies it is Theta(X) . And log_b1(...) is the same as log_b2(...) times a conversion factor constant. What you said was (translated): I know that the running time of merge sort is, in the worst case, no worse than n log(n) . [You arrived at this conclusion somehow with math.] But comparison sorts take

Why does memoization not improve the runtime of Merge Sort?

允我心安 提交于 2019-12-05 17:30:36
Why does memoization not improve the runtime of Merge Sort? I have this question from Assignment task. But as far as I know, Merge Sort uses divide and conquer approach (no overlapping subproblems) but Memoization is based on dynamic programing (with overlapping subproblem). I know the runtime of Merge Sort is O(nlogn) . I even searched on web search engines and there is no result for this question. Is this question wrong? If it sounds wrong but why would a professor give a wrong question in assignment? If the question is not wrong or my understanding about the question, Merge Sort and

How to Merge sort a Linked List with O(nlogn) time and O(1) space complexity

余生长醉 提交于 2019-12-05 17:05:34
(disclaimer: for school) As far as I know, recursively splitting a linked list, then sending it off to another function to merge is O(nlogn) time and O(n) space. Is it possible to do mergesort on linked list with O(nlogn) time and O(1) space complexity? How would you go about doing this? ANY HELP APPRECIATED PS: to make sure that the traditional mergesort is space complexity 0(n), this is an example of 0(n), right? How would it be changed for O(1) space? void sortTrack() { Node merge = this.head; this.head = Node (merge); } public Node mergeSort(Node head){ if ((head == null)||(head.next ==

What is the best in place sorting algorithm to sort a singly linked list

徘徊边缘 提交于 2019-12-05 07:01:35
I've been reading on in place sorting algorithm to sort linked lists. As per Wikipedia Merge sort is often the best choice for sorting a linked list: in this situation it is relatively easy to implement a merge sort in such a way that it requires only Θ(1) extra space, and the slow random-access performance of a linked list makes some other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible. To my knowledge, the merge sort algorithm is not an in place sorting algorithm, and has a worst case space complexity of O(n) auxiliary. Now, with this taken

Algorithms: Hybrid MergeSort and InsertionSort Execution Time

随声附和 提交于 2019-12-05 04:06:55
Good day SO community, I am a CS student currently performing an experiment combining MergeSort and InsertionSort. It is understood that for a certain threshold, S, InsertionSort will have a quicker execution time than MergeSort. Hence, by merging both sorting algorithms, the total runtime will be optimized. However, after running the experiment many times, using a sample size of 1000, and varying sizes of S, the results of the experiment does not give a definitive answer each time. Here is a picture of the better results obtained (Note that half of the time the result is not as definitive):

Implementing a mergesort without using an additional array?

一笑奈何 提交于 2019-12-05 01:11:58
问题 I've read a lot about mergesort recently and I wonder if there is a way to do a mergesort without using at least one additional array. Is it possible? 回答1: According to Wikipedia it is indeed possible, but might not yield any performance gain: Sorting in-place is possible (e.g., using lists rather than arrays) but is very complicated, and will offer little performance gains in practice, even if the algorithm runs in O( n log n ) time. In these cases, algorithms like heapsort usually offer

Implementing merge sort function in python class, errors

落花浮王杯 提交于 2019-12-04 20:44:52
So I have a function defined that works great at doing merge sort on a linear array if it is implemented by its lonesome, but if I put it into a class it bugs out. I think it's a great example of what I don't quite understand about how classes work; possibly in regards to namespace management(?). See below: def sort(array): print('Splitting', array) if len(array) > 1: m = len(array)//2 left = array[:m] right = array[m:] sort(left) sort(right) i = 0 j = 0 k = 0 while i < len(left) and j < len(right): if left[i] < right[j]: array[k] = left[i] i += 1 else: array[k] = right[j] j += 1 k += 1 while