0

Bubble sort

This is my code on topic Bubble sort my bubble sort code is giving me some garbage value what is the problem

public class Program
{
   public static void main(String[] args ) {
      int[] arr={3,5,7,2,1,8};
      int n=arr.length;
     for(int i=0;i<n-1;i++){
      for(int j=0;j<n-i-1;j++){
        if(arr[j]>arr[j+1]){
            int temp=arr[j];
            arr[j]=arr[j+1];
            arr[j+1]=temp;
        }  
      }
    }
       System.out.print ( arr );  
  }
}
  • no it is still giving garbage value like [I@4dc63996 – Abhishek Kumar Mar 08 '22 at 09:42
  • 1
    Hint: if you're going to say that a value is garbage, it really helps to *show the value in the question*. (But yes, basically the problem has nothing to do with sorting - it's entirely about how you print out an array in Java.) – Jon Skeet Mar 08 '22 at 09:57

1 Answers1

0

AFAIK, this should work

import java.util.Arrays;

public class Program
{
   public static void main(String[] args ) {
      int[] arr={3,5,7,2,1,8};
      int n=arr.length;
     for(int i=0;i<n-1;i++){
      for(int j=0;j<n-i-1;j++){
        if(arr[j]>arr[j+1]){
            int temp=arr[j];
            arr[j]=arr[j+1];
            arr[j+1]=temp;
        }  
      }
    }
       System.out.print ( Arrays.toString(arr) );  
  }
}
Noah
  • 193
  • 11