Sanady

Sorting and Generating

Feb 23rd, 2018
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package javaarray.arraytest;
  7. import java.util.Random;
  8.  
  9. /**
  10. *
  11. * @author xrener
  12. */
  13. public class TestArray
  14. {
  15. private static void bubbleSort(int arr[])
  16. {
  17. int n = arr.length;
  18. for (int i = 0; i < n-1; i++)
  19. for (int j = 0; j < n-i-1; j++)
  20. if (arr[j] > arr[j+1])
  21. {
  22. // swap temp and arr[i]
  23. int temp = arr[j];
  24. arr[j] = arr[j+1];
  25. arr[j+1] = temp;
  26. }
  27. }
  28.  
  29. public static void arrayInit()
  30. {
  31. int[] MyArray = new int[32];
  32. System.out.println("Dlzka mojho pola: "+ MyArray.length +"");
  33. Random rand = new Random();
  34. for(int i = 0; i < MyArray.length; i++)
  35. {
  36. MyArray[i] = rand.nextInt(32);
  37. System.out.println("Nahodna hodnota: "+ MyArray[i] +"");
  38. }
  39.  
  40. bubbleSort(MyArray);
  41. for(int i = 0; i < MyArray.length; i++)
  42. {
  43. System.out.println("Sortovane hodnoty: "+ MyArray[i] +"");
  44. }
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment