Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Уймин Максим
- //IT School, 2 группа
- //09.10.2015
- //Задача 9. Отсортировать элементы массива в порядке возрастания или убывания по последней цифре
- import java.io.PrintStream;
- import java.util.Scanner;
- public class Exersise_9 {
- public static PrintStream out = System.out;
- public static Scanner scan = new Scanner(System.in);
- public static int[] CreateArray (int N){
- //Функция создаёт массив и заполняет его случайными числами
- int[]array = new int[N];
- out.println("Original array:");
- for (int i=0;i<N;i++){
- array[i]=(0) + (int)(Math.random()*((100-(-100))+1));
- out.print(array[i]+"\t");
- }
- out.println();
- return array;
- }
- public static void SortArray(int[]array,int N){
- //Функция сортирует массив в соответствии с заданием и выводит его на экран
- out.println("Should I sort array 1.from max to min or 2.from min to max? (enter the number of the correct answer)");
- int answer = scan.nextInt();
- while (answer !=1 && answer!=2){
- out.println ("Wrong command, try again");
- out.println("Should I sort array 1.from max to min or 2.from min to max? (enter the number of the correct answer)");
- answer = scan.nextInt();
- }
- if (answer == 1) SortFromMaxToMin(array,N);
- else if (answer == 2) SortFromMinToMax(array,N);
- }
- public static void SortFromMaxToMin(int[] array,int N){
- //Функция выполняет сортировку в соответствии с заданием в порядке убывания
- for (int i = 0; i < N-1; i ++ )
- for (int j = N-2; j >= i; j -- )
- if ( array[j] % 10 < array[j+1] % 10 )
- {
- int c = array[j];
- array[j] = array[j+1];
- array[j+1] = c;
- }
- for (int i=0;i<N;i++){
- out.print(array[i]+"\t");
- }
- }
- public static void SortFromMinToMax(int[] array,int N){
- //Функция выполняет сортировку в соответствии с заданием в порядке возрастания
- for (int i = 0; i < N-1; i ++ )
- for (int j = N-2; j >= i; j -- )
- if ( array[j] % 10 > array[j+1] % 10 )
- {
- int c = array[j];
- array[j] = array[j+1];
- array[j+1] = c;
- }
- for (int i=0;i<N;i++){
- out.print(array[i]+"\t");
- }
- }
- public static void MainFunction (){
- //Здесь собран весь функционал программы
- out.println("Enter the array size(intger number, greather then zero)");
- int N = scan.nextInt();
- int[] array=new int [N];
- array = CreateArray(N);
- SortArray(array,N);
- }
- public static void main (String [] args){
- //Основная функция, отсюда начинается выполнение приложения
- MainFunction();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement