mergesort

Batcher's odd-even-merge sort

瘦欲@ 提交于 2020-01-24 20:16:24
问题 Hi I have a question about Batcher's odd-even-merge sort. I have the following code: public class Batcher { public static void batchsort(int a[], int l, int r) { int n = r-l+1; for (int p=1; p<n; p+=p) for (int k=p; k>0; k/=2) for (int j=k%p; j+k<n; j+=(k+k)) for (int i=0; i<n-j-k; i++) if ((j+i)/(p+p) == (j+i+k)/(p+p)) exch(a, l+j+i, l+j+i+k); } public static void main(String[] args) { int a[] = new int[] {2, 4, 3, 4, 6, 5, 3}; batchsort(a, 0, a.length - 1); for (int i=0; i<a.length; i++) {

Batcher's odd-even-merge sort

冷暖自知 提交于 2020-01-24 20:16:05
问题 Hi I have a question about Batcher's odd-even-merge sort. I have the following code: public class Batcher { public static void batchsort(int a[], int l, int r) { int n = r-l+1; for (int p=1; p<n; p+=p) for (int k=p; k>0; k/=2) for (int j=k%p; j+k<n; j+=(k+k)) for (int i=0; i<n-j-k; i++) if ((j+i)/(p+p) == (j+i+k)/(p+p)) exch(a, l+j+i, l+j+i+k); } public static void main(String[] args) { int a[] = new int[] {2, 4, 3, 4, 6, 5, 3}; batchsort(a, 0, a.length - 1); for (int i=0; i<a.length; i++) {

Benchmarking quicksort and mergesort yields that mergesort is faster

醉酒当歌 提交于 2020-01-24 13:57:05
问题 I've tried benchmarking and for some reason when trying both of them on array of 1M elements the Mergesort sorted it in 0.3s and Quicksort took 1.3s. I've heard that generally quicksort is faster, because of its memory management, but how would one explain these results? I am running MacBook Pro if that makes any difference. The input is a set of randomly generated integers from 0 to 127. The codes are in Java: MergeSort: static void mergesort(int arr[]) { int n = arr.length; if (n < 2)

Interview question: Merge two sorted linked list without creating a new list [closed]

梦想的初衷 提交于 2020-01-24 01:52:06
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 6 days ago . How to merge sorted list l1 and l2 recursively? 回答1: public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null) { return l2; } if (l2 == null) { return l1; } ListNode result = l1.val > l2.val ? l2 : l1; ListNode cur = result; ListNode h1 = result.next; ListNode h2 =

What is wrong in this implementation of merge sort?

元气小坏坏 提交于 2020-01-17 03:50:05
问题 I know that there are many implementations of merge sort but this is one which I have read in the book "Introduction to algorithms". The following code is an implementation of merge sort which is not working correctly: #include <iostream> using namespace std; void merge(int*a, int p, int q, int r) { //function to merge two arrays int n1 = (q - p); // size of first sub array int n2 = (r - q); // size of second subarray int c[n1], d[n2]; for (int i = 0; i <= n1; i++) { c[i] = a[p + i]; } for

What is wrong in this implementation of merge sort?

我只是一个虾纸丫 提交于 2020-01-17 03:50:02
问题 I know that there are many implementations of merge sort but this is one which I have read in the book "Introduction to algorithms". The following code is an implementation of merge sort which is not working correctly: #include <iostream> using namespace std; void merge(int*a, int p, int q, int r) { //function to merge two arrays int n1 = (q - p); // size of first sub array int n2 = (r - q); // size of second subarray int c[n1], d[n2]; for (int i = 0; i <= n1; i++) { c[i] = a[p + i]; } for

Different answers if I use Vector instead of Array while counting Inversions, what could be the cause?

非 Y 不嫁゛ 提交于 2020-01-16 10:08:27
问题 I've been trying to do the count inversions question using mergesort for the past 2-3 days and after much trying, I just picked up the answer from Hackerrank's editorial, now their code is using an Array , and if I use a Vector instead of an Array , the answer is Actual answer + 1 (or different to say the least haven't tried it on many cases). I was wondering what might be the reason for it. I also have another question on explanation of this code, in particular the variable declarations and

MergeSort algorithm in c#

不问归期 提交于 2020-01-15 03:31:05
问题 I wrote below code for Merge Class : class Merge { public static void sort(IComparable[] a) { sort(a, 0, a.Length); } public static void sort(IComparable[] a, int low, int high) { int N = high - low; if (N <= 1) return; int mid = low + N / 2; sort(a, low, mid); sort(a, mid, high); IComparable[] aux = new IComparable[N]; int i = low, j = mid; for (int k = 0; k < N; k++) { if (i == mid) aux[k] = a[j++]; else if (j == high) aux[k] = a[i++]; else if (a[j].CompareTo(a[i]) < 0) aux[k] = a[j++];

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

て烟熏妆下的殇ゞ 提交于 2020-01-13 09:28:07
问题 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

Python class to merge sorted files, how can this be improved?

随声附和 提交于 2020-01-10 14:34:00
问题 Background: I'm cleaning large (cannot be held in memory) tab-delimited files. As I clean the input file, I build up a list in memory; when it gets to 1,000,000 entries (about 1GB in memory) I sort it (using the default key below) and write the list to a file. This class is for putting the sorted files back together. It works on the files I have encountered thus far. My largest case, so far, is merging 66 sorted files. Questions: Are there holes in my logic (where is it fragile)? Have I