FAQ Database Discussion Community


Why Merge sort is used for objects in Android/Java API?

java,android,sorting,quicksort,mergesort
In Java Arrays.sort() for primitive type uses quick sort. On the other hand Arrays.sort() for objects uses Merge sort. And, same goes for Collection.sort() which also uses Merge sort. Collections sort uses Arrays sort implementation underneath. So, in simple sense i can say that primitives are sorted using quick sort...

Can't press Ctrl + D to reach EOF

c,arrays,sorting,pointers,mergesort
Hi I'm new to C so still being confused about a few things but getting there. I'm trying to implement mergesort and i think I've implemented it correctly. When I compile and run the program it waits for me to give it input. That's fine I'll give it 5 4...

Finding the constant c in the time complexity of certain algorithms

algorithm,sorting,time-complexity,mergesort,insertion-sort
I need help finding and approximating the constant c in the complexity of insertion sort (cn^2) and merge sort (cnlgn) by inspecting the results of their running times. A bit of background, my purpose was to "implement insertion sort and merge sort (decreasing order) algorithms and measure the performance of...

Merge Sort algorithm isn't merging properly

vba,pseudocode,mergesort
So, I do know what a merge sort is supposed to do, and I can somewhat visualize it now. Recursively splitting until only one element is left in the array, since an array of one element is already sorted, it reduces the amount of work needed for each recursion, and...

Random values when using merge in merge sort C++

c++,arrays,sorting,mergesort
For a small homework assignment, I'm supposed to write a simple merge function whose prototype looks like this: void merge(int a[], int left_low, int left_high, int right_low, int right_high) The directions say that for simplicity's sake, we are only taking in a single array, a[] and that right_low = left_high...

Mergesort Algorithm Using n/2 Extra Memory Space

algorithm,sorting,mergesort
Mergesort algorithm uses n(size of input) extra memory space. I would like to know whether it is possible to reduce extra memory space from n to n/2.

MergeSort on Linked List

c++,linked-list,mergesort
I'm fairly bad at coding so I really need some help as this homework is dued by Wednesday. I have seen mergesort normally split into three functions: (mergesort, sortedmerge, frontbacksplit, etc). But my professor's array based mergesort is done in one function so I'm assuming he wants us to only...

Merge Sorting in Objective C

objective-c,mergesort
I am trying to implement merge sort in objective -C. This is a similar question asked in the following link , did not find it answered so creating a new question. Merge sort in Objective-C This is what I have tried , -(NSArray *)mergeSort:(NSArray *)unsortedArray { if ([unsortedArray count] <...

Merge sort cuasing stack to overflow?

c++,linked-list,stack-overflow,mergesort
I wrote mergesort() in C++ for linked lists. The issue is that my professor has provided test code with a very large list (length of 575,000). This causes a stack overflow error for my function since it is written recursively. So it's possible my professor expects us to write it...

Merge Sort Output isn't Correct

c++,mergesort
So, I have had a few issues already but I think I have made significant progress on my own & have tried many things. However I can not get this to output the correct values I conceptually understand merge sort, but coding it is confusing me. So... If I input:...

Not entering while loop? [closed]

c++,while-loop,mergesort,insertion-sort
[EDIT] - Nevermind, this is a really dumb question I'm currently trying to create a program that will basically tell me when insertion sort takes a longer time than merge sort for given a, b and n (in this case, successive powers of 2). This is what I have so...

Is this a correctly implemented mergesort in Haskell?

algorithm,sorting,haskell,functional-programming,mergesort
I could not find my code anywhere on the net, so can you please tell me why or why not the function myMergeSort is a mergesort? I know my function myMergeSort sorts, but am not sure if it really sorts using the mergesort algorithm or if it is a different...

Implementation of mergesort

c++,implementation,mergesort
I'm writing a mergesort on list (don't look at the functions, I still build them). The problem is following: compilator can't read an induction of merge_sort, I don't know why. I'd like to ask you for help. Below is shown the code. #include <iostream> #include <cstdlib> #include <iomanip> #include <time.h>...

Inversions in Array

c++,mergesort
I am trying to count the number of inversions in this array and I am really not sure where to start the count. I have tried many different spots and I thought this would be the right place to increment the counter, but I am mistaken because the output is...

Repeating steps in merge sort

c#,algorithm,sorting,mergesort
I am trying to dig into some algorithms and stuck with merge sorting. I mean it works but I am logging the steps of this algorithm and it seams that it repeats same steps for same subintervals many times. Is this correct and is this the way this algorithm works?...

Determining number of calls (activations) to mergesort

algorithm,big-o,mergesort
I'm studying for a CS 125 final exam, and Big O Notation was covered (briefly). Given that: Mergesort has a Running Time Best Case of O(N lg(N)) and Running Time Worst Case of O(N lg (N)) There are 32 floating point values, initially in random order, stored in a double...

Simple Merge Sort bug in Python

python,list,python-3.x,recursion,mergesort
I'm doing a Merge Sort assignment in Python, but I keep have the error of RuntimeError: maximum recursion depth exceeded Here's my code: def merge_sort(list): left_num = len(list) // 2 left_sorted = merge_sort(list[:left_num]) right_sorted = merge_sort(list[left_num:]) final_sort = merge(left_sorted, right_sorted) return final_sort def merge(left_sorted, right_sorted): final_sort = [] while left_sorted...

Nested function crashing the Swift compiler

swift,generics,nested,mergesort,nested-generics
I wrote a recursive mergeSort function: func mergeSort<T: Comparable>(inout array: [T]) { if array.count <= 1 { return } var leftSlice = [T](array[0..<array.count / 2]) var rightSlice = [T](array[array.count / 2...array.endIndex - 1]) mergeSort(&leftSlice) mergeSort(&rightSlice) array = merge(leftSlice, rightSlice) } func merge<T: Comparable>(var left: [T], var right: [T]) -> [T]...

How do I properly implement merge sort

c++,sorting,mergesort
I have the merge sort code below which is not giving proper output. Input : 1 7 3 9 4 5 2 6 8 Output : 1 5 2 6 7 3 8 1 9. #include<iostream> #include<vector> using namespace std; void merge(vector<int>& array,vector<int>& helper,int low,int mid,int high) { int size...

Java: Mergesort for ArrayList does not work

java,sorting,mergesort
It does the comparison of Objects properly but doubles the size of list with duplicate objects. public static void mergeSort(List<Person> list) { List<Person> helper = new ArrayList<Person>(list.size()); mergeSort(list, helper, 0, list.size()); } private static void mergeSort(List<Person> list, List<Person> helper, int low, int high) { if(low < high) { int middle...

How to implement mergeSort?

java,sorting,mergesort
I am trying to implement a recursive mergesort but the method is writting only zeros in the sorted vector. Here is my code. At the main function i just read the values to a vector and pass it as argument to mergesort. Any ideias of what i am doing wrong?...

Merge sort does not split the right side of the list

c++,sorting,mergesort
Like the title states, my merge sort only splits the left side. Everything else works fine, including my merge function. I can't figure out why. I would really appreciate the help. In a list that includes: 7, 4, 10, 2, 6, 1, 3, 7, 11, 5 it outputs 1, 2,...

Runtime Complexity | Recursive calculation using Master's Theorem

recursion,mergesort,master-theorem
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...

Number of Inversion Count in an array. Using Merge Sort

c++,algorithm,mergesort
For those how not know about inversion. Inversion- Given an array A of N integers, an inversion of the array is defined as any pair of indexes (i,j) such that i < j and A[i] > A[j]. Inshort: {inv}(A) = {(A(i),A(j)), i < j { and } A(i) > A(j)}...

Merge Sorting Algorithms

java,algorithm,sorting,mergesort
i have been solving the problem related to merge sorting but dont know what is the problem in my program so can you please look on it? the question was to give a unsorted array and it will provide sorted without changing the original array. please let me know where...

My Array merge sorter does not work properly with odd number length of the array that includes some duplicate values

java,arrays,mergesort
My Array merge sorter does not work properly with odd number length of the array that includes some duplicate values. For example, for int[] array = {1, 3, 15, 3, 7, 9, 8, 15, 0} the result is {0, 1, 3, 3, 7, 8, 0, 9, 15,}. Can someone tell...

Merge Sort vs Selection Sort

java,algorithm,sorting,mergesort,selection-sort
I have written these 2 sorting algorithms and it appears that selection sort is faster than merge sort, surely this can't be right? My test data is 10 random arrays of size 5000 to 50000 where the largest possible numbers in the array is 100 Here is my selection sort...

Doubly Linked List, MergeSort, getting undefined and unreliable results

c,algorithm,data-structures,mergesort,doubly-linked-list
I try my best to look up solutions to problems before asking on here, but I've hit a bit of a brick wall. While there is one for Java, it didn't help me enough to understand where I went wrong with my implementation. So without further ado, here's some background,...

Mergesort on a linked-list using only one function, segmentation fault

c,sorting,linked-list,mergesort
I need to implement the function: struct listnode * mergesort(struct listnode *data) My professor provided the main() function testing code. I only need to submit the mergesort function. He told us to do it in C or C++ but the test code main() he gave us is in C. This...

What is wrong in this implementation of merge sort?

c++,algorithm,sorting,mergesort
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...

Implementing Merge Sort

c++,mergesort
So, I am trying to implement merge sort, which I conceptually understand but am obviously having great difficulty programming (I'm quite inexperienced). I have looked up previous posts which has helped, but I can't get past this point. I am currently having the following errors : 13:20: error: âSizeâ was...

recurrence relation on a Merge Sort algorithm

algorithm,data-structures,mergesort,recurrence
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...

merge sort recursion not clear how the sorting of array is achieved please help to understand the code [duplicate]

c++,mergesort
This question already has an answer here: Explanation of Merge Sort for Dummies 6 answers can someone help me to understand how the sorting is being done using recursion code of merge sort void merge_sort(int arr[],int low,int up) { int mid; int temp[MAX]; if(low<up)//if more than one element {...

Algorithms Asymptotic running times

algorithm,mergesort,asymptotic-complexity
What are the best case and worst case asymptotic running times for sorting an array of size n using mergesort ?...

Iterative Merge Sort, works same speed as Bubblesort

c++,sorting,iteration,mergesort
I have tried to implement an iterative Merge sort using nested loops. Although this algorithm does sort correctly (as in after sorting things are in correct order), I know there is something wrong with this implementation as I tried to sort larger collections with it and compare timings with slower...

Implementing merge sort algorithm in JavaScript

javascript,algorithm,sorting,merge,mergesort
I'm trying to implement merge sort algorithm in JavaScript. But I'm getting a strange behavior when it comes to merging two sorted arrays. When I pass those two arrays: [1,4, 5] and [3, 6, 7, 10] to the merge function, I always get this result: [ 1, 3, 4, 6,...

Extrapolating the runtime of mergesort?

sorting,time-complexity,mergesort
I'm trying to solve the following problem, which asks me to extrapolate the runtime of mergesort to larger inputs. Here's the problem: A user runs the code: int a=(int*) malloc (N*sizeof(int)); for (i=0; i<N; i++) { a[i] = rand(); } mergesort (a,N); This code generates and then orders random numbers...