Class 10 ICSE - Java Array Sorting Programs

Class 10th Java aims to empower students by enabling them to build their own applications introducing some effective tools to enable them to enhance their knowledge, broaden horizons, foster creativity, improve the quality of work and increase efficiency.
It also develops logical and analytical thinking so that they can easily solve interactive programs. Students learn fundamental concepts of computing using object oriented approach in one computer language with a clear idea of ethical issues involved in the field of computing
Class 10th java topics includes revision of class 9th, constructors, user-defined methods, objects and classes, library classes , etc.
Program 1: Write a program to enter numbers in array and sort them in ascending order using selection sort technique.
Solution:
import java.util.*;    
class SelectionSort 
{  
    public static void main(String args[]) 
    {  
        int i,j; 
        Scanner sc = new Scanner(System.in);  
        System.out.println("Enter Array Size : ");  
        int n = sc.nextInt();  
        int arr[] = new int[n]; 
        System.out.println("Enter Array Elements : ");  
        for(i=0; i < n; i++) 
        {  
            arr[i] = sc.nextInt();  
        } 
        System.out.println("Original Array is :");  
        for(i=0; i < n; i++) {  
            System.out.print(arr[i]+ "  ");  
        }
        System.out.println(" \nSorting Array using Selection Sort Technique " );  
        for(i= 0; i < n-1; i++) 
        {
            int index = i;
            for(j= i+1; j < n; j++) 
            {
                if(arr[j] < arr[index])
                {
                index = j; 
                }  
            } 
                int temp = arr[i];  
                arr[i] = arr[index];  
                arr[index] = temp;
        }
        System.out.println("Now the Array after Sorting is :");  
        for(i=0; i < n; i++) 
        {  
            System.out.print(arr[i]+ "  ");  
        }  
    }  
}
                                              
                                              
                    

Program 2: Write a program to enter numbers in array and sort them in descending order using selection sort technique.
Solution:
   import java.util.*;    
   class SelectionSort 
   {  
      public static void main(String args[]) 
      {  
          int i,j; 
          Scanner sc = new Scanner(System.in);  
          System.out.println("Enter Array Size : ");  
          int n = sc.nextInt();  
          int arr[] = new int[n]; 
          System.out.println("Enter Array Elements : ");  
          for(i=0; i < n; i++) 
          {  
              arr[i] = sc.nextInt();  
          } 
          System.out.println("Original Array is :");  
          for(i=0; i < n; i++) {  
              System.out.print(arr[i]+ "  ");  
          }
          System.out.println(" \nSorting Array using Selection Sort Technique " );  
          for(i= 0; i < n-1; i++) 
          {
              int index = i;
              for(j= i+1; j < n; j++) 
              {
                  if(arr[j] > arr[index])
                  {
                  index = j; 
                  }  
              } 
                  int temp = arr[i];  
                  arr[i] = arr[index];  
                  arr[index] = temp;
         }
          System.out.println("Now the Array after Sorting is :");  
          for(i=0; i < n; i++) 
          {  
              System.out.print(arr[i]+ "  ");  
          }  
      }  
   }
                         
                        

Program 3: Write a program to enter numbers in array and sort them in ascending order using bubble sort technique.
Solution:
  import java.util.*;
  class bubblesort
  {
      public static void main(String args[])
      {   int temp;
          Scanner sc=new Scanner(System.in);
          System.out.println("Enter Array Size : ");  
           int n = sc.nextInt();  
           int a[] = new int[n];
           System.out.println("Enter Array Elements : ");  
          for(int i=0;i< n;i++)
          {
              System.out.println("Enter the "+(i+1)+" element:");
              a[i]=sc.nextInt();
          }
          //display array
          System.out.println("Original array is:");
          for (int i = 0; i < n; i++)
          {
  
              System.out.print(a[i] +" ");
          }
          for(int i=0; i < n-1; i++)
          {  
              for(int j=0; j < n-i-1; j++)
              {  
                  if(a[j] < a[j+1])
                  { 
                      //swap elements  
                      temp = a[j];  
                      a[j] = a[j+1];  
                      a[j+1] = temp;  
                  }  
  
              }  
          }  
          System.out.println("\nSorted array is:");
          for (int i=0;i< n;i++)
          {
              System.out.print(a[i] +" ");
          }
      }
  }    

                        

Program 4: Write a program to enter numbers in array and sort them in descending order using bubble sort technique.
Solution:
import java.util.*;
class bubblesort
{
    public static void main(String args[])
    {   int temp;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Array Size : ");  
         int n = sc.nextInt();  
         int a[] = new int[n];
         System.out.println("Enter Array Elements : ");  
        for(int i=0;i< n;i++)
        {
            System.out.println("Enter the "+(i+1)+" element:");
            a[i]=sc.nextInt();
        }
        //display array
        System.out.println("Original array is:");
        for (int i = 0; i < n; i++)
        {

            System.out.print(a[i] +" ");
        }
        for(int i=0; i < n-1; i++)
        {  
            for(int j=0; j < n-i-1; j++)
            {  
                if(a[j] > a[j+1])
                { 
                    //swap elements  
                    temp = a[j];  
                    a[j] = a[j+1];  
                    a[j+1] = temp;  
                }  

            }  
        }  
        System.out.println("\nSorted array is:");
        for (int i=0;i< n;i++)
        {
            System.out.print(a[i] +" ");
        }
    }
}
                        

Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com