Languages
[Edit]
EN

Java - merge sort implementation algorithm (custom)

6 points
Created by:
ParaEagle
724

Merge sort is a D&C (Divide and Conquer) algorithm. Like quick sort average time complexity is O(n log n). But the difference is that merge sort worst time complexity is also O(n log n) as quick sort O(n^2). Qucik sort adventage over merge sort is that it requires less memory, thus quick sort has better space complexity.

Java - merge sort implementation

Edit

Output:

Complex unit test with order assertion

Edit

Compare speed

Edit

Now we will compare speed of:

  • buble sort
  • this article merge sort impelmentation
  • quick sort - JDK implementation - Arrays.sort()

Bubble sort only on 50_000 elements array as it takes more then 4 secounds.

Merge sort and quick sort on array of 5_000_000 elements and we use the same seed with Random class to generate the same random numbers to have good comparing test.

References

Edit

Alternative titles

  1. How to implement own merge sort algorithm in java?
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join