Java Program to Implement Sorted Array

This Java program is to Implement Sorted array. A sorted array is an array data structure in which each element is sorted in numerical, alphabetical, or some other order, and placed at equally spaced addresses in computer memory.

Here is the source code of the Java program to implement sorted array. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.Arrays;
  2.  
  3. public class SortedArray<T>
  4. {
  5.     private T[] array;
  6.  
  7.     public SortedArray(T[] array)
  8.     {
  9.         this.array = array;
  10.     }
  11.  
  12.     public void sort()
  13.     {
  14.         Arrays.sort(array);
  15.     }
  16.  
  17.     public T[] getArray()
  18.     {
  19.         return array;
  20.     }
  21.  
  22.     public static void main(String...arg)
  23.     {
  24.         Integer[] inums = {10,9,8,7,6};
  25.         Float[] fnums = {23.9f,5.5f,10.8f,2.5f,82.0f};
  26.         Double[] dnums = {12.5,244.92,1.9,98.3,35.2};
  27.         String[] strings = {"banana","pineapple","apple","mango","jackfruit"};
  28.  
  29.         System.out.println("The Values Before sorting");
  30.         System.out.println();
  31.  
  32.         System.out.println("Integer Values");
  33.         for (int i = 0; i < inums.length; i++)
  34.             System.out.print(inums[i] + "\t");
  35.  
  36.         System.out.println();
  37.         System.out.println("Floating Values");
  38.         for (int i = 0; i < fnums.length; i++)
  39.             System.out.print(fnums[i] + "\t");
  40.  
  41.         System.out.println();
  42.         System.out.println("Double Values");
  43.  
  44.         for (int i = 0; i < dnums.length; i++)
  45.             System.out.print(dnums[i] + "\t");
  46.  
  47.         System.out.println();
  48.         System.out.println("String Values");
  49.  
  50.         for (int i = 0; i < strings.length; i++)
  51.             System.out.print(strings[i] + "\t");
  52.  
  53.         SortedArray<Integer> integer = new SortedArray<Integer>(inums);
  54.         SortedArray<Float> floating = new SortedArray<Float>(fnums);
  55.         SortedArray<Double> doubles = new SortedArray<Double>(dnums);
  56.         SortedArray<String> string = new SortedArray<String>(strings);
  57.  
  58.         integer.sort();
  59.         floating.sort();
  60.         doubles.sort();
  61.         string.sort();
  62.  
  63.         inums = integer.getArray();
  64.         fnums = floating.getArray();
  65.         dnums = doubles.getArray();
  66.         strings = string.getArray();
  67.  
  68.         System.out.println();
  69.         System.out.println("The Values After sorting");
  70.         System.out.println();
  71.         System.out.println("Integer Values");
  72.         for (int i = 0; i < inums.length; i++)
  73.             System.out.print(inums[i] + "\t");
  74.  
  75.         System.out.println();
  76.         System.out.println("Floating Values");
  77.         for (int i = 0; i < fnums.length; i++)
  78.             System.out.print(fnums[i] + "\t");
  79.  
  80.         System.out.println();
  81.         System.out.println("Double Values");
  82.         for (int i = 0; i < dnums.length; i++)
  83.             System.out.print(dnums[i] + "\t");
  84.  
  85.         System.out.println();
  86.         System.out.println("String Values");
  87.         for (int i = 0; i < strings.length; i++)
  88.             System.out.print(strings[i] + "\t");
  89.     }	
  90. }

$javac SortedArray.java
$java SortedArray
 
The Values Before sorting
 
Integer Values
10	9	8	7	6	
Floating Values
23.9	5.5	10.8	2.5	82.0	
Double Values
12.5	244.92	1.9	98.3	35.2	
String Values
banana	pineapple	apple	mango	jackfruit	
 
The Values After sorting
 
Integer Values
6	7	8	9	10	
Floating Values
2.5	5.5	10.8	23.9	82.0	
Double Values
1.9	12.5	35.2	98.3	244.92	
String Values
apple	banana	jackfruit	mango	pineapple

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

👉 For weekly programming practice and certification updates, join Sanfoundry’s official WhatsApp & Telegram channels

advertisement
Manish Bhojasia – Founder & CTO at Sanfoundry

I’m Manish, Founder & CTO at Sanfoundry, with 25+ years of experience across Linux systems, SAN technologies, advanced C programming, and building large-scale, performance-driven learning and certification platforms focused on clear skill validation.

LinkedIn  ·  YouTube MasterClass  ·  Telegram Classes  ·  Career Guidance & Conversations