Filip_Markoski

[ADS] Shaker (cocktail) sort

Nov 14th, 2017
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 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.  
  8.     private static void printNumbers(int[] input) {
  9.         for (int i = 0; i < input.length; i++) {
  10.             System.out.print(input[i] + " ");
  11.         }
  12.         System.out.print(System.lineSeparator());
  13.     }
  14.  
  15.     static void swap(int array[], int i, int j) {
  16.         int temp = array[i];
  17.         array[i] = array[j];
  18.         array[j] = temp;
  19.     }
  20.  
  21.     static void MybubbleSort(int array[]) {
  22.         printNumbers(array);
  23.         /*
  24.         System.out.print("Here "+array.length+System.lineSeparator());
  25.          */
  26.  
  27.         for (int i = 0; i < array.length; i++) {
  28.             /*
  29.             System.out.print(i+" "+System.lineSeparator());
  30.              */
  31.             for (int j = 1; j < array.length - i; j++) {
  32.                 /*
  33.                 System.out.print(i + " " + j + System.lineSeparator());
  34.                  */
  35.                 /* Swap Elements */
  36.                 if (array[j - 1] > array[j]) {
  37.                     swap(array, j - 1, j);
  38.                 }
  39.             }
  40.  
  41.             printNumbers(array);
  42.         }
  43.     }
  44.  
  45.     static void bubbleSort(int array[]) {
  46.         for (int i = array.length - 1; i >= 0; i--) {
  47.             boolean flipped = false;
  48.             for (int j = 1; j <= i; j++) {
  49.                 if (array[j - 1] > array[j]) {
  50.                     swap(array, j - 1, j);
  51.                     flipped = true;
  52.                 }
  53.             }
  54.             if (!flipped) break;
  55.             printNumbers(array);
  56.         }
  57.     }
  58.  
  59.     /* A variation of bubblesort */
  60.     static void shakerSort(int array[], int n) {
  61.         int countMin = 0;
  62.         int countMax = 0;
  63.         boolean done = false;
  64.         for (int k = 0; k < (array.length + 1) / 2; ++k) {
  65.  
  66.             done = true;
  67.  
  68.             /* Smallest in front */
  69.             for (int j = array.length - 1; j > countMin; j--) {
  70.                 if (array[j - 1] > array[j]) {
  71.                     swap(array, j - 1, j);
  72.                     done = false;
  73.                 }
  74.             }
  75.             countMin++;
  76.             printNumbers(array);
  77.  
  78.             /* Biggest in rear */
  79.             for (int j = countMax; j < array.length - 1; j++) {
  80.                 if (array[j] > array[j + 1]) {
  81.                     swap(array, j, j + 1);
  82.                     done = false;
  83.                 }
  84.             }
  85.             countMax++;
  86.             printNumbers(array);
  87.             if (done) break;
  88.         }
  89.     }
  90.  
  91.     public static void main(String[] args) throws IOException {
  92.         int i;
  93.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  94.         String s = stdin.readLine();
  95.         int n = Integer.parseInt(s);
  96.  
  97.         s = stdin.readLine();
  98.         String[] pom = s.split(" ");
  99.         int[] a = new int[n];
  100.         for (i = 0; i < n; i++)
  101.             a[i] = Integer.parseInt(pom[i]);
  102.         shakerSort(a, n);
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment