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
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
Thank you so much :)
ReplyDeleteSort an Array Elements in Descending Order in Java
ReplyDeleteThanks for sharing this post, really this is helpful for me
import java.util.Scanner;
ReplyDeletepublic 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;
}
}
There is no exception handling in your program .......correct it.
ReplyDelete
ReplyDeleteI 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
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.
ReplyDeletepython Training institute in Chennai
python Training institute in Bangalore
python Training in Pune
can't find exception handling being put to use
ReplyDelete
ReplyDeleteNice blog! Thanks for sharing this valuable information
Java Training in Bangalore
Java Classes in Pune
Great post. keep sharing such a worthy information.
ReplyDeleteData Science Training in Chennai
ReplyDeleteGreat post. keep sharing such a worthy information.
Data Science Certification in Chennai
Great article.
ReplyDeletealso, check Java Classes in Nagpur
infomative blog, keep posting . If you want to learn about java web developer then checkout java course in pune
ReplyDelete