Jump to content

Insertion Sort algorithm

- - - - -

  • Please log in to reply
No replies to this topic

#1
existentialist

existentialist

    Newbie

  • Members
  • PipPip
  • 14 posts
Ok so I'm reading an insertion sort example in pseudocode (pascal-like) but it's different from the implementations i've seen in java and even the example at Wikipedia. Can someone explain this to me?


procedure insertion sort(a[1], a[2], a[3]... a[n] : real numbers with n>= 2)

for j := 2 to n

begin

    i := 1

    while a[j] >a[i]

        i := i + 1

    m := a[j]

    for k := 0 to j - i - 1

        a[j-k] := a[j-k-1]

    a[i] := m

end

NOTE: I added square brackets into the notation in place of a subscript.

So far I have this working draft in C:


void insertion_sort() {

    int i,j,k,m;

    int n = ELEMENTS-1;

    for (j=1; j<n; j++) {

        i=0;

        while (arr[j] > arr[i])

            ++i;

        m = arr[j];

        for(k=0; k>j-i-1; k--)

            arr[j-k] = arr[j-k-1];

        arr[i] = m;

    }

}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users