  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # How to sort in-place using the merge sort algorithm?  » arrays » How to sort in-place using the merge sort algorithm?

By : Sayoni
Date : November 20 2020, 07:00 PM
Does that help Knuth left this as an exercise (Vol 3, 5.2.5). There do exist in-place merge sorts. They must be implemented carefully.
First, naive in-place merge such as described here isn't the right solution. It downgrades the performance to O(N2). code : ## What is the fastest simple sorting algorithm (except quick sort/merge sort) for 500 numbers?

By : Azharuddin
Date : March 29 2020, 07:55 AM
To fix the issue you can do I once compared some sorting algorithms. I found comb sort and heap sort are very easy to implement and give very good results.
code :
``````void comb_sort(double *a, int size) {
int gap = size;
bool swapped = false;
while ((gap > 1) || swapped) {
if (gap > 1) gap = int(gap/1.247330950103979);
swapped = false;
for (int i = 0; gap + i < size; i++)
if (a[i + gap] < a[i]) {
swap(&a[i + gap], &a[i]);
swapped = true;
}
}
}
`````` ## How to sort a big array consists of four sorted parts, preferably by merge sort algorithm?

By : LemonMan
Date : March 29 2020, 07:55 AM
Any of those help You don't need to do a merge sort. You just need to do the merge operations in-place 3 times (1st part merge 2nd part; 3rd part merge 4th part; then merge those two). Note that merge is an O(n) operation, which is what makes a merge sort work. So this algorithm will be O(n) + O(n) + O(n) = O(n).
O(n) is optimal since you will need to access each element at least once no matter what. (So the problem, and this algorithm, are Omega(n) and Theta(n) as well, for what it's worth.) ## Why does my merge sort algorithm not sort my array correctly?

By : user3257278
Date : March 29 2020, 07:55 AM
around this issue I think the problem is in array reference in C# as @canton7 said, try this code:
code :
``````private static int[] data = new int[] { 1, 9, 10, 2, 4, 5, 6 };
private static int[] intArray;

static void Main()
{
int N = data.Length;
intArray = new int[N];
Sort(0, N - 1);
for (int i = 0; i < N; i++)
Console.WriteLine(intArray[i]);
}

private static void Merge(int lo, int mid, int hi)
{
int i = lo;
int j = mid + 1;
for (int k = lo; k <= hi; k++)
{
if (i > mid)
intArray[k] = data[j++];
else if (j > hi)
intArray[k] = data[i++];
else if (data[j] < data[i])
intArray[k] = data[j++];
else if (data[i] < data[j])
intArray[k] = data[i++];
}
if (intArray.Length != 0)
for (int k = lo; k <= hi; k++)
data[k] = intArray[k];
}

private static void Sort(int lo, int hi)
{
if (hi == lo)
return;
int mid = lo + (hi - lo) / 2;
Sort(lo, mid);
Sort(mid + 1, hi);
Merge(lo, mid, hi);
}
`````` ## Counting comparisons in selection sort, insertion sort, merge sort, and quick sort?

By : J.Young Tsang
Date : March 29 2020, 07:55 AM
This might help you Since your question is not marked as homework, I assume you mainly want to find out how many comparisons are made during sorting. For this, I would
not reinvent sorting but use highly efficient sorting from the Java library use an own Comparator that decorates an existing compare() or compareTo() to count the number of comparisons. ## How is shell sort algorithm any better than of merge sort algorithm?

By : supto
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further You have to remember the context in which shellsort was proposed: shellsort was published in 1959; quicksort, in 1961; mergesort, in 1948 (OK, that was a bit surprising). The computers of the day were slow and had small memories. Thus the asymptotic advantage of mergesort was hardly relevant compared to the increased complexity of implemention and code. In fact, shellsort gets the quadratic fallback of modern practical mergesorts for free, since insertion sorting with a gap of 1 is insertion sort.
It was not known then how to do an efficient in-place merge (and even now, no one implements it, because it's wildly inefficient in practice). 