mergesort

performance problems in parallel mergesort C++

[亡魂溺海] 提交于 2019-12-17 19:04:24
问题 I have tried to write a parallel implementation of mergesort using threads and templates. The relevant code is listed below. I have compared the performance with sort from the C++ STL. My code is 6 times slower than std::sort when no threads are spawned. Playing with the variable maxthreads (and/or FACTOR) I was able to only double the performance, so that in the best case I am 3 times slower than std::sort. I have tried the code on a 16 core multiprocessor machine. htop shows that the cores

sorting a doubly linked list with merge sort

做~自己de王妃 提交于 2019-12-17 10:01:15
问题 I have found this code in the internet and it was for arrays ,I want to change it for doubly linked list(instead of index we should use pointer) would you please help me that how can i change merge method(I have changed sort method by myself) also this is not my home work ,I love working with linked list!! public class MergeSort { private DoublyLinkedList LocalDoublyLinkedList; public MergeSort(DoublyLinkedList list) { LocalDoublyLinkedList = list; } public void sort() { if

Regarding in-place merge in an array

陌路散爱 提交于 2019-12-17 09:31:19
问题 I came across the following question. Given an array of n elements and an integer k where k < n . Elements { a 0 ... a k } and { a k +1 ... a n } are already sorted. Give an algorithm to sort in O( n ) time and O(1) space. It does not seem to me like it can be done in O( n ) time and O(1) space. The problem really seems to be asking how to do the merge step of mergesort but in-place. If it was possible, wouldn't mergesort be implemented that way? I am unable to convince myself though and need

Why is quicksort better than mergesort?

前提是你 提交于 2019-12-17 02:04:11
问题 I was asked this question during an interview. They're both O(nlogn) and yet most people use Quicksort instead of Mergesort. Why is that? 回答1: Quicksort has O( n 2 ) worst-case runtime and O( n log n ) average case runtime. However, it’s superior to merge sort in many scenarios because many factors influence an algorithm’s runtime, and, when taking them all together, quicksort wins out. In particular, the often-quoted runtime of sorting algorithms refers to the number of comparisons or the

merging N sorted files using K way merge

雨燕双飞 提交于 2019-12-13 15:12:23
问题 There is decent literature about merging sorted files or say merging K sorted files. They all work on the theory that first element of each file is put in a Heap, then until the heap is empty poll that element, get another from the file from where this element was taken. This works as long as one record of each file can be put in a heap. Now let us say I have N sorted files but I can only bring K records in the heap and K < N and let us say N = Kc where "c" is the multiplier implying that N

Merge and sort a list using merge sort

安稳与你 提交于 2019-12-13 13:27:57
问题 This is my code def merge_lists(all_lst): if len(all_lst) < 2: return all_lst[0] # get rid of the extra [] left = merge_lists(all_lst[:len(all_lst)//2]) #[[2, 7, 10]] ##[[0, 4, 6]] right = merge_lists(all_lst[len(all_lst)//2:])#[[0, 4, 6], [3, 11]] ##[[3,11]] def merge(left,right): results = [] while left and right: if left[0] < right[0]: results.append(left[0]) left.remove(left[0]) else: results.append(right[0]) right.remove(right[0]) results.extend(left) results.extend(right) return results

Count number of different values in chosen (large) range in VBA?

霸气de小男生 提交于 2019-12-13 13:10:57
问题 How can I count the number of different values (numbers and strings mixed) in a chosen (large) range in VBA? I think about this in this way: 1. Read in data into one dimensional array. 2. Sort array (quick or merge sort) need to test which 3. Simply count number of different values if sorted array : if(a[i]<>a[i+1]) then counter=counter+1 . Is it the most efficient way to solve this problem? Edit: I want to do it in Excel. 回答1: Here is a VBA Solution You don't need an Array to get this done.

Implementing mergesort on a linked list

南楼画角 提交于 2019-12-13 10:33:06
问题 I was tasked with implementing a merge sort algorithm on a list written in C/C++. I have the general idea down, wrote my code and have successfully compiled it. However, when I run it, it will begin fine but then hang on "prepared list, now starting sort" without giving any kind of error. I have tried to look through my code but I am at a complete loss as to what the issue could be. I am also pretty amateurish with debugging, so using gdb to the best of my abilities has lead me no where. Any

mergesort algorithm in c++

爱⌒轻易说出口 提交于 2019-12-13 08:49:51
问题 i have following code #include <iostream> using namespace std; void merge(int c[],int a[],int n,int b[],int m){ for (int i=0,j=0,k=0;k<n+m;k++){ if (i==n) { c[k]=b[j++]; continue;} if (j==m ){ c[k]=a[i++];continue;} c[k]=(a[i]<b[j])? a[i++]:b[j++]; } } void mergesort(int a[],int b[],int l,int r){ if (l>r) return ; int m=(l+r)/2; mergesort(b,a,l,m); mergesort(b,a,m+1,r); merge(a+l,b+l,m-l+1,b+m+1,r-m); } int main(){ int a[]={12,4,2,5,3,6,7,8,10,11}; const int n=sizeof(a)/sizeof(int); int b[n];

Modification to merge sort to implement merge sort with insertion sort Java

一笑奈何 提交于 2019-12-13 02:51:32
问题 I want to implement a modification to merge sort, where n/k sublists of length k are sorted using insertion sort and then merged using the standard merging mechanism of merg sort. I'm wondering what the value k has to equal for the modified version of merge sort to equal the original version of merge sort in terms of rum time complexity. This is a conceptual exercise by myself for myself. Code and or an explanation is appreciated. 回答1: Your n/k-way merge is O(n^2/k) (explanation here). Each