in Education by
I've got a large array of primitive types (double). How do I sort the elements in descending order? Unfortunately the Java API doesn't support sorting of primitive types with a Comparator. The first approach that probably comes to mind is to convert it to a list of objects (boxing): double[] array = new double[1048576]; Arrays.stream(array).boxed().sorted(Collections.reverseOrder())… However, boxing each primitive in the array is too slow and causes a lot of GC pressure! Another approach would be to sort and then reverse: double[] array = new double[1048576]; ... Arrays.sort(array); // reverse the array for (int i = 0; i < array.length / 2; i++) { // swap the elements double temp = array[i]; array[i] = array[array.length - (i + 1)]; array[array.length - (i + 1)] = temp; } This approach is also slow - particularly if the array is already sorted quite well. What's a better alternative? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I think it would be best not to re-invent the wheel and use Arrays.sort(). Yes, I saw the "descending" part. The sorting is the hard part, and you want to benefit from the simplicity and speed of Java's library code. Once that's done, you simply reverse the array, which is a relatively cheap O(n) operation. Here's some code I found to do this in as little as 4 lines: for (int left=0, right=b.length-1; left<right; left++, right--) { // exchange the first and last int temp = b[left]; b[left] = b[right]; b[right] = temp; }

Related questions

0 votes
    Currently, I'm using Spring boot with DynamoDB and using DynamoDBMapper for all DB operation. Right now it' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    How can I sort an event by id in DESC order? My all events are coming and by default they are ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    How can I sort an event by id in DESC order? My all events are coming and by default they are ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    Which of these method of Array class is used sort an array or its subset? (a) binarysort() (b) ... Collections Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Columns can be arranged in descending order too by using the special ____ operator. (a) asc() (b) desc( ... Operations of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    How is Arrays.asList() different than the standard way of initialising List? (a) Both are same (b) ... Collections Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which classes allow primitive types to be accessed as objects? Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    Which of the following options are the primitive field types in Google BigQuery schema? (1)All of the options (2)FLOAT (3)INTEGER (4)TIMESTAMP (5)STRING (6)BOOLEAN...
asked May 31, 2021 in Education by JackTerrance
0 votes
    Which one is the correct order of phases in JSP life cycle? (a) Initialization, Cleanup, Compilation, Execution (b) ... & API of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    HOW DO I SORT THIS BY BIRTHYEAR IN ASCENDING ORDER? I'm distance learning student and have no help. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    Which of the following sort a dataframe by the order of the elements in B? (a) x[rev(order(x$B)), ... Regression of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    What is the default SORT order for a SQL? (a) Ascending (b) Descending (c) As specified by ... and Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    Which of these are types of multitasking? (a) Process based (b) Thread based (c) Process and Thread ... chapter Multithreading of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Is there a way to add member functions to a Drools declared type? That is, I'd like to say ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Which of the following is an incorrect statement regarding the use of generics and parameterized types in Java? (a ... Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
...