0

Part 1
I know that QuickSort can be used 'in place' but could someone explain to me how Insertion sort Algorithm does this using 'in place'.

From my understanding:

Insertion Sort starts at the first value and compares it to the next value, if that value is less than our value they switch positions. We continue this recursively. (Short explanation)

So would you say that this is 'in place' because we don't create a new array to do this, but just compare two elements in an array?

If my understanding was wrong could someone please explain the algorithm for insertion sort in place.

Part 2
Also how would I use insertion sort to illustrate the idea of a loop invariant?

I know that a loop invariant is a condition that is immediately true before and after each iteration of a loop but I'm not sure how this would relate to an insertion sort.

Cœur
  • 37,241
  • 25
  • 195
  • 267
TheRapture87
  • 1,403
  • 3
  • 21
  • 31
  • 1
    What you're describing is bubble sort, not insertion sort. – Thorsten Dittmar Apr 20 '15 at 10:50
  • The book I'm using asked these questions and refers to insertion sort. Edit: I think i mis-understood you, are you saying my explanation of insertion sort is actually bubble sort? – TheRapture87 Apr 20 '15 at 10:54
  • Yes. Bubble Sort works like compare i to i+1 and if they are in wrong order, switch. In Insertion Sort, there is no switch. You take an item x, start at first position and if you find an item y that is greater than x, you insert the x before y, thus shifting the rest of the array to the right. Both Bubble Sort and Insertion Sort can be implemented in-place - they just work differently. http://en.wikipedia.org/wiki/Insertion_sort – Thorsten Dittmar Apr 20 '15 at 11:00

1 Answers1

0

Like Thorsten mentioned in the comments section, you have described bubble sort. Modified from Wikipedia, pseudocode for bubble sort is as follows:

procedure bubbleSort( A : list of sortable items )
   n = length(A)
   for i = 1 to n inclusive do // Outer Loop
     for j = 1 to n-1-i inclusive do
       /* if this pair is out of order */
       if A[j] > A[j+1] then
         swap(A[j], A[j+1])
       end if
     end for
   end for
end procedure

The loop invariant in bubble sort (for the outer loop) would be that, after each iteration of the loop, the entire array until the current value of i would be sorted. This is because, each time one reaches the outer loop, it will be after going through all iterations of the inner loop (from i to n-1), finding the minimum element there and swapping that element with the ith one.

Bubble sort is, indeed, in place, since all the sorting happens within the original array itself, and does not require a separate external array.

Edit- now onto insertion sort:

Pseudo code is as follows (all hail Wikipedia):

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

Here, at each step, what happens is that for each element, you select the appropriate location at which to insert it into the array, i.e., you insert it just after the first element that is smaller than it in the array. In a little more detail, what the inner loop does is that, it keeps shifting elements to the right till it encounter an element smaller than the element in consideration, at which point you insert the element just after the smaller element. What this will mean is that every element until the aforementioned element is sorted. the outer loop ensures that this is done for all elements within the array, which means that by the time the outer loop completes, the array is sorted.

The loop invariant for the outer loop is, like before, that after the ith iteration, all elements till the current i will be sorted. Just before the ith interation, however, all elements till i-1 will be sorted.

The insertion sort algorithm does not require an external array for sorting. More specifically, all operations are done on the array itself (except for the one variable we need to store the element that we are currently trying to insert into its appropriate location), and no external arrays are used- there is no copying from this array to another one, for example. So, the space complexity required by the algorithm (excluding, of course, the space the array itself occupies) will be O(1), as opposed to dependent on the size of the array, and the sort is in-place, much like bubble sort.

aspiring_sarge
  • 2,355
  • 1
  • 25
  • 32
  • Well, insertion sort can be implemented in-place. It just works differently than Bubble Sort :-) http://en.wikipedia.org/wiki/Insertion_sort – Thorsten Dittmar Apr 20 '15 at 11:01
  • Thanks for the explanation but I described bubble sort by mistake, I want to know how insertion sort would work for in place and how insertion sort can be used to illustrate the idea of a loop invariant – TheRapture87 Apr 20 '15 at 11:02
  • There we go. I've added a few more details. Let me know if there's anything that requires clarification :) – aspiring_sarge Apr 20 '15 at 11:27
  • Thanks for adding more details, it definitely helps! I'm just struggling to grasp the Loop Invariant! From the definition it says that the loop invariant must be true immediately before and after each iteration but how is this the case? The array is not sorted before and it is not sorted after until the end? – TheRapture87 Apr 20 '15 at 11:32
  • The whole array isn't, but the sub-array is. In the loop invariant, one is allowed to use the loop variable as well. So, if the loop variable is i, at each step, the array from the `1`st till the `(i-1)`th element will be sorted at the start of the loop, and the array from the `1`st to the `i`th element will be sorted at the end of the loop, and not the whole array. – aspiring_sarge Apr 20 '15 at 14:15