Advertisement
Uimin_Maxim

Задача 9

Jan 27th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. //Уймин Максим
  2. //IT School, 2 группа
  3. //09.10.2015
  4. //Задача 9. Отсортировать элементы массива в порядке возрастания или убывания по последней цифре
  5. import java.io.PrintStream;
  6. import java.util.Scanner;
  7. public class Exersise_9 {
  8.     public static PrintStream out = System.out;
  9.     public static Scanner scan = new Scanner(System.in);
  10.     public static int[] CreateArray (int N){
  11.         //Функция создаёт массив и заполняет его случайными числами
  12.         int[]array = new int[N];
  13.         out.println("Original array:");
  14.         for (int i=0;i<N;i++){
  15.             array[i]=(0) + (int)(Math.random()*((100-(-100))+1));
  16.             out.print(array[i]+"\t");
  17.         }
  18.         out.println();
  19.         return array;
  20.     }
  21.     public static void SortArray(int[]array,int N){
  22.         //Функция сортирует массив в соответствии с заданием и выводит его на экран
  23.         out.println("Should I sort array 1.from max to min or 2.from min to max? (enter the number of the correct answer)");
  24.         int answer = scan.nextInt();
  25.         while (answer !=1 && answer!=2){
  26.             out.println ("Wrong command, try again");
  27.             out.println("Should I sort array 1.from max to min or 2.from min to max? (enter the number of the correct answer)");
  28.             answer = scan.nextInt();
  29.         }
  30.         if (answer == 1) SortFromMaxToMin(array,N);
  31.         else if (answer == 2) SortFromMinToMax(array,N);
  32.     }
  33.     public static void SortFromMaxToMin(int[] array,int N){
  34.         //Функция выполняет сортировку в соответствии с заданием в порядке убывания
  35.         for (int i = 0; i < N-1; i ++ )
  36.             for (int j = N-2; j >= i; j -- )
  37.             if ( array[j] % 10 < array[j+1] % 10 )
  38.             {
  39.             int c = array[j];
  40.             array[j] = array[j+1];
  41.             array[j+1] = c;
  42.             }
  43.         for (int i=0;i<N;i++){
  44.             out.print(array[i]+"\t");
  45.         }
  46.     }
  47. public static void SortFromMinToMax(int[] array,int N){
  48.     //Функция выполняет сортировку в соответствии с заданием в порядке возрастания
  49.     for (int i = 0; i < N-1; i ++ )
  50.         for (int j = N-2; j >= i; j -- )
  51.         if ( array[j] % 10 > array[j+1] % 10 )
  52.         {
  53.         int c = array[j];
  54.         array[j] = array[j+1];
  55.         array[j+1] = c;
  56.         }
  57.         for (int i=0;i<N;i++){
  58.             out.print(array[i]+"\t");
  59.         }
  60.     }
  61.     public static void MainFunction (){
  62.         //Здесь собран весь функционал программы
  63.         out.println("Enter the array size(intger number, greather then zero)");
  64.         int N = scan.nextInt();
  65.         int[] array=new int [N];
  66.         array = CreateArray(N);
  67.         SortArray(array,N);
  68.     }
  69.     public static void main (String [] args){
  70.         //Основная функция, отсюда начинается выполнение приложения
  71.         MainFunction();
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement