Advertisement
MilaDimitrovaa

Алгоритми - Домашно

Nov 2nd, 2020
2,276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class homewroks {
  5.  
  6.    
  7.           public static void BubbleSort(int[] ARR){
  8.                 int j, Buff;
  9.                 boolean swap;
  10.                
  11.                 do{
  12.                    swap = false;
  13.                    for (j = 0; j < ARR.length - 1; j++) {
  14.                         if(ARR[j] > ARR[j + 1]){
  15.                             swap = true;
  16.                             Buff = ARR[j];
  17.                             ARR[j] = ARR[j + 1];
  18.                             ARR[j + 1] = Buff;
  19.                         }
  20.                     }
  21.                 }while(swap);
  22.             }
  23.          
  24.             public static void main(String[] args) {
  25.                 //point 1
  26.                 Scanner scan = new Scanner(System.in);
  27.                 System.out.println("Input number of elements: ");
  28.                 int n = scan.nextInt();
  29.          
  30.                 //point 2
  31.                 int[] ARR = new int[n];
  32.          
  33.                 //point 3
  34.                 for (int i = 0; i < n ; i++) {
  35.                     System.out.printf("Please input element [%d]", i);
  36.                     ARR[i] = scan.nextInt();
  37.                 }
  38.          
  39.                 //point 4
  40.                 System.out.println("BEFORE SORTING: ");
  41.                 System.out.println(Arrays.toString(ARR));
  42.          
  43.                 //point 5
  44.                 BubbleSort(ARR);
  45.          
  46.                 //point 6
  47.                 System.out.println("AFTER SORTING: ");
  48.                 System.out.println(Arrays.toString(ARR));
  49.          
  50.             }
  51.        
  52.  
  53. }
  54.  
  55.  
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement