Question

Write a function to sort an array of integers and also determine and explain the worst...

Write a function to sort an array of integers and also determine and explain the worst
case complexity (Big O notation) of your algorithm. (You may NOT use Collections.sort)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE

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[i]

                    int temp = arr[j];

                    arr[j] = arr[j+1];

                    arr[j+1] = temp;

                }

    }

Worst case occurs when array is reverse sorted. This is because in case, every pair of elements need to be swapped at each iteration making the time complexity O(n^2),

Add a comment
Know the answer?
Add Answer to:
Write a function to sort an array of integers and also determine and explain the worst...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT