Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Sorter1D
- {
- public static void main(String args[])
- {
- int[] x = {5,8,1,7,3,9,2,4,6,0};
- //Print the original array (int[] x).
- System.out.println("Original array.");
- for(int i = 0; i < x.length; i++)
- {
- if(i < x.length)
- {
- System.out.print(x[i] + " ");
- }
- else
- {
- System.out.print(x[i]);
- }
- }
- //Separate the different arrays
- System.out.println();
- System.out.println("-------------------------");
- //Sort the original array in ascending order. (0, 1, 2, etc.)
- System.out.println("Array in ascending order.");
- for(int i = 0; i < x.length; i++)
- {
- int y = i;
- for(int z = (i + 1); z < x.length; z++)
- {
- if(x[z] < x[y])
- {
- y = z;
- }
- int temp = x[y];
- x[y] = x[i];
- x[i] = temp;
- }
- //Print the new sorted array
- if(i < x.length)
- {
- System.out.print(x[i] + " ");
- }
- else
- {
- System.out.print(x[i] + "\n");
- }
- }
- //Separate the different arrays
- System.out.println();
- System.out.println("-------------------------");
- //Sort the original array in descending order. (9, 8, 7, etc.)
- System.out.println("Array in descending order.");
- for(int i = 0; i < x.length; i++)
- {
- int y = i;
- for(int z = (i + 1); z < x.length; z++)
- {
- if(x[z] > x[y])
- {
- y = z;
- }
- int temp = x[y];
- x[y] = x[i];
- x[i] = temp;
- }
- //Print the new sorted array
- if(i < x.length)
- {
- System.out.print(x[i] + " ");
- }
- else
- {
- System.out.print(x[i] + "\n");
- }
- }
- System.out.println();
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement