VelizarAng

SortArr

Apr 13th, 2021 (edited)
888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SortingArr {
  4.     public static void FillingArr(int[] arr){
  5.         Scanner scan = new Scanner(System.in);
  6.  
  7.         for (int i = 0; i < arr.length; i++) {
  8.             System.out.printf("Enter element [%d]: ", i);
  9.             arr[i] = scan.nextInt();
  10.         }
  11.  
  12.     }
  13.  
  14.     public static void SortArr(int[] arr){
  15.         int i, j, Buff;
  16.         int n = arr.length;
  17.         for(i = 0; i < n - 1 ; i++){
  18.             for(j = 0; j < n - i - 1; j++){
  19.                 if(arr[j] > arr[j + 1]){
  20.                     Buff = arr[j];
  21.                     arr[j] = arr[j + 1];
  22.                     arr[j + 1] = Buff;
  23.                 }
  24.             }
  25.         }
  26.     }
  27.  
  28.     public static void main(String[] args) {
  29.         Scanner scan = new Scanner(System.in);
  30.  
  31.         System.out.print("Enter the number of elements: ");
  32.         int n = scan.nextInt();
  33.         int[] arr = new int[n];
  34.  
  35.         FillingArr(arr);
  36.         SortArr(arr);
  37.  
  38.         System.out.print(arr[0]);
  39.         for (int i = 1; i < arr.length; i++) {
  40.             System.out.print(" <= ");
  41.             System.out.printf("%d", arr[i]);
  42.         }
  43.     }
  44. }
Add Comment
Please, Sign In to add comment