TsetsoP

BubbleSort

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