-1

I have BookData, which has int id and String name

Coding is

bookData book = new bookData(1,"Ethan");
bookData book = new bookData(3,"Queen");
bookData book = new bookData(2,"BOB")

ArrayList al = new ArrayList();
al.add(book);

how can i use merge sort to sort id in this ArrayList?

I really need to get this done without using Collections class and Arrays Class

3
  • 1
    You can check this stackoverflow.com/questions/6818683/… Commented Dec 4, 2018 at 5:52
  • bookData need change to BookData,it's a very bad practice Commented Dec 4, 2018 at 5:54
  • 3
    The question doesn't appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, and show a specific roadblock you're running into with Minimal, Complete, and Verifiable example. For more information, please see How to Ask. Commented Dec 4, 2018 at 5:54

1 Answer 1

0

Besides using the correct syntaxes, you can perform List.sort as:

List<BookData> al = new ArrayList<>(); // initialise as you will ; renamed class name as 'BookData'
al.sort(Comparator.comparingInt(bookData::getId));

which performs an iterative merge sort on its own. From its implementation note -

This implementation is a stable, adaptive, iterative mergesort that requires far fewer than n lg(n) comparisons when the input array is partially sorted, while offering the performance of a traditional mergesort when the input array is randomly ordered. If the input array is nearly sorted, the implementation requires approximately n comparisons. Temporary storage requirements vary from a small constant for nearly sorted input arrays to n/2 object references for randomly ordered input arrays.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.