MHB Insertion Sort: Explained and Applied

  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Applied Sort
AI Thread Summary
The discussion focuses on implementing the Insertion Sort algorithm for lists, particularly singly linked lists, as opposed to arrays. Participants explore how to adapt the array-based algorithm to work with linked lists, emphasizing the need for pointers to navigate and manipulate list elements. A key point is the importance of maintaining access to the list while shifting elements, which requires careful pointer management. The conversation also includes suggestions for abstracting the algorithm further to improve its adaptability. Overall, the thread provides insights into the complexities of sorting linked lists using Insertion Sort.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hi! (Smile)

$\text{ Could you explain me how I could sort the following list, applying the Insertion sort ? }$

View attachment 3445
 

Attachments

  • list.png
    list.png
    1 KB · Views: 139
Technology news on Phys.org
Code:
Given A[1...N];
for i = 2 to N 
//put elements in its right position
  Insert(A[i])
//A[1..i] is sorted now
end for 

Insert (A[i])
{
//compare A[i] to A[k] ;  1<=k<=i-1 
//We A[k] to right if  A[i] > A[k] else A[k+1]=A[i]
for k=i-1 to 1 
  if (A[k] > A[i])
    A[j] =A[j+1];
  else
    A[i] = A[j+1] ; break;
  end if
end for
}

Applying the algorithm above

Code:
100-(10)-2-6-230-77-33
10-100-(2)-6-230-77-33
2-10-100-(6)-230-77-33
2-6-10-100-(230)-77-33
2-6-10-100-230-(77)-33
2-6-10-77-100-230-(33)
2-6-10-33-77-100-230
//The parenthesis represents the element to be inserted next.
 
ZaidAlyafey said:
Code:
Given A[1...N];
for i = 2 to N 
//put elements in its right position
  Insert(A[i])
//A[1..i] is sorted now
end for 

Insert (A[i])
{
//compare A[i] to A[k] ;  1<=k<=i-1 
//We A[k] to right if  A[i] > A[k] else A[k+1]=A[i]
for k=i-1 to 1 
  if (A[k] > A[i])
    A[j] =A[j+1];
  else
    A[i] = A[j+1] ; break;
  end if
end for
}

This algorithm uses an array, instead of a list. How could we apply the Insertion Sort to a list? (Thinking)
 
evinda said:
This algorithm uses an array, instead of a list. How could we apply the Insertion Sort to a list? (Thinking)

A list is an abstract data structure which holds an ordered sequence of elements. An array is one concrete way to implement a list, by storing the elements contiguously in memory and simply interpreting A as "the ith element". Basically, an array is a kind of list. Does that answer your question?
 
evinda said:
This algorithm uses an array, instead of a list. How could we apply the Insertion Sort to a list? (Thinking)

If it is a singly linked list with unique elements we use that head.next to move from one element to another. Then to insert an element "ele" to its right position we create a temporary variable temp to traverse the list using temp.next from the beginning and compare it to the value of ele in hand if temp.next is greater than ele we place it their by putting temp.data = value. Then we have to shift all elements one position to right until we reach the original position of ele. The insertion stops if temp.next.data = ele;
 
evinda said:
I understand the Insertion Sort.. But, could you explain me how I can apply it to lists? (Thinking)

What is your definition of a list? What do you think a list is? How would you access the elements of a list in pseudocode? (that's actually the only operation needed in insertion sort since you don't need to add or remove elements, only compare and swap)
 
evinda said:
I understand the Insertion Sort.. But, could you explain me how I can apply it to lists? (Thinking)

Suppose A is a pointer to the first element of a list.

Then where the sort algorithm refers to A[k] you could use for instance the algorithm:
Code:
procedure Nth(A, n)
  P = A
  for i = 2 to n
    P = P.next
  return P
(Thinking)
 
  • #10
evinda said:
I understand the Insertion Sort.. But, could you explain me how I can apply it to lists? (Thinking)

What is unclear in my explanation ?

ZaidAlyafey said:
If it is a singly linked list with unique elements we use that head.next to move from one element to another. Then to insert an element "ele" to its right position we create a temporary variable temp to traverse the list using temp.next from the beginning and compare it to the value of ele in hand if temp.next is greater than ele we place it their by putting temp.data = value. Then we have to shift all elements one position to right until we reach the original position of ele. The insertion stops if temp.next.data = ele;
 
  • #11
I like Serena said:
Suppose A is a pointer to the first element of a list.

Then where the sort algorithm refers to A[k] you could use for instance the algorithm:
Code:
procedure Nth(A, n)
  P = A
  for i = 2 to n
    P = P.next
  return P
(Thinking)

At the following algorithm:

Code:
1.Insertion Sort(A[1...n]){
2.  int key,i,j;
3.  for (j=2; j<n+1; j++){
4.       key=A[j];
5.      i=j-1;
6.       while (i>0 && A[i]>key){
7.                A[i+1]=A[i];
8.                i=i-1;
9.       }
10.      A[i+1]=key;
11. }
12.  return A;
13.}
I changed the lines 7,8 like that:

Code:
 Nth(A,i-1).next=Nth(A,i+1) , Nth(A,i+1).next=Nth(A,i)

Is it right? (Thinking)
 
  • #12
ZaidAlyafey said:
What is unclear in my explanation ?

How could we shift all elements one position to the right? (Worried)

Is that what I have written in post #11 right? (Thinking)
 
  • #13
Is that what I have written in post #11 right? (Thinking)

It looks okay'ish, but I find it very difficult to interpret, so I'm not sure. (Sweating)

evinda said:
How could we shift all elements one position to the right? (Worried)

That's a better question. (Nod)
Let's see if we can come up with a sub algorithm for this.

Suppose P points to the element before the first one that we want to shift.
And suppose Q points to the key element that comes after the last element that we want to shift.

$$
\underset{{}^\uparrow_P}{\boxed{2}} \to \boxed{10} \to \boxed{100}
\to \underset{{}^\uparrow_Q}{\boxed{key=6} }
\to \boxed{230} \to \boxed{77} \to \boxed{33}
$$

What should be do with P and Q to achieve what you asked? (Wondering)
 
  • #14
I like Serena said:
It looks okay'ish, but I find it very difficult to interpret, so I'm not sure. (Sweating)
That's a better question. (Nod)
Let's see if we can come up with a sub algorithm for this.

Suppose P points to the element before the first one that we want to shift.
And suppose Q points to the key element that comes after the last element that we want to shift.

$$
\underset{{}^\uparrow_P}{\boxed{2}} \to \boxed{10} \to \boxed{100}
\to \underset{{}^\uparrow_Q}{\boxed{key=6} }
\to \boxed{230} \to \boxed{77} \to \boxed{33}
$$

What should be do with P and Q to achieve what you asked? (Wondering)

It should be like that:

Code:
Q.next=P.next;
P.next=Q;

Right? (Thinking)
 
  • #15
evinda said:
It should be like that:

Code:
Q.next=P.next;
P.next=Q;

Right? (Thinking)

Almost.
What will the next element of $100$ be? (Wondering)
 
  • #16
I like Serena said:
Almost.
What will the next element of $100$ be? (Wondering)

So should it be like that?

Code:
P.next.next.next=Q.next;
Q.next=P.next;
P.next=Q;

(Thinking)
 
  • #17
evinda said:
So should it be like that?

Code:
P.next.next.next=Q.next;
Q.next=P.next;
P.next=Q;

(Thinking)

Something like that (except you have one "next" too many). (Smile)

But it will only work for this specific sequence then.
I gave this sequence only as an example... (Wasntme)
 
  • #18
I like Serena said:
Something like that (except you have one "next" too many). (Smile)

But it will only work for this specific sequence then.
I gave this sequence only as an example... (Wasntme)

I haven't understood how we could do it for a general case... (Worried)

Could you explain it to me? (Thinking)
 
  • #19
evinda said:
I haven't understood how we could do it for a general case... (Worried)

Could you explain it to me? (Thinking)

Well, apparently we also need a pointer to the last element that we want to shift.
Let's call it R.
We might find it by scanning through the list with a while-loop, but let's not go there yet.

$$
\underset{{}^\uparrow_P}{\boxed{2}} \to \boxed{10} \to \underset{{}^\uparrow_R}{\boxed{100}}
\to \underset{{}^\uparrow_Q}{\boxed{key=6} }
\to \boxed{230} \to \boxed{77} \to \boxed{33}
$$

Then the sub algorithm is:
Code:
Procedure: Shift sub sequence after key(P,R,Q)

Input: P   pointer to list element before the sub sequence
       R   pointer to last element of the sub sequence
       Q   pointer to key element after the sub sequence

  R.next = Q.next
  Q.next = P.next
  P.next = Q
(Thinking)
 
  • #20
I like Serena said:
Well, apparently we also need a pointer to the last element that we want to shift.
Let's call it R.
We might find it by scanning through the list with a while-loop, but let's not go there yet.

To find where R should point, couldn't we use the function
Code:
Procedure Nth(A,n)
that you have written in the post #9, since R points to the previous element of the key? Or am I wrong? (Thinking)

I like Serena said:
$$
\underset{{}^\uparrow_P}{\boxed{2}} \to \boxed{10} \to \underset{{}^\uparrow_R}{\boxed{100}}
\to \underset{{}^\uparrow_Q}{\boxed{key=6} }
\to \boxed{230} \to \boxed{77} \to \boxed{33}
$$

Then the sub algorithm is:
Code:
Procedure: Shift sub sequence after key(P,R,Q)

Input: P   pointer to list element before the sub sequence
       R   pointer to last element of the sub sequence
       Q   pointer to key element after the sub sequence

  R.next = Q.next
  Q.next = P.next
  P.next = Q
(Thinking)

I understand! (Nod)
 
  • #21
evinda said:
To find where R should point, couldn't we use the function
Code:
Procedure Nth(A,n)
that you have written in the post #9, since R points to the previous element of the key? Or am I wrong? (Thinking)

Yep!
We could! (Smile)Suppose we rewrite your algorithm a little, making a step to make it more abstract:
Code:
1.Insertion Sort(A[1..n]){
2.  int key,i,j;
3.  for (j=2; j<n+1; j++){
4.       key=A[j];
5.       Find i < j such that A[i] <= key or else set i to 0
6.       Shift sub sequence from i+1 to j-1 to after key j
7.       A[i+1]=key;
8. }
9.  return A;
10.}

How could we adapt it further to the use of lists? (Wondering)
 
  • #22
I like Serena said:
Suppose we rewrite your algorithm a little, making a step to make it more abstract:
Code:
1.Insertion Sort(A[1..n]){
2.  int key,i,j;
3.  for (j=2; j<n+1; j++){
4.       key=A[j];
5.       Find i < j such that A[i] <= key or else set i to 0
6.       Shift sub sequence from i+1 to j-1 to after key j
7.       A[i+1]=key;
8. }
9.  return A;
10.}

How could we adapt it further to the use of lists? (Wondering)

So, is it like that? (Thinking)

Code:
1.Insertion Sort (NODE *A){
2.   int key, i, j;
3.   for(j=2; j<n+1; j++){
4.        key=Nth(A,j).data;
5.        i=j-1;
6.        while(i>0 && Nth(A,i).data>key){
7.                Nth(A,j-1).next=Nth(A,j);
8.                Nth(A,j).next=Nth(A,i+1);
9.                Nth(A,i+1).next=Nth(A,j)
10.               i=i-1;
11.      }
12.      Nth(A, i+1).data=key;
13.  }
14.  return A;
15.}
 
  • #23
evinda said:
So, is it like that? (Thinking)

Code:
1.Insertion Sort (NODE *A){
2.   int key, i, j;
3.   for(j=2; j<n+1; j++){
4.        key=Nth(A,j).data;
5.        i=j-1;
6.        while(i>0 && Nth(A,i).data>key){
7.                Nth(A,j-1).next=Nth(A,j);
8.                Nth(A,j).next=Nth(A,i+1);
9.                Nth(A,i+1).next=Nth(A,j)
10.               i=i-1;
11.      }
12.      Nth(A, i+1).data=key;
13.  }
14.  return A;
15.}

I'm afraid that after:
[m]7. Nth(A,j-1).next=Nth(A,j);[/m]
the list is broken and you cannot get to the original Nth(A,j).next any more in line 8. (Worried)

Additionally, where is $n$ coming from? (Wondering)
 
  • #24
I like Serena said:
I'm afraid that after:
[m]7. Nth(A,j-1).next=Nth(A,j);[/m]
the list is broken and you cannot get to the original Nth(A,j).next any more in line 8. (Worried)

Additionally, where is $n$ coming from? (Wondering)

Could we maybe replace the for with the following while? (Thinking)

Code:
1.Insertion Sort (NODE *A){
2.   int key, i, j;
3.   while (A.next != NULL){
4.        key=Nth(A,j).data;
5.        i=j-1;
6.        while(i>0 && Nth(A,i).data>key){
7.                Nth(A,j-1).next=Nth(A,j);
8.                Nth(A,j).next=Nth(A,i+1);
9.                Nth(A,i+1).next=Nth(A,j)
10.               i=i-1;
11.      }
12.      Nth(A, i+1).data=key;
13.      A.next=A.next.next;
14.  }
15.  return A;
16.}

What could I change after the $7^{th}$ line? (Worried)
 
  • #25
Manipulating the list halfway destroys normal access to it. (Worried)
You can resolve this by tracking the relevant pointers separately.
For instance:
Code:
7.                P = Nth(A, i+1)
8.                Q = Nth(A, j-1)
9.                R = Nth(A, j)
10.               P.next = ...

Btw, should you be referring to j and j-1 here? (Wondering)
 
  • #26
I like Serena said:
Manipulating the list halfway destroys normal access to it. (Worried)
You can resolve this by tracking the relevant pointers separately.
For instance:
Code:
7.                P = Nth(A, i+1)
8.                Q = Nth(A, j-1)
9.                R = Nth(A, j)
10.               P.next = ...

Btw, should you be referring to j and j-1 here? (Wondering)

So, should it be like that? (Thinking)

Code:
    1.Insertion Sort (NODE *A){
    2.   int key, i, j=2;
    3.   while (A.next != NULL){
    4.        key=Nth(A,j).data;
    5.        i=j-1;
    6.        while(i>0 && Nth(A,i).data>key){
    7.                P=Nth(A, i+1);
    8.                Q=Nth(A, j-1);
    9.                R=Nth(A, j);
    10.               Q.next=R.next;
    11.               R.next=P.next;
    12.               P.next=R;
    13.               i=i-1;
    14.       }       
    15.       P.data=key;
    16.       A.next=A.next.next;
    17.       j++;
    18.  }
    19.  return A;
    20.}
 
  • #27
evinda said:
So, should it be like that? (Thinking)

Code:
    7.                P=Nth(A, i+1);
    8.                Q=Nth(A, j-1);
    9.                R=Nth(A, j);
    10.               Q.next=R.next;
    11.               R.next=P.next;
    12.               P.next=R;

That would work better, but you're swapping the wrong values.
You're supposed to achieve [m]A[i+1]=A[/m]. (Worried)

Anyway, it would be better to leave the elements where they are, and just reassign the values.
You can do that with: [m]Nth(A,i+1).data = Nth(A,i).data[/m] instead of what you have now. (Wasntme)
 
  • #28
I like Serena said:
That would work better, but you're swapping the wrong values.
You're supposed to achieve [m]A[i+1]=A[/m]. (Worried)

Anyway, it would be better to leave the elements where they are, and just reassign the values.
You can do that with: [m]Nth(A,i+1).data = Nth(A,i).data[/m] instead of what you have now. (Wasntme)


The prof told us that we should not just reassign the values. (Shake)

Could you explain me what I could change, in order to achieve $A[i+1]=A$? (Thinking)
 
  • #29
evinda said:
The prof told us that we should not just reassign the values. (Shake)

Could you explain me what I could change, in order to achieve $A[i+1]=A$? (Thinking)


Since you won't to lose track of any of the nodes, you'll have to swap those 2 around (as in the movie ZaidAlyafey posted (Wink)).

You can do that with:
Code:
P = Nth(A, i-1)
Q = Nth(A, i)
R = Nth(A, i+1)
P.next = R
Q.next = R.next
R.next = Q

But what would be much better, is if you use the inner while-loop only to find the place where the key should be inserted.
And, after the while-loop, move the node with the key to that location. (Mmm)
 
  • #30
I like Serena said:
Since you won't to lose track of any of the nodes, you'll have to swap those 2 around (as in the movie ZaidAlyafey posted (Wink)).

You can do that with:
Code:
P = Nth(A, i-1)
Q = Nth(A, i)
R = Nth(A, i+1)
P.next = R
Q.next = R.next
R.next = Q

But what would be much better, is if you use the inner while-loop only to find the place where the key should be inserted.
And, after the while-loop, move the node with the key to that location. (Mmm)

So, do I have to do it like that? (Thinking)

Code:
    Insertion Sort (NODE *A){
       int key, i, j=2;
       while (A.next != NULL){
            key=Nth(A,j).data;
            i=j-1;
            while(i>0 && Nth(A,i).data>key){
                 P = Nth(A, i-1);
                 Q = Nth(A, i);
                 R = Nth(A, i+1);
                 i=i-1;
             }
             P.next = R;
             Q.next = R.next;
             R.next = Q;      
             P.data=key;
             A.next=A.next.next;
             j++;
       }
       return A;
    }
 
  • #31
evinda said:
So, do I have to do it like that? (Thinking)

Code:
    Insertion Sort (NODE *A){
       int key, i, j=2;
       while (A.next != NULL){
            key=Nth(A,j).data;
            i=j-1;
            while(i>0 && Nth(A,i).data>key){
                 P = Nth(A, i-1);
                 Q = Nth(A, i);
                 R = Nth(A, i+1);
                 i=i-1;
             }
             P.next = R;
             Q.next = R.next;
             R.next = Q;      
             P.data=key;
             A.next=A.next.next;
             j++;
       }
       return A;
    }

Something like that... (Sweating)

What happens if you apply this algorithm to the list in your OP? (Wondering)
 
  • #32
I like Serena said:
Something like that... (Sweating)

What happens if you apply this algorithm to the list in your OP? (Wondering)

At the first iteration, in the inner while loop isn't it P=Nth(A, 0); ? (Thinking)
Which is its value? (Thinking)
 
  • #33
evinda said:
At the first iteration, in the inner while loop isn't it P=Nth(A, 0); ? (Thinking)
Which is its value? (Thinking)

Currently that is not properly defined.
But we can define it ourselves such that it will return NULL if it does not exist. (Wasntme)
 
  • #34
I like Serena said:
Anyway, it would be better to leave the elements where they are, and just reassign the values.
You can do that with: [m]Nth(A,i+1).data = Nth(A,i).data[/m] instead of what you have now. (Wasntme)

So, would I just have to replace the lines 7-12 with [m]Nth(A,i+1).data = Nth(A,i).data[/m] ? (Thinking)
Code:
1.Insertion Sort (NODE *A){
2.   int key, i, j;
3.   for(j=2; j<n+1; j++){
4.        key=Nth(A,j).data;
5.        i=j-1;
6.        while(i>0 && Nth(A,i).data>key){
7.                Nth(A,i+1).data = Nth(A,i).data
8.  }
9.  return A;
10.}
 
  • #35
Or do I have to add at the code something else? (Thinking)
 
  • #36
evinda said:
Or do I have to add at the code something else? (Thinking)

Weird.
I thought I had already answered in this thread. :confused:
evinda said:
So, would I just have to replace the lines 7-12 with [m]Nth(A,i+1).data = Nth(A,i).data[/m] ? (Thinking)
Code:
1.Insertion Sort (NODE *A){
2.   int key, i, j;
3.   for(j=2; j<n+1; j++){
4.        key=Nth(A,j).data;
5.        i=j-1;
6.        while(i>0 && Nth(A,i).data>key){
7.                Nth(A,i+1).data = Nth(A,i).data
8.  }
9.  return A;
10.}

Yes, but you still need lines 8-10:
Code:
8.                i=i-1;
9.       }
10.      A[i+1]=key;
(Wasntme)
 
  • #37
I like Serena said:
Weird.
I thought I had already answered in this thread. :confused:

Yes, but you still need lines 8-10:
Code:
8.                i=i-1;
9.       }
10.      A[i+1]=key;
(Wasntme)

So, is it like that? (Thinking)

Code:
1.Insertion Sort (NODE *A){
2.   int key, i, j;
3.   for(j=2; j<n+1; j++){
4.        key=Nth(A,j).data;
5.        i=j-1;
6.        while(i>0 && Nth(A,i).data>key){
7.                Nth(A,i+1).data = Nth(A,i).data
8.                i=i-1;
9.       }
10.      A[i+1]=key;
11. return A;
 
  • #38
evinda said:
So, is it like that? (Thinking)

Code:
1.Insertion Sort (NODE *A){
2.   int key, i, j;
3.   for(j=2; j<n+1; j++){
4.        key=Nth(A,j).data;
5.        i=j-1;
6.        while(i>0 && Nth(A,i).data>key){
7.                Nth(A,i+1).data = Nth(A,i).data
8.                i=i-1;
9.       }
10.      A[i+1]=key;
11. return A;

Well... we can't do [m]A[i+1]=key[/m] with a linked list... (Worried)

Btw, can you tell what the order of this algorithm is now? (Wondering)
 
  • #39
I like Serena said:
Well... we can't do [m]A[i+1]=key[/m] with a linked list... (Worried)

So, is it like that? (Thinking)

Code:
1.Insertion Sort (NODE *A){
2.   int key, i, j;
3.   for(j=2; j<n+1; j++){
4.        key=Nth(A,j).data;
5.        i=j-1;
6.        while(i>0 && Nth(A,i).data>key){
7.                Nth(A,i+1).data = Nth(A,i).data
8.                i=i-1;
9.       }
10.      Nth(A,i+1).data=key;
11. return A;
I like Serena said:
Btw, can you tell what the order of this algorithm is now? (Wondering)

Is it $O(n^3)$ ? (Thinking)
 
  • #40
evinda said:
So, is it like that? (Thinking)

Is it $O(n^3)$ ? (Thinking)

Yes! (Nod)
 
  • #41
I like Serena said:
Yes! (Nod)

And could we also write an algorithm with complexity $O(n^2)$ ? (Thinking)
 
  • #42
evinda said:
And could we also write an algorithm with complexity $O(n^2)$ ? (Thinking)

Yes. (Smile)
 
  • #43
I like Serena said:
Yes. (Smile)

Could you give me a hint, how we could do this? (Thinking)
 
  • #44
evinda said:
Could you give me a hint, how we could do this? (Thinking)

We would need to make the algorithm a little bit more abstract. (Nerd)

Something like:
Code:
1.Insertion Sort(A[1..n])
2.  j = 2
3.  for each key starting with A[2]
4.       Find element at i < j such that A[i] <= key or else set i to 0
5.       Shift sub sequence from i+1 to j-1 to after key at j
6.       j = j + 1
7.  return A;

The inner loop that was $O(n^2)$ can be replaced by an $O(1)$ operation (line 5). (Mmm)
 
Last edited:
  • #45
I like Serena said:
We would need to make the algorithm a little bit more abstract. (Nerd)

Something like:
Code:
1.Insertion Sort(A[1..n])
2.  j = 2
3.  for each key starting with A[2]
4.       Find element at i < j such that A[i] <= key or else set i to 0
5.       Shift sub sequence from i+1 to j-1 to after key at j
6.       A[i+1]=key;
7.       j = j + 1
8.  return A;

The inner loop that was $O(n^2)$ can be replaced by an $O(1)$ operation (line 5). (Mmm)

Could you explain me further how we could shift a sub sequence with time complexity $O(1)$ ? (Thinking)
 
  • #46
evinda said:
Could you explain me further how we could shift a sub sequence with time complexity $O(1)$ ? (Thinking)

Suppose we have n=1000 elements, and we have i=5, and j=800.
Furthermore, suppose we have a pointer P to element i, a pointer Q to j-1, and a pointer R to j. (Sweating)

Then we can rehook the elements in the list by:
[m]Q->next = R->next;
R->next = P->next;
P->next = R;[/m]

Regardless of n, this is always 3 operations, so O(1). (Whew)
 
  • #47
I like Serena said:
Suppose we have n=1000 elements, and we have i=5, and j=800.
Furthermore, suppose we have a pointer P to element i, a pointer Q to j-1, and a pointer R to j. (Sweating)

Then we can rehook the elements in the list by:
[m]Q->next = R->next;
R->next = P->next;
P->next = R;[/m]

Regardless of n, this is always 3 operations, so O(1). (Whew)

So, you mean that it is like that? :confused:

Code:
InsertionSort(A[1...n])
j=2;
for (j=2; j<n; j++){
     key=A[j];
     for (i=0; i<j; i++){
          if (A[i]<key){
             Q->next=R->next;
             R->next=P->next;
             P->next=R;
          }
          else i=0;
          P->next->data=key;
          j=j+1;
     }
     return A;
}

Or have I understood it wrong? (Worried)
If it's right, don't we have to find the positions to which Q,P and R point? (Thinking)
Also, at the else-statement, why do we set i=0? (Thinking)
 
  • #48
evinda said:
So, you mean that it is like that? :confused:

Code:
InsertionSort(A[1...n])
j=2;
for (j=2; j<n; j++){
     key=A[j];
     for (i=0; i<j; i++){
          if (A[i]<key){
             Q->next=R->next;
             R->next=P->next;
             P->next=R;
          }
          else i=0;
          P->next->data=key;
          j=j+1;
     }
     return A;
}

Or have I understood it wrong? (Worried)
If it's right, don't we have to find the positions to which Q,P and R point? (Thinking)
Also, at the else-statement, why do we set i=0? (Thinking)

Actually, we won't need i, j, or n any more.
It's more like this:
Code:
InsertionSort(NODE* A)
  NODE *P, *Q, *R, *S;
  int key;

  if (A == NULL)
    return A;
  Q = A;
  R = Q->next;

  // For each key starting at j = 2
  for (Q = A, R = Q->next; R != NULL; Q = R, R = S) {
    key = R->data;
    S = R->next;

    // Find element at i < j such that A[i] <= key or else set i to 0
    for (P = A; (P != R) && (P->data <= key); P = P->next) {
    }

    // Shift sub sequence from i+1 to j-1 to after key at j
    if (P->data > key) {
      Q->next = R->next;
      R->next = P->next;
      P->next = R;
    } else {
      Q->next = R->next;
      R->next = A;
      A = R;
    }
  }
  return A;
}
(Thinking)
 
  • #49
I like Serena said:
Actually, we won't need i, j, or n any more.
It's more like this:
Code:
InsertionSort(NODE* A)
  NODE *P, *Q, *R, *S;
  int key;

  if (A == NULL)
    return A;
  Q = A;
  R = Q->next;

  // For each key starting at j = 2
  for (Q = A, R = Q->next; R != NULL; Q = R, R = S) {
    key = R->data;
    S = R->next;

    // Find element at i < j such that A[i] <= key or else set i to 0
    for (P = A; (P != R) && (P->data <= key); P = P->next) {
    }

    // Shift sub sequence from i+1 to j-1 to after key at j
    if (P->data > key) {
      Q->next = R->next;
      R->next = P->next;
      P->next = R;
    } else {
      Q->next = R->next;
      R->next = A;
      A = R;
    }
  }
  return A;
}
(Thinking)

Can we use S, at the for loop, without initializing it? (Thinking)

What shoud be its value? :confused:
 
  • #50
evinda said:
Can we use S, at the for loop, without initializing it? (Thinking)

What shoud be its value? :confused:

I have introduced S to keep track of the element that comes after R, which we need to be able to move on to the next element. (Thinking)

It is initialized at the beginning of the for-loop.
The first time it is used is as part of the increment-part of the for-loop, which comes later. (Wasntme)
 
Back
Top