Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class ShakerSort {
  6.  
  7. static void shakerSort(int a[], int n) {
  8. // Vasiot kod tuka
  9. if (n == 1) {
  10. System.out.print(a[0] + " \n" + a[0] + " ");
  11. }
  12. for (int i = 0; i < n / 2; i++) {
  13. boolean flag = false;
  14. for (int j = n-1; j>0; j--) { // prvo pominuvanje
  15. if (a[j] < a[j - 1]) {
  16. int tmp = a[j];
  17. a[j] = a[j - 1];
  18. a[j - 1] = tmp;
  19. flag = true;
  20. }
  21. }
  22. for (int k = 0; k < n; k++) {
  23. System.out.print(a[k] + " ");
  24. }
  25. System.out.println();
  26. for (int j = 0; j < n-i-1; j++) { // vtoro pominuvanje
  27. if (a[j] > a[j + 1]) {
  28. int tmp = a[j];
  29. a[j] = a[j + 1];
  30. a[j + 1] = tmp;
  31. flag = true;
  32. }
  33. }
  34. for (int k = 0; k < n; k++) {
  35. System.out.print(a[k] + " ");
  36. }
  37. System.out.println();
  38. if (!flag) break; // ako ni eden element ne e smenet znachi veke e sortirana
  39. }
  40.  
  41. }
  42.  
  43. public static void main(String[] args) throws IOException {
  44. int i;
  45. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  46. String s = stdin.readLine();
  47. int n = Integer.parseInt(s);
  48.  
  49. s = stdin.readLine();
  50. String[] pom = s.split(" ");
  51. int[] a = new int[n];
  52. for (i = 0; i < n; i++) {
  53. a[i] = Integer.parseInt(pom[i]);
  54. }
  55. shakerSort(a, n);
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement