Sorting an Integer Array in Java

Problem:

Write a program that reads 10 value of integers from the user, store them in an array of integers. Then print the reverse of this array. Later, print a sorted version of the user. You are not allowed to use the Arrays.sort() method.


Output:

Enter the values: 1 6 3 4 2 9 8 10 5 7
Reversed array: 7 5 10 8 9 2 4 3 6 1
Values sorted: 1 2 3 4 5 6 7 8 9 10
-2


Solution:

import java.util.Scanner;
 
public class Problem2
{
 public static void main (String[] args)
 {
 Scanner scan = new Scanner(System.in);
 System.out.print("Enter the values: ");
 int[]list = new int[10];
 for (int i=0; i <10; i++)
 {
  list[i]= scan.nextInt();
 }
 System.out.print("Reversed array:  " );
 for(int k =9; k>=0;k--)
  System.out.print(list[k] + " ");
 
 for (int d=0;d<10;d++)
 {
  for (int i =0; i<9;i++)
  {
   if(list[i]>list[i+1])
   {
   int greater = list[i];
   list[i] = list[i+1];
   list[1+i]= greater;
   }
  }
 }
 System.out.println();
 System.out.print("Values sorted:   ");
 for(int values :  list)
  System.out.print(values + " ");
}
}


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact