Click here to Skip to main content
15,889,839 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
class BubbleSort {
    void bubbleSort(int arr[])
    {
        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]) {
                    // swap arr[j+1] and arr[j]
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
    }
 
    /* Prints the array */
    void printArray(int arr[])
    {
        int n = arr.length;
        for (int i = 0; i < n; ++i)
            System.out.print(arr[i] + " ");
        System.out.println();
    }
 
    // Driver method to test above
    public static void main(String args[])
    {
        BubbleSort ob = new BubbleSort();
        int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
        ob.bubbleSort(arr);
        System.out.println("Sorted array");
        ob.printArray(arr);
    }
}

What I have tried:

<pre>class BubbleSort {
    void bubbleSort(int arr[])
    {
        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]) {
                    // swap arr[j+1] and arr[j]
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
    }
 
    /* Prints the array */
    void printArray(int arr[])
    {
        int n = arr.length;
        for (int i = 0; i < n; ++i)
            System.out.print(arr[i] + " ");
        System.out.println();
    }
 
    // Driver method to test above
    public static void main(String args[])
    {
        BubbleSort ob = new BubbleSort();
        int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
        ob.bubbleSort(arr);
        System.out.println("Sorted array");
        ob.printArray(arr);
    }
}
Posted
Updated 8-Sep-22 1:53am
Comments
Patrice T 8-Sep-22 6:20am    
And you have a question ?

1 solution

I fail to see the point of this "question": the code runs with the example you use, but it's poor quality student grade software with no practical use outside being handed in as a homework solution.

And that's a problem for a couple of reasons: firstly because it's easy to find code like this and so it's easy for tutors to find - which means whoever hands this in as their own work is wide open punishment for plagiarism.
And secondly because it's counter productive: you don't learn any skill by looking, you learn by doing - you can watch as much of the Tour de France as you like, you are still going to fall off when you first try to ride a bicycle!
And homework is set to get you started on a development skill: a trivial task like this teaches you how to think about a problem and translate a specification (the assignment) into a product (an app even a student will only run once or twice).

So posting it here as a question does nothing except make you look kinda silly - especially in a few month time when you will look back at this and wish you have never put your name to it, much less did it publicly ... :laugh:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900