Guest User

Untitled

a guest
Apr 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. package com.javarush.test.level07.lesson12.bonus03;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5.  
  6. /* Learning and practicing algorithm.
  7. Task: Read from the keyboard 20 numbers and display them in descending order.
  8. */
  9.  
  10. public class Solution
  11. {
  12. public static void main(String[] args) throws Exception
  13. {
  14. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  15. int N = 20;
  16. int [] array = new int[N];
  17.  
  18. for (int i = 0; i < array.length; i++) {
  19. array[i] = Integer.parseInt(reader.readLine());
  20. }
  21.  
  22. array = sort(array);
  23.  
  24. for (int el : array) {
  25. System.out.print(el + " ");
  26. }
  27.  
  28. }
  29.  
  30. public static int[] sort(int[] a)
  31. {
  32.  
  33. int [] temp = new int[a.length];
  34.  
  35. for (int i = 0; i < a.length; i++) {
  36. temp[i] = a[i];
  37. }
  38.  
  39. boolean isSorted;
  40. do {
  41. isSorted = false;
  42. for (int i = 0; i < temp.length - 1; i++) {
  43. if (temp[i + 1] > temp[i]) {
  44. int t = temp[i];
  45. temp[i] = temp[i + 1];
  46. temp[i + 1] = t;
  47. isSorted = true;
  48. }
  49. }
  50. } while (isSorted);
  51.  
  52. return temp;
  53. }
Add Comment
Please, Sign In to add comment