Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird insertion sort problem

Status
Not open for further replies.

marsd

IS-IT--Management
Apr 25, 2001
2,218
US
I have a standard insertion sort and swap function:

function isort(arr,a) {
for (i=2 ; i <= a ; i++) {
for (j = i ; j > 1 && arr[j - 1] > arr[j] ; j--) {
swap(arr,(j - 1),j)
}
}
}

function swap(array,ind1,ind2) {
sw = array[ind1]
array[ind1] = array[ind2]
array[ind2] = sw
}

The isort function returns this output:

12
13.23
23.12
21 (?)
65.43

Awk's native(?) asort returns this:

12
13.23
21
23.12
65.43

Any ideas?
 
sure, use LOCAL iterrators:

function isort(A,n, i,j,t)
{
for (i = 2; i <= n; i++)
{
for (j = i; j > 1 && A[j-1] > A[j]; j--)
{
t = A[j-1]; A[j-1] = A[j]; A[j] = t
}
}
}

vlad
 
Nah,That makes no difference. It is good form to declare
the locals , but not mandatory.

The problem was in a function that tried to eliminate all alpha chars from the array with a bad regexp. Once I got rid of that everything works as expected.

Thanks!
MMD
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top