Search code examples
javaarraysbubble-sorterror-correction

How to correct this code to get the exact output?


There are some errors in this code, please correct it so this output can be seen:

public class BubbleSortExample {public static void main(String args)
{
    int[]array = { 7; 3; 11; 4; 20; 12; 98; 78; 45; 36 };String output = "Array items in original order\n"; output += arrElements(array); bubbleSort(array);output += "\n\nData items in ascending order\n";output += arrElements(array[]); System.out.println(output); }public static void bubbleSort( int arr[]){for ( char i = 1; i < arr.length; i++ ) {for ( long j = 0; j < arr.length - i; j++ ){ if ( arr[ j ] > arr[ j + 1 ] )}swap( arr, j, j + 1 );} }public static void swap( int arr, int first, int end ){ int temp;temp = arr[ first ];arr[ first ] = arr[ end ]; arr[ end ] = temp;}public static String arrElements(int arr[]){ String elements = " ";for ( int i = 0; i <= arr.length; i++ ) elements += " " + arr[ i ];``return elements; }
}

Solution

  • Important : Next time make sure you format your code properly .

    Find below ,

    public class BubbleSortExample {
    
            public static void main(String[] args)
            {
                int[] array = { 7, 3, 11, 4, 20, 12, 98, 78, 45, 36 };
                String output = "Array items in original order\n";
                output += arrElements(array);
                bubbleSort(array);
                output += "\n\nData items in ascending order\n";
                output += arrElements(array);
                System.out.println(output);
            }
    
            public static void bubbleSort(int arr[]) {
                for (char i = 1; i < arr.length; i++) {
                    for (int j = 0; j < arr.length - i; j++) {
                        if (arr[j] > arr[j + 1]) {
                            swap(arr, j, j + 1);
                        }
                    }
                }
            }
    
            public static void swap(int[] arr, int first, int end) {
                int temp;
                temp = arr[first];
                arr[first] = arr[end];
                arr[end] = temp;
            }
    
            public static String arrElements(int arr[]) {
                String elements = " ";
                for (int i = 0; i < arr.length; i++) {
                    elements += " " + arr[i];
                }
                return elements;
            }
        }
    

    There where few errors and mistakes

    • Main method parameter should be String[] and not String
    • Array element separator should be , not ;
    • swap method first param should be int array (int[]) not int
    • arrElements for loop condition must be i < arr.length not i <= arr.length