Wednesday, December 24, 2014

Java Sort and char int and float and in java pro



Java Sort char Array

import java.util.Arrays;
public class SortCharArrayExample
{
public static void main(String[] args)
{
char[] c1 = new char[]{'d','a','f','k','e'};
System.out.print("Original Array :\t ");
for(int index=0; index < c1.length ; index++)
System.out.print("  "  + c1[index]);
Arrays.sort(c1);
System.out.print("\nSorted char array :\t ");
for(int index=0; index < c1.length ; index++)
System.out.print("  "  + c1[index]);
char[] c2 = new char[]{'d','a','f','k','e'};
Arrays.sort(c2,1,4);
System.out.print("\nPartially Sorted char array :\t ");
for(int index=0; index < c2.length ; index++)
System.out.print("  "  + c2[index]);
   }
}

Java Sort float Array

import java.util.Arrays;
public class SortFloatArrayExample
{
  public static void main(String[] args)
{
float[] f1 = new float[]{3f,2f,5f,4f,1f};
System.out.print("Original Array :\t ");
for(int index=0; index < f1.length ; index++)
System.out.print("  "  + f1[index]);
Arrays.sort(f1);
System.out.print("\nSorted float array :\t ");
for(int index=0; index < f1.length ; index++)
System.out.print("  "  + f1[index]);
float[] f2 = new float[]{5f,2f,3f,1f,4f};
Arrays.sort(f2,1,4);
System.out.print("\nPartially Sorted float array :\t ");
for(int index=0; index < f2.length ; index++)
System.out.print("  "  + f2[index]);
  }
}

Java Sort int Array

import java.util.Arrays;
public class SortIntArrayExample
{
public static void main(String[] args)
{
 int[] i1 = new int[]{3,2,5,4,1};
System.out.print("Original Array : ");
for(int index=0; index < i1.length ; index++)
System.out.print("  "  + i1[index]);
System.out.print("Sorted int array : ");
for(int index=0; index < i1.length ; index++)
System.out.print("  "  + i1[index]);
int[] i2 = new int[]{5,2,3,1,4};
Arrays.sort(i2,1,4);
System.out.print("Partially Sorted int array : ");
for(int index=0; index < i2.length ; index++)
System.out.print("  "  + i2[index]);
  }
}

No comments:

Post a Comment