Sorting sequences
Author |
Message |
em010
|
Posted: Thu Dec 01, 2011 9:09 pm Post subject: Sorting sequences |
|
|
What is it you are trying to achieve?
So I would like to sort a sequence of numbers... the fragment that I made compares numbers and switches them if the previous one is greater than the other.
What is the problem you are having?
I looped this so that it would compare until it has made a complete loop without swapping any numbers and would like to know what kind of exit statement to use...
Describe what you have tried to solve this problem
<Answer Here>
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
The fragment that I will show is this:
*i have already declared variables and have a sequence, just need to sort them
[syntax="turing"]
loop
for i:1..6
if upicks(i)> upicks(i+1) then
upicks(7):= upicks(i+1)
upicks(i+1):= upicks(i)
upicks(i):= upicks(7)
end if
end for
exit when ________________?
end loop
Please specify what version of Turing you are using
<Answer Here>
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tony
|
Posted: Thu Dec 01, 2011 9:46 pm Post subject: RE:Sorting sequences |
|
|
code: |
exit when <complete loop without swapping any numbers>
|
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Amarylis
|
Posted: Thu Dec 01, 2011 10:11 pm Post subject: Re: Sorting sequences |
|
|
I would use two arrays in here, one for the sorted numbers and one for the unsorted. Use some nested loops in here to make it so you compare all the values of one array to one value of the second array (first array has unsorted values), and to set the value of the second array to the biggest number in the first array's values. It should be simple enough as
Turing: |
%% This is already inside your loops
if array2(x) < array1 (i) then
array2(x) := array1 (i)
end if
|
then for the next bit, just make sure that the value for array2 (x) is also smaller than array2(x-1)
|
|
|
|
|
|
Aange10
|
Posted: Thu Dec 01, 2011 10:46 pm Post subject: Re: Sorting sequences |
|
|
Here's a program I got, and re-coded to show you how to bubble sort. Which is what your doing. It works with numbers and with integers.
If you want to use numbers, than so be it. Just make sure that the comparison is instead of Str.Lower() its strint()
(Line 24)
Description: |
|
Download |
Filename: |
Alphabetize.t |
Filesize: |
1.38 KB |
Downloaded: |
50 Time(s) |
|
|
|
|
|
|
|
|