Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. public class Sorter1D
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         int[] x = {5,8,1,7,3,9,2,4,6,0};
  6.  
  7.         //Print the original array (int[] x).
  8.         System.out.println("Original array.");
  9.         for(int i = 0; i < x.length; i++)
  10.         {
  11.             if(i < x.length)
  12.             {
  13.                 System.out.print(x[i] + " ");
  14.             }
  15.             else
  16.             {
  17.                 System.out.print(x[i]);
  18.             }
  19.         }
  20.  
  21.         //Separate the different arrays
  22.         System.out.println();
  23.         System.out.println("-------------------------");
  24.  
  25.         //Sort the original array in ascending order. (0, 1, 2, etc.)
  26.         System.out.println("Array in ascending order.");
  27.         for(int i = 0; i < x.length; i++)
  28.         {
  29.             int y = i;
  30.             for(int z = (i + 1); z < x.length; z++)
  31.             {
  32.                 if(x[z] < x[y])
  33.                 {
  34.                     y = z;
  35.                 }
  36.  
  37.                 int temp = x[y];
  38.                 x[y] = x[i];
  39.                 x[i] = temp;
  40.             }
  41.  
  42.             //Print the new sorted array
  43.             if(i < x.length)
  44.             {
  45.                 System.out.print(x[i] + " ");
  46.             }
  47.             else
  48.             {
  49.                 System.out.print(x[i] + "\n");
  50.             }
  51.         }
  52.  
  53.         //Separate the different arrays
  54.         System.out.println();
  55.         System.out.println("-------------------------");
  56.  
  57.         //Sort the original array in descending order. (9, 8, 7, etc.)
  58.         System.out.println("Array in descending order.");
  59.         for(int i = 0; i < x.length; i++)
  60.         {
  61.             int y = i;
  62.             for(int z = (i + 1); z < x.length; z++)
  63.             {
  64.                 if(x[z] > x[y])
  65.                 {
  66.                     y = z;
  67.                 }
  68.  
  69.                 int temp = x[y];
  70.                 x[y] = x[i];
  71.                 x[i] = temp;
  72.             }
  73.  
  74.             //Print the new sorted array
  75.             if(i < x.length)
  76.             {
  77.                 System.out.print(x[i] + " ");
  78.             }
  79.             else
  80.             {
  81.                 System.out.print(x[i] + "\n");
  82.             }
  83.         }
  84.         System.out.println();
  85.         System.out.println();
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement