Computer worst case: Bubble Sort Algorithm?

That's almost a bubble sort, but the outer loop is wrong. What you should see is something like: int size = intArray. Length boolean sorted = false while (not sorted) { .... sorted = true .... for (int j=1; j

.... .... modify the swap code to set sorted=false after any swap. .... } .... size = size-1; // one less unsorted record on each iteration. } A true bubble sort is supposed to stop after any pass where no swaps are made, and the code you posted doesn't do that.

It always runs the worst-case number of comparisons. N(n-1)/2 The code does work, though, and how it works is that the outer loop varies the j variable from 0 up to n-1. (n is short for intArray.length.) The value of j is the number of elements that have already been bubbled up to the end of the array in previous iterations of the outer loop.

That's why the upper bound on the inner loop is reduced by j in the inner loop. I used the local variable "size" to do the same thing, and there are a bunch of other ways to do that. The inner loop makes one bubble sort pass through the unsorted part of the array.

Every entry that is to the right of it's final resting place will move one position to the left. The largest element will be bubbled all the way to the right. I like looping from i=1 while i

Theoretically, the compiler should see that size (or intArray. Length) doesn't change and optimize that away, but I don't like using the optimizer as an excuse to be sloppy. Besides, they miss stuff sometimes.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions