Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level08.lesson11.bonus03;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- /* Задача по алгоритмам
- Задача: Введи с клавиатуры 20 слов и выведи их в алфавитном порядке.
- */
- public class Solution
- {
- public static void main(String[] args) throws Exception
- {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String[] array = new String[20];
- for (int i = 0; i < array.length; i++)
- {
- array[i] = reader.readLine();
- }
- sort(array);
- for (String x : array)
- {
- System.out.println(x);
- }
- }
- public static void sort(String[] array)
- {
- //Напишите тут ваш код
- boolean flag = true;
- String temp;
- while (flag) {
- flag = false;
- for (int j = 0; j < array.length - 1; j++) {
- if (isGreaterThen(array[j],array[j+1])) {
- temp = array[j];
- array[j] = array [j + 1];
- array[j + 1] = temp;
- flag = true;
- }
- }
- }
- }
- //Метод для сравнения строк: 'а' больше чем 'b'
- public static boolean isGreaterThen(String a, String b)
- {
- return a.compareTo(b) > 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement