The Artima Developer Community
Sponsored Link

Java Buzz Forum
4 Examples to Sort Array in Java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Javin Paul

Posts: 1090
Nickname: javinpaul
Registered: Jan, 2012

Javin Paul is Java Programmer working on Finance domain.
4 Examples to Sort Array in Java Posted: Aug 13, 2014 7:23 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Javin Paul.
Original Post: 4 Examples to Sort Array in Java
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
Latest Java Buzz Posts
Latest Java Buzz Posts by Javin Paul
Latest Posts From Java67

Advertisement
You can use Arrays.sort() method to sort both primitive and object array in Java. This method sorts given array into ascending order, which is numeric order for primitives and defined by compareTo() or compare() method for objects. For primitive arrays e.g. int,  short, character, float, double or long this method uses  dual-pivot Quicksort sorting algorithm implemented by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloach (author of Effective Java) . This algorithm offers O(n log(n)) performance on many data sets that cause other quicksort algorithms to degrade into their worst quadratic performance e.g. O(n^2), and is typically faster than traditional (one-pivot) Quicksort implementations. That's why I always said that prefer library method your own, you can get it right but amount of exposure library method gets, you will never get for your implementations. On the other hand object array is sorted using stable MergeSort algorithm, which ensures that equal elements keep their original position in sorted array. Implementation of mergesort used in sort(Object[]) is stable, adaptive, and iterative that requires much lesser than O(n log(n)) comparisons when the input array is partially sorted, while offering the performance of a traditional mergesort when the input array is randomly ordered. In best case, when input array is almost sorted, this implementation requires approximately O(n) comparisons. By the way temporary storage requirements vary from a small constant for nearly sorted input arrays to n/2 object references for randomly ordered input arrays. In order to sort different types of array in Java, you can use any of the overloaded version of sort() method from Arrays class. It also has two special method for sorting object array, one sorts the array in natural order, while other sort them in custom order of provided comparator. Since two dimensional array is also array of array in Java, you can use any this method to sort multi dimensional array in Java also. We will see step by step examples of sorting all kinds of array in Java in subsequent section.
Read more »

Read: 4 Examples to Sort Array in Java

Topic: IntelliJ IDEA 14 EAP 138.1503.3 is Out Previous Topic   Next Topic Topic: Difference between POST and GET Request in HTTP Protocol

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use