Java Sort double Array Example

  1. /*
  2.   Java Sort double Array Example
  3.   This example shows how to sort a double array using sort method of Arrays class of
  4.   java.util package.
  5. */
  6.  
  7. import java.util.Arrays;
  8.  
  9. public class SortDoubleArrayExample {
  10.  
  11. public static void main(String[] args) {
  12.  
  13. //create double array
  14. double[] d1 = new double[]{3,2,5,4,1};
  15.  
  16. //print original double array
  17. System.out.print("Original Array :\t ");
  18. for(int index=0; index < d1.length ; index++)
  19. System.out.print(" " + d1[index]);
  20.  
  21. /*
  22.   To sort java double array use Arrays.sort() method of java.util package.
  23.   Sort method sorts double array in ascending order and based on quicksort
  24.   algorithm.
  25.   There are two static sort methods available in java.util.Arrays class
  26.   to sort a double array.
  27.   */
  28.  
  29. //To sort full array use sort(double[] d) method.
  30. Arrays.sort(d1);
  31.  
  32. //print sorted double array
  33. System.out.print("\nSorted double array :\t ");
  34. for(int index=0; index < d1.length ; index++)
  35. System.out.print(" " + d1[index]);
  36.  
  37. /*
  38.   To sort partial double array use
  39.   sort(double[] d, int startIndex, int endIndex) method where startIndex is
  40.   inclusive and endIndex is exclusive
  41.   */
  42.  
  43. double[] d2 = new double[]{5,2,3,1,4};
  44. Arrays.sort(d2,1,4);
  45.  
  46. //print sorted double array
  47. System.out.print("\nPartially Sorted double array :\t ");
  48. for(int index=0; index < d2.length ; index++)
  49. System.out.print(" " + d2[index]);
  50.  
  51. }
  52. }
  53.  
  54. /*
  55. Output Would be
  56.  
  57. Original Array : 3.0 2.0 5.0 4.0 1.0
  58. Sorted double array : 1.0 2.0 3.0 4.0 5.0
  59. Partially Sorted double array : 5.0 1.0 2.0 3.0 4.0
  60. */

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!