Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Objects;
  5. import java.util.Scanner;
  6.  
  7. public class qwe {
  8.  
  9. public static void main(String[] args) throws IOException {
  10.  
  11. qwe Q = new qwe();
  12. Scanner sc = new Scanner(System.in);
  13. System.out.print("Enter limit of array: ");
  14. int a = sc.nextInt();//scan limit of array
  15. int[] arr = new int[a];//new array
  16.  
  17. for (int i = 0; i < arr.length; i++) {
  18. arr[i] = (int)(Math.random()*a+1);//random elements in array
  19. }
  20.  
  21. System.out.print("Enter your choice: sortUP(1), sortDOWN(2): ");
  22. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  23. String y = reader.readLine();
  24.  
  25. if(Objects.equals(y, "1")){sortUP(arr);}
  26. else if(Objects.equals(y, "2")){sortDOWN(arr);}
  27. else {
  28. System.out.println("ERROR");
  29. }
  30. Q.out(arr);
  31.  
  32. }
  33.  
  34. public static void sortUP(int[] mas){
  35. for (int i = 0; i < mas.length; i++) {
  36. for (int j = i; j < mas.length; j++) {
  37. if(mas[i]>mas[j]){
  38. int h = mas[i];
  39. mas[i] = mas[j];
  40. mas[j] = h;
  41. }
  42. }
  43. }
  44. }
  45.  
  46. public static void sortDOWN(int[] mas){
  47. for (int i = 0; i < mas.length; i++) {
  48. for (int j = i; j < mas.length; j++) {
  49. if(mas[i]<mas[j]){
  50. int h = mas[i];
  51. mas[i] = mas[j];
  52. mas[j] = h;
  53. }
  54. }
  55. }
  56. }
  57.  
  58. public void out(int[] arr){
  59. for(int Arr : arr) System.out.print("|" + Arr + "|" + " ");
  60. }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement