Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays.sort(Object[] a) - how is it implemented?

Are there any resources on how the mergeSort used by Arrays.sort(Object[] a) is implemented? While it is documented quite good, I have a hard time understanding it (especially why the src and dest are are switched when mergeSort() get's recursively called).

like image 296
helpermethod Avatar asked Feb 07 '10 19:02

helpermethod


1 Answers

Here is the source of java.util.Arrays.

Actually, you have that source code in the JDK - just open java.util.Arrays in your IDE and the source code + the comments will appear. If you don't have an IDE, look at JDK_HOME\src.zip

Then, put it in your IDE and trace how it works.

  • put breakpoints (and run a program in debug mode)
  • use System.out.println(..)
  • change parts of it to see how they are reflected.
  • read the wikipedia article about merge sort
  • pay attention to this comment: // Recursively sort halves of dest into src
like image 119
Bozho Avatar answered Nov 03 '22 08:11

Bozho