简体   繁体   中英

Insertion Sort Java Superposition

Here I have a new array that I've created:

{18, 45, 33, 65, 76, 32, 96, 12, 46, 68}

Now, I'm using an insertion sort on this array . But I was wondering about something.

At some points we as humans cannot tell how many iterations have gone on in an array just by looking at it, right?

For example, imagine that at a point we have this newly made array after the program iterates over the array a few times using insertion sort:

{18, 33, 45, 65, 76, 32, 96, 12, 46, 68}

Just by looking, isn't it impossible to know how many comparisons the computer made? I asked my teacher, and she says that looking at this new array it is obvious how many times that the new array was compared by the computer. IE, just by looking at this new array my teacher could tell how many times it has been iterated upon.

How? Isn't it impossible to know for certain? She says that it is a specific number. Can someone explain how many times the new array has been compared?

It's impossible. You can sometimes pin it down fairly closely, particularly if you know the original array, but you can't get an exact number just by looking at the array state.

For example, if you don't know the original array state, and you look and see that the first 6 elements of the array are sorted, you could be looking at the state after the algorithm is done with the first 6 elements. However, it's also possible that those elements looked like that from the start, and you could be looking at the state before the algorithm has done anything, or after the algorithm has finished with the first 3 elements. All you really know is that it hasn't finished working with the 7th element yet.

If you do know the original array state, you can often tell exactly how much of the array has been sorted, and thus how many iterations have passed. However, not all iterations will affect the array state. For example, if the array was already sorted, the array state will never change. Also, it may be impossible to tell if the algorithm is in the middle of an iteration.

It is possible to determine the upper and lower bound number of comparisons done by the insertion sort algorithm for the case your provided. But it depends on the implementation. I will base my answer on the pseudocode below .

for i = 1 to length(A)
    j ← i
    while j > 0 and A[j-1] > A[j]
        swap A[j] and A[j-1]
        j ← j - 1
    end while
end for

If you take look at both states of the array, let's call the first state one

{18, 45, 33, 65, 76, 32, 96, 12, 46, 68}

and the second state two,

{18, 33, 45, 65, 76, 32, 96, 12, 46, 68}

you'll see that the change occurred at the beginning of the array on index 1 and 2.

The algorithm starts at index 1 and compares arr[1] with all its predecessors. This is only arr[0] , so there is 1 comparison and another if you count j > 0 . Since the previous element is smaller, not bigger, the while loop is left and the for loop starts its next iteration.

There arr[2] , which is 33, is compared to its predecessor arr[1] , since it is smaller, it is swapped. Then it is compared to arr[0] . It is bigger so the while is left.

This is the minimum that needs to be done to change the array from state one to state two. But the next two iterations of the algorithm, which would try to sort in 65 and 76, don't change the state and so it is entirely possible for the algorithm to have progressed until 76.

Only the next step would change the state further as it would sort in the 32.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM