Sorting programs in java programming

 

Sorting is a process that arrange the item based on their size in ascending or descending order. We will use the sorting process in Java programing . In this page you will see a several types of java program in sorting.

   First we will see a java program to sort a group of whole numbers . The whole number is nothing but it is anInteger. 

CODE TO SORT AN INTEGER 

import java.util.Arrays;
public class sortingnumber{
    public static void main(String args[])
    {
        int[] arr={
            9,8,7,6,5,4,3,2,1
        };
      System.out.println("Before sorting the values in array "+Arrays.toString(arr));
      //arraysorting
      Arrays.sort(arr);
      System.out.println("after sorting the values in array "+Arrays.toString(arr));
    }
}

OUTPUT:

Before sorting the values in array
 [9, 8, 7, 6, 5, 4, 3, 2, 1]
After sorting the values in array
 [1, 2, 3, 4, 5, 6, 7, 8, 9]

CODE TO SHORT AH STRING:
import java.util.Arrays;
public class sortingnames{
    public static void main(String args[])
    {
        String[] arr={
           "Zebra" ,
           "vishal"  ,
           "ragul",
           "mohan",
            "gokul",
            "dhanush",
            "dhanush"
        };
      System.out.println("Before sorting the values in array "+Arrays.toString(arr));
      //arraysorting
      Arrays.sort(arr);
      System.out.println("after sorting the values in array "+Arrays.toString(arr));
    }
}

OUTPUT:

Before sorting the values in array
 [Zebra, vishal, ragul, mohan, gokul, dhanush, dhanush]

After sorting the values in array 
[Zebra, dhanush, dhanush, gokul, mohan, ragul, vishal]


EXPLANATION:

In the line 1 we will import the array because we will store the data in the array. At next we will declare an array with an integer at the name arr. In arr we will store our input values. Using the println statement we will print the array values after shorting. Using Arrays.sort()  this statement we will sort the array values in ascending order. At the next line we will use the println statement that prints the values in the arr after shorting.

1 Comments

Post a Comment

Post a Comment

Previous Post Next Post