Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Objects;
- import java.util.Scanner;
- public class qwe {
- public static void main(String[] args) throws IOException {
- qwe Q = new qwe();
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter limit of array: ");
- int a = sc.nextInt();//scan limit of array
- int[] arr = new int[a];//new array
- for (int i = 0; i < arr.length; i++) {
- arr[i] = (int)(Math.random()*a+1);//random elements in array
- }
- System.out.print("Enter your choice: sortUP(1), sortDOWN(2): ");
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String y = reader.readLine();
- if(Objects.equals(y, "1")){sortUP(arr);}
- else if(Objects.equals(y, "2")){sortDOWN(arr);}
- else {
- System.out.println("ERROR");
- }
- Q.out(arr);
- }
- public static void sortUP(int[] mas){
- for (int i = 0; i < mas.length; i++) {
- for (int j = i; j < mas.length; j++) {
- if(mas[i]>mas[j]){
- int h = mas[i];
- mas[i] = mas[j];
- mas[j] = h;
- }
- }
- }
- }
- public static void sortDOWN(int[] mas){
- for (int i = 0; i < mas.length; i++) {
- for (int j = i; j < mas.length; j++) {
- if(mas[i]<mas[j]){
- int h = mas[i];
- mas[i] = mas[j];
- mas[j] = h;
- }
- }
- }
- }
- public void out(int[] arr){
- for(int Arr : arr) System.out.print("|" + Arr + "|" + " ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement