Sorting Signed Numbers
ATS
Posts: 4
I need to sort an array of signed integers in ascending order. After attempting to do a simple bubble sort, I get this result:
38
44
81
118
-94
-81
-50
-44
-7
Based upon this result, I know I need to first check somehow to see if the integers being sorted are negative and do something if they are... How can I resolve this problem? Check out my attached sort routine code. Thanks.
38
44
81
118
-94
-81
-50
-44
-7
Based upon this result, I know I need to first check somehow to see if the integers being sorted are negative and do something if they are... How can I resolve this problem? Check out my attached sort routine code. Thanks.
bs2
744B
Comments
-Phil
If you want to do a signed compare, you first need to look at the sign bits. If they're both positive or both negative, you can compare them directly. If the signs are opposite, you only need to compare the signs since (-) < (+)
Read the chapter in the Stamp Manual on memory allocation for further discussion of this technique.