Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. class BubbleSort
  2. {
  3. void bubbleSort(int arr[])
  4. {
  5. int n = arr.length;
  6. for (int i = 0; i < n-1; i++)
  7. for (int j = 0; j < n-i-1; j++)
  8. if (arr[j] > arr[j+1])
  9. {
  10. // swap temp and arr[i]
  11. int temp = arr[j];
  12. arr[j] = arr[j+1];
  13. arr[j+1] = temp;
  14. }
  15. }
  16.  
  17. /* Prints the array */
  18. void printArray(int arr[])
  19. {
  20. int n = arr.length;
  21. for (int i=0; i<n; ++i)
  22. System.out.print(arr[i] + " ");
  23. System.out.println();
  24. }
  25.  
  26. // Driver method to test above
  27. public static void main(String args[])
  28. {
  29. BubbleSort ob = new BubbleSort();
  30. int arr[] = {64, 34, 25, 12, 22, 11, 90};
  31. ob.bubbleSort(arr);
  32. System.out.println("Sorted array");
  33. ob.printArray(arr);
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement