Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         int[] array = {2, -1, -5, 3, 4, 2, 10};
  6.  
  7.         sort(array);
  8.         // Arrays.sort(array);
  9.         System.out.println(Arrays.toString(array));
  10.     }
  11.  
  12.     private static void sort(int[] array)
  13.     {
  14.         boolean isSorted = false;
  15.  
  16.         while (!isSorted) {
  17.             isSorted = true;
  18.  
  19.             for (int i = 0; i < array.length - 1; i++) {
  20.                 int a = array[i];
  21.                 int b = array[i + 1];
  22.                
  23.                 if (a > b) {
  24.                     array[i] = b;
  25.                     array[i + 1] = a;
  26.                     isSorted = false;
  27.                 }
  28.             }
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement