Sort float Array Example /*    Java Sort float Array Example    This example shows how to sort a float array u...

Sort float Array Example

08:05 PASSOVE INCOME 0 Comments


                 Sort float Array Example

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

0 comments: