I'm trying to implement insertion sort in TI-BASIC on the TI-84 and have nearly been successful. My solution sorts all but the first element of the list. In order to work properly, I believe this line:
While C>1 and L1(C)>V
needs to be
While C>0 and L1(C)>V
However this creates an error because apparently TI-BASIC is checking L1(0)
even though we are exiting the loop before getting there.
Could someone please explain how to avoid this problem and make the code work?
This is my code:
0->dim(L1
randIntNoRep(1,10,10)->L1
For(I,2,10)
L1(I)->V
I-1->C
While C>1 and L1(C)>V
L1(C)->L1(C+1)
C-1->C
End
V->L1(C+1)
End
Disp L1
After a lot of trial and error, i found the solution. The trick is if C=1 you can't have it in a while loop you have to have it in an If statement. Place this block of code after your while loop:
If C=1 and L1(C)>V
Then
L1(C)->L1(C+1)
C-1->C
End
The whole thing:
SetUpEditor L1
0->dim(L1
randIntNoRep(1,10,10)->L1
For(I,2,10)
L1(I)->V
I-1->C
While C>1 and L1(C)>V
L1(C)->L1(C+1)
C-1->C
End
If C=1 and L1(C)>V
Then
L1(C)->L1(C+1)
C-1->C
End
V->L1(C+1)
End
Disp L1