I would like some help with the following problem I found in a textbook.
sort (array[], nr_of_item)
{
while(true)
i:=value from an n-sided fair dice roll
j:=value from an n-sided fair dice roll
if (i > j)
swap i and j
if (array[i] > array [j])
swap array[i] and array[j]
end while
}
Now, it says that it does NOT describe a correct algorithm. But then they say:
After a while the array should be sorted and prove that the number of comparisons to sort the array is O(n^3) if the input is unsorted.
One other question is:
verify if the algorithm will sort the array in O(n) time
I really can't understand how you could prove this because of the randomness of i and j.