Advertisement
YORDAN2347

bubbleSort

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