Created
October 19, 2018 11:49
-
-
Save uladzislau-slk/1a28b2f2a2482ecefe6980b3303e4948 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OddEvenSort { | |
public static void main(String[] args) { | |
int num[] = new int[10]; | |
int min = -10000; | |
int max = 10000; | |
int buf; | |
for (int i = 0; i < num.length; i++) { | |
num[i] = (int) (Math.random() * (max - min)) + min; | |
} | |
System.out.print("Чётно-нечётная сортировка\nИсходный массив: "); | |
for (int i = 0; i < num.length; i++) System.out.print(" " + num[i]); | |
for (int j = 0; j < num.length - 1; j++) { | |
for (int k = 0; k < num.length; k += 2) { | |
if (num[k] > num[k + 1]) { | |
buf = num[k]; | |
num[k] = num[k + 1]; | |
num[k + 1] = buf; | |
} | |
} | |
for (int k = 1; k < num.length - 1; k += 2) { | |
if (num[k] > num[k + 1]) { | |
buf = num[k]; | |
num[k] = num[k + 1]; | |
num[k + 1] = buf; | |
} | |
} | |
} | |
System.out.print("\nОтсортированный массив: "); | |
for (int i = 0; i < num.length; i++) System.out.print(" "+num[i]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment