Guest User

Untitled

a guest
May 21st, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1.  
  2. public class BubbleSort {
  3.  
  4. public static void main(String[] args) {
  5.  
  6. //create an int array we want to sort using bubble sort algorithm
  7. int intArray[] = new int[]{5,90,35,45,150,3};
  8.  
  9. //print array before sorting using bubble sort algorithm
  10. System.out.println("Array Before Bubble Sort");
  11. for(int i=0; i < intArray.length; i++){
  12. System.out.print(intArray[i] + " ");
  13. }
  14.  
  15. //sort an array using bubble sort algorithm
  16. bubbleSort(intArray);
  17.  
  18. System.out.println("");
  19.  
  20. //print array after sorting using bubble sort algorithm
  21. System.out.println("Array After Bubble Sort");
  22. for(int i=0; i < intArray.length; i++){
  23. System.out.print(intArray[i] + " ");
  24. }
  25. }
  26.  
  27. private static void bubbleSort(int[] intArray) {
  28. int n = intArray.length;
  29. int temp = 0;
  30.  
  31. for(int i=0; i < n; i++){
  32. for(int j=1; j < (n-i); j++){
  33.  
  34. if(intArray[j-1] > intArray[j]){
  35. //swap the elements!
  36. temp = intArray[j-1];
  37. intArray[j-1] = intArray[j];
  38. intArray[j] = temp;
  39. }
  40.  
  41. }
  42. }
  43.  
  44. }
  45. }
Add Comment
Please, Sign In to add comment