Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class ShakerSort {
- static void shakerSort(int a[], int n) {
- // Vasiot kod tuka
- if (n == 1) {
- System.out.print(a[0] + " \n" + a[0] + " ");
- }
- for (int i = 0; i < n / 2; i++) {
- boolean flag = false;
- for (int j = n-1; j>0; j--) { // prvo pominuvanje
- if (a[j] < a[j - 1]) {
- int tmp = a[j];
- a[j] = a[j - 1];
- a[j - 1] = tmp;
- flag = true;
- }
- }
- for (int k = 0; k < n; k++) {
- System.out.print(a[k] + " ");
- }
- System.out.println();
- for (int j = 0; j < n-i-1; j++) { // vtoro pominuvanje
- if (a[j] > a[j + 1]) {
- int tmp = a[j];
- a[j] = a[j + 1];
- a[j + 1] = tmp;
- flag = true;
- }
- }
- for (int k = 0; k < n; k++) {
- System.out.print(a[k] + " ");
- }
- System.out.println();
- if (!flag) break; // ako ni eden element ne e smenet znachi veke e sortirana
- }
- }
- public static void main(String[] args) throws IOException {
- int i;
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- String s = stdin.readLine();
- int n = Integer.parseInt(s);
- s = stdin.readLine();
- String[] pom = s.split(" ");
- int[] a = new int[n];
- for (i = 0; i < n; i++) {
- a[i] = Integer.parseInt(pom[i]);
- }
- shakerSort(a, n);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement