Last list item: Difference between revisions

Line 490:
 
=={{header|Ring}}==
===WithWithout sorting===
<lang ring>
see "working..." + nl
 
List = [6,81,243,14,25,49,123,69,11]
n = 0
 
while true
n++
Listif n = sort(List)1
first = see nl + "List[1] = "
second = showArray(List[2])
see nl
ok
secondfirst = min(List)
ind1 = find(List,first)
del(List,ind1)
second = delmin(List,ind2)
ind2 = find(List,second)
if ind1 < del(List,ind2)
del(List,ind2)
del(List,ind1)
else
del(List,ind1)
del(List,ind2)
ok
sum = first + second
add(List,sum)
if len(List) = 1
exit
ok
if n = 1
see nl + "List = "
showArray(List)
see nl
ok
showList(first,second,sum,List)
end
 
see "Last item is: " +List[1] + nl
see "done..." + nl
 
func showList(first,second,sum,List)
see "two smallest is = " + first + " + " + second + " = " + sum + nl
see "List = "
showArray(List)
 
func showArray(array)
txt = ""
Line 542 ⟶ 536:
see txt + nl
</lang>
{{out}}
===Without sorting===
<pre>
working...
 
List = [6,81,243,14,25,49,123,69,11]
 
two smallest is = 6 + 11 = 17
List = [81,243,14,25,49,123,69,17]
two smallest is = 14 + 17 = 31
List = [81,243,25,49,123,69,31]
two smallest is = 25 + 31 = 56
List = [81,243,49,123,69,56]
two smallest is = 49 + 56 = 105
List = [81,243,123,69,105]
two smallest is = 69 + 81 = 150
List = [243,123,105,150]
two smallest is = 105 + 123 = 228
List = [243,150,228]
two smallest is = 150 + 228 = 378
List = [243,378]
Last item is: 621
done...
</pre>
===WithoutWith sorting===
<lang ring>
see "working..." + nl
Line 551 ⟶ 568:
while true
n++
firstList = minsort(List)
if nfirst = List[1]
second = List[2]
ind1 = find(List,first)
del(List,ind1)
second = min(List)
ind2 = find(List,second)
del(List,if ind1 < ind2)
del(List,ind2)
del(List,ind1)
else
del(List,ind1)
showArraydel(List,ind2)
ok
sum = first + second
add(List,sum)
Line 592 ⟶ 615:
working...
 
List = [81,243,14,25,49,69,81,123,69243,17]
 
two smallest is = 6 + 11 = 17
List = [81,243,14,25,49,69,81,123,69243,17]
two smallest is = 14 + 17 = 31
List = [81,243,25,49,69,81,123,69243,31]
two smallest is = 25 + 31 = 56
List = [8149,24369,4981,123,69243,56]
two smallest is = 49 + 56 = 105
List = [69,81,243,123,69243,105]
two smallest is = 69 + 81 = 150
List = [243105,123,105243,150]
two smallest is = 105 + 123 = 228
List = [243,150,243,228]
two smallest is = 150 + 228 = 378
List = [243,378]
2,468

edits