Advertisement
Md_Sakib_Hossain

Bubble Sort Indexed Array

Mar 9th, 2021
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | None | 0 0
  1. import java.lang.*;
  2. import java.util.*;
  3. public class BubbleSortIA{
  4.     public static void main(String[] args) {
  5.         int array[]={4,-1,2,9,5};
  6.         // int temp; //You can do this using temp or without temp variable
  7.  
  8.         for (int i=0;i<array.length -1; i++) {
  9.             for (int j=0;j<array.length - 1; j++) {
  10.                 if(array[j+1]<array[j]){
  11.                    // temp = array[j+1];  
  12.                    // array[j+1]=array[j];
  13.                    // array[j]=temp;
  14.  
  15.                    array[j+1]=array[j]^array[j+1];
  16.                    array[j]=array[j]^array[j+1];
  17.                    array[j+1]=array[j]^array[j+1];
  18.                 }
  19.             }
  20.         }
  21.  
  22.         for (int X : array) {
  23.             System.out.print(X+" ");
  24.         }
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement