mergesort

What is the sequence of actions at a recursion?

痞子三分冷 提交于 2019-12-08 12:56:10
问题 I am trying to understand a merge sort algorithm java code but I really stuck at the splitting phase. Full code is here: public class Main { public static void main(String[] args) { int[] intArray = { 20, 35, -15, 7, 55, 1, -22 }; mergeSort(intArray, 0, intArray.length); for (int i = 0; i < intArray.length; i++) { System.out.println(intArray[i]); } } // { 20, 35, -15, 7, 55, 1, -22 } public static void mergeSort(int[] input, int start, int end) { if (end - start < 2) { return; } int mid =

Java recursion and Merge Sort

妖精的绣舞 提交于 2019-12-08 11:58:46
问题 I'm trying to write a simple merge sort program in Java, I'm seeing a lot of red in Eclipse. I'm still a beginner, and don't quite see whats wrong. thanks. -Kyle public class merge{ public static int[] mergeSub(int[] array, int left, int right){ if(left<right) { int mid = (left+right)/2; int[] a = mergeSub(array, left, mid); int [] b = mergeSub(array, mid+1, right); return merge(a, b); } int[] arr=new int[1]; arr[0]=arr[left]; return arr; } static int[] merge(int[] left, int[] right){ int

Runtime Complexity | Recursive calculation using Master's Theorem

放肆的年华 提交于 2019-12-08 07:50:44
问题 So I've encountered a case where I have 2 recursive calls - rather than one. I do know how to solve for one recursive call, but in this case I'm not sure whether I'm right or wrong. I have the following problem: T(n) = T(2n/5) + T(3n/5) + n And I need to find the worst-case complexity for this. (FYI - It's some kind of augmented merge sort) My feeling was to use the first equation from the Theorem, but I feel something is wrong with my idea. Any explanation on how to solve problems like this

Choosing minimum length k of array for merge sort where use of insertion sort to sort the subarrays is more optimal than standard merge sort

别来无恙 提交于 2019-12-08 06:11:11
问题 This is a question from Introduction to Algorithms By Cormen. But this isn't a homework problem instead self-study. There is an array of length n . Consider a modification to merge sort in which n/k sublists each of length k are sorted using insertion sort and then merged using merging mechanism, where k is a value to be determined. The relationship between n and k isn't known. The length of array is n . k sublists of n/k means n * (n/k) equals n elements of the array. Hence k is simply a

3 partition mergesort

北战南征 提交于 2019-12-08 05:11:20
问题 My professor assigned my class with implementing mergesort in arrays with 3-part partitioning and merging. That was the exact question from the professor. Problem is I have found no such thing as a 3-way mergesort I only know of a 3-way quicksort so I thought that he probably meant to take an array, split it into 3 parts and then mergesort those 3 parts together and I'm doing this by mergesorting the first 2 parts together and then mergesorting the combined part with the 3rd part. Did I think

Multi-threading a merge sorting algorithm

岁酱吖の 提交于 2019-12-08 04:30:21
问题 I have a class that does some recursive merge sorting on a generic List, as long as the element implements Comparable. I'm trying to make the code multi-threaded to improve performance, and to do this, I have a static variable maxThreads which keeps the number of threads I create from not exploding, and I have a static variable currentThreads that keeps track of the number of threads I currently have running. There seems to be a race condition on my currentThreads variable, but I haven't been

recurrence relation on a Merge Sort algorithm

[亡魂溺海] 提交于 2019-12-08 02:01:54
问题 The question is : UNBALANCED MERGE SORT is a sorting algorithm, which is a modified version of the standard MERGE SORT algorithm. The only difference is that instead of dividing the input into 2 equal parts in each stage, we divide it into two unequal parts – the first 2/5 of the input, and the other 3/5. a. Write the recurrence relation for the worst case time complexity of the UNBALANCED MERGE SORT algorithm. b. What is the worst case time complexity of the UNBALANCEDMERGESORT algorithm?

Merge sort running time

回眸只為那壹抹淺笑 提交于 2019-12-07 13:11:14
问题 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)? 回答1: 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

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

旧城冷巷雨未停 提交于 2019-12-07 06:59:02
问题 (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

Sorting a linked list in Java

社会主义新天地 提交于 2019-12-06 18:28:12
问题 I have written a bubble sort algorithm to sort a linked list. I am a Java beginner and trying to learn data structures. I am confused why my second element is not sorted properly. EDIT class SListNode { Object item; SListNode next; SListNode(Object obj) { item = obj; next = null; } SListNode(Object obj, SListNode next) { item = obj; this.next = next; } } public class SList { private SListNode head; private SListNode temp; public void sortList() { SListNode node = head,i,j; head = node; i =