Write a program to sort list of elements in ascending and descending order and show the exception handling.

import java.util.*;
class SortOrder
{
  public static void main(String[] args)
  {
    Scanner sc=new java.util.Scanner(System.in);
    System.out.print("Enter the array size: ");
    int size=sc.nextInt();
        int[] arr=new int[size];
        System.out.println("Enter Array elements: ");
        for(int i=0;i<size;i++)
        {
            System.out.print("Element No. "+(i+1)+": ");
            arr[i]=sc.nextInt();
        }
        System.out.print("Before Sorting: ");
        for(int i=0;i<size;i++)
        System.out.print(" "+arr[i]);
        for(int i=0;i<size;i++)
        {
            int temp;
            for(int j=i+1;j<size;j++)
            {
                if(arr[i]>arr[j])
                {
                        temp=arr[i];
                        arr[i]=arr[j];
                        arr[j]=temp;
                  }
            }
        }
        System.out.println();
        System.out.print("After Sorting in ascending order: ");
        for(int i=0;i<size;i++)
    {
            System.out.print(" "+arr[i]);
    }
    System.out.println();
        System.out.print("After Sorting in descending order: ");
    for(int i=size-1;i>=0;i--)
    {
            System.out.print(" "+arr[i]);
        }

  }
}

Output


C:\Users\Jaisha\Desktop\Java>javac SortOrder.java

C:\Users\Jaisha\Desktop\Java>java SortOrder
Enter the array size: 5
Enter Array elements:
Element No. 1: 5
Element No. 2: 3
Element No. 3: 62
Element No. 4: 1
Element No. 5: 52
Before Sorting:  5 3 62 1 52
After Sorting in ascending order:  1 3 5 52 62
After Sorting in descending order:  62 52 5 3 1

12 comments:

  1. Thank you so much :)

    ReplyDelete
  2. import java.util.Scanner;
    public class quicksort {
    public static int[] main(String args[])
    {
    int n;
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the number of elements you want in a array");
    n=scan.nextInt();
    int array[] = new int[n];
    for(int i=0;i<array.length;i++)
    {
    array[i]=scan.nextInt();
    }
    int start=0,end=array.length-1;
    for(int i=0;i<array.length;i++)
    {
    if(array[i]<=array[i+1])
    {
    array[i] =array[start];
    start=start+1;
    }
    }
    array[start]=array[end];
    return array;
    }
    }

    ReplyDelete
  3. There is no exception handling in your program .......correct it.

    ReplyDelete

  4. I was recommended this web site by means of my cousin.
    I am now not certain whether this post is written through him as nobody else recognise such precise about my difficulty. You're amazing! Thank you!

    selenium training in Chennai
    selenium training in Tambaram
    selenium training in Velachery
    selenium training in Omr
    selenium training in Annanagar

    ReplyDelete
  5. This is beyond doubt a blog significant to follow. You’ve dig up a great deal to say about this topic, and so much awareness. I believe that you recognize how to construct people pay attention to what you have to pronounce, particularly with a concern that’s so vital. I am pleased to suggest this blog.
    python Training institute in Chennai
    python Training institute in Bangalore
    python Training in Pune

    ReplyDelete
  6. can't find exception handling being put to use

    ReplyDelete
  7. Great post. keep sharing such a worthy information.
    Data Science Training in Chennai

    ReplyDelete
  8. infomative blog, keep posting . If you want to learn about java web developer then checkout java course in pune

    ReplyDelete