What is really interesting is how small optimizations affect the result.
Ive tried to improve my insert sort alghorithm and optimize it to get better results. Instead however, I end up with worse result. See the code below:
my initial insert sort alghorith:
void SortListIntegerValues6(object oList)
{
string list = GetLocalString(oList,"LIST");
int num = GetStringLength(list)/2;
string list_new = GetStringLeft(list,2);
int num_new = 1;
string char_new, charthis;
int valthis;
int min,max,th, n = 1;
while(n < num)
{
charthis = GetSubString(list,n++*2,2);
valthis = GetLocalInt(oList,charthis);
min = 0;
max = num_new;
while(TRUE)
{
th = (max+min+1)/2;
char_new = GetSubString(list_new,(th-1)*2,2);
if(valthis > GetLocalInt(oList,char_new))
{
if(th == 1 || valthis < GetLocalInt(oList,GetSubString(list_new,(th-2)*2,2)))
{
list_new = GetStringLeft(list_new,(th-1)*2)+charthis+char_new+GetStringRight(list_new,(num_new-th)*2);
break;
}
max = th;
}
else
{
if(th == num_new || valthis > GetLocalInt(oList,GetSubString(list_new,th*2,2)))
{
list_new = GetStringLeft(list_new,(th-1)*2)+char_new+charthis+GetStringRight(list_new,(num_new-th)*2);
break;
}
min = th;
}
}
num_new++;
}
//SetLocalString(oList,"LIST",list_new);
SpeakString("FINISHED");
}
First attempt to optimize it:
void SortListIntegerValues7(object oList)
{
string list = GetLocalString(oList,"LIST");
int num = GetStringLength(list);
string list_new = GetStringLeft(list,2);
int num_new = 1;
string char_new, charthis;
int valthis;
int min,max,th, n = 2;
while(n < num)
{
charthis = GetSubString(list,n,2);
valthis = GetLocalInt(oList,charthis);
min = 0;
max = num_new;
while(TRUE)
{
th = (max+min+1)/2;
char_new = GetSubString(list_new,(th-1)*2,2);
if(valthis > GetLocalInt(oList,char_new))
{
if(th == 1 || valthis < GetLocalInt(oList,GetSubString(list_new,(th-2)*2,2)))
{
list_new = GetStringLeft(list_new,(th-1)*2)+charthis+char_new+GetStringRight(list_new,(num_new-th)*2);
break;
}
max = th;
}
else
{
if(th == num_new || valthis > GetLocalInt(oList,GetSubString(list_new,th*2,2)))
{
list_new = GetStringLeft(list_new,(th-1)*2)+char_new+charthis+GetStringRight(list_new,(num_new-th)*2);
break;
}
min = th;
}
}
num_new++;
n+= 2;
}
//SetLocalString(oList,"LIST",list_new);
SpeakString("FINISHED");
}
Because this failed I tried different approach:
void SortListIntegerValues8(object oList)
{
string list = GetLocalString(oList,"LIST");
int num = GetStringLength(list)/2;
string list_new = GetStringLeft(list,2);
string char_new, charthis;
int valthis;
int num_new,min,max,th, n = 1;
while(n < num)
{
charthis = GetSubString(list,n++*2,2);
valthis = GetLocalInt(oList,charthis);
min = 0;
max = ++num_new;
while(TRUE)
{
th = (max+min+1)/2;
char_new = GetSubString(list_new,(th-1)*2,2);
if(valthis > GetLocalInt(oList,char_new))
{
if(th == 1 || valthis < GetLocalInt(oList,GetSubString(list_new,(th-2)*2,2)))
{
list_new = GetStringLeft(list_new,(th-1)*2)+charthis+char_new+GetStringRight(list_new,(num_new-th)*2);
break;
}
max = th;
}
else
{
if(th == num_new || valthis > GetLocalInt(oList,GetSubString(list_new,th*2,2)))
{
list_new = GetStringLeft(list_new,(th-1)*2)+char_new+charthis+GetStringRight(list_new,(num_new-th)*2);
break;
}
min = th;
}
}
}
//SetLocalString(oList,"LIST",list_new);
SpeakString("FINISHED");
}
And just for curiosity I tried what happens if I use subroutine function instead of direct system call see here:
string GetElement(string list, int nTh)
{
return GetSubString(list,nTh*2,2);
}
void SortListIntegerValues9(object oList)
{
string list = GetLocalString(oList,"LIST");
int num = GetStringLength(list)/2;
string list_new = GetStringLeft(list,2);
string char_new, charthis;
int valthis;
int num_new,min,max,th, n = 1;
while(n < num)
{
charthis = GetElement(list,n++);
valthis = GetLocalInt(oList,charthis);
min = 0;
max = ++num_new;
while(TRUE)
{
th = (max+min+1)/2;
char_new = GetSubString(list_new,(th-1)*2,2);
if(valthis > GetLocalInt(oList,char_new))
{
if(th == 1 || valthis < GetLocalInt(oList,GetSubString(list_new,(th-2)*2,2)))
{
list_new = GetStringLeft(list_new,(th-1)*2)+charthis+char_new+GetStringRight(list_new,(num_new-th)*2);
break;
}
max = th;
}
else
{
if(th == num_new || valthis > GetLocalInt(oList,GetSubString(list_new,th*2,2)))
{
list_new = GetStringLeft(list_new,(th-1)*2)+char_new+charthis+GetStringRight(list_new,(num_new-th)*2);
break;
}
min = th;
}
}
}
//SetLocalString(oList,"LIST",list_new);
SpeakString("FINISHED");
}
Ive initialized a list of 380 elements and ran each of these scripts on it, removing one random element at the time till it write FINISHED.
And the results were:
sort8: 379
sort6: 377
sort7: 374
sort9: 373
The bad result of the 7 really troubles me, I expected a save of a (1+number_of_elements) number of instructions and its actually even worse...
Can someone brought some light to this?