Guest User

Untitled

a guest
Apr 5th, 2021
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. /**
  2. * Write a description of class BubbleShort here.
  3. *
  4. * @author (your name)
  5. * @version (a version number or a date)
  6. */
  7. public class BubbleShort
  8. {
  9. static void bubbleSort(int[] arr){
  10. int n = arr.length;
  11. int temp = 0;
  12. for(int i=0; i<n; i++){
  13. for(int j=1; j<(n-1); j++){
  14. if(arr[j-1] > arr[j]){
  15. //swap elements
  16. temp = arr[j-1];
  17. arr[j-1] = arr[j];
  18. arr[j] = temp;
  19. }
  20. }
  21. }
  22. }
  23. public static void main(String[] args){
  24. int arr[] = {3,60,35,2,45,320,5};
  25.  
  26. System.out.println("Array Before Bubble Sort");
  27. for(int i=0; i<arr.length; i++){
  28. System.out.print(arr[i] + " ");
  29. }
  30. System.out.println();
  31.  
  32. bubbleSort(arr); //sorting array elements using bubble sort
  33.  
  34. System.out.println("Array After Bubble Sort");
  35. for(int i=0; i<arr.length; i++){
  36. System.out.print(arr[i] + " ");
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment