eniallator

Bubble sort in java

Oct 3rd, 2018
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.51 KB | None | 0 0
  1. public class BubbleSort{
  2.     public void sort(int[] inArray) {
  3.         for (int i = inArray.length - 1; i > 0; i--) {
  4.             boolean sortFlag = true;
  5.  
  6.             for (int j = 0; j < i; j++) {
  7.                 if (inArray[j] > inArray[j + 1]) {
  8.                     sortFlag = false;
  9.  
  10.                     int temp = inArray[j];
  11.                     inArray[j] = inArray[j + 1];
  12.                     inArray[j + 1] = temp;
  13.                 }
  14.             }
  15.  
  16.             if (sortFlag) return;
  17.         }
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment