Advertisement
Dido09

3.4 - BubbleSort

May 8th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class BubbleSort {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. Scanner scanner = new Scanner(System.in);
  8. boolean swap = false;
  9. int buffer = 1;
  10.  
  11. int[] nums = new int[5];
  12. for ( int i=0; i<5; i++) {
  13. nums[i]=scanner.nextInt();
  14. }
  15. do {
  16. swap = false;
  17. for(int i=1; i<5; i++) {
  18. if(nums[i]<nums[i-1]) {
  19. buffer = nums[i];
  20. nums[i]=nums[i-1];
  21. nums[i-1]=buffer;
  22. swap=true;
  23. }
  24.  
  25. }
  26.  
  27. }while(swap);
  28.  
  29. for(int i=0;i<5;i++) {
  30. System.out.println(nums[i]);
  31. }
  32.  
  33. scanner.close();
  34.  
  35. }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement