January 19, 2021, 04:52:27 AM

### Author Topic: [bb] Sorted Linked Type Lists by Miracle [ 1+ years ago ]  (Read 477 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] Sorted Linked Type Lists by Miracle [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Sorted Linked Type Lists
Author : Miracle
Posted : 1+ years ago

Description : This sort algorithm is essentially a bubble sort, which can be slow especially for very long lists.

Code :
Code: BlitzBasic
1. Global NUM_COWS = 8
2.
3. Type cow
4.         Field z
5.         Field pr.cow
6.         Field nx.cow
7. End Type
8.
9. Type list
10.         Field firstcow.cow
11.         Field lastcow.cow
12. End Type
13.
14. Global list.list = New list
15. For t = 1 To NUM_COWS
16.         moo.cow = New cow
17.         mooz = Rand(0,100)
18.         SortCowsOnZ(moo)
19. Next
20.
21. Print "Unsorted list:"
22. For moo.cow = Each cow
23.         Print mooz
24. Next
25. Print
26. Print "Sorted list:"
27. moo = listfirstcow
28. Repeat
29.         Print mooz
30.         moo = moo
31. x
32. Until moo = Null
33.
34. WaitKey()
35.
36. End
37.
38. Function SortCowsOnZ( cow.cow )
39.         moo.cow = listlastcow
40.         done = 0
41.         If cow
42. x <> Null And cowpr <> Null Then
43.                 If cowpr <> Null Then cowpr
44. x = cow
45. x
46.                 If cow
47. x <> Null Then cow
48. xpr = cowpr
49.         EndIf
50.         If moo <> Null Then
51.                 Repeat
52.                         If mooz >= cowz Then
53.                                 cowpr = moo
54.                                 cow
55. x = moo
56. x
57.                                 If moo
58. x <> Null Then moo
59. xpr = cow
60.                                 moo
61. x = cow
62.                                 done = 1
63.                         EndIf
64.                         moo = moopr
65.                 Until moo = Null Or done = 1
66.         EndIf
67.         If done = 0 Then
68.                 cow
69. x = listfirstcow
70.                 If listfirstcow <> Null Then listfirstcowpr = cow
71.         EndIf
72.         If cow
73. x = Null Then listlastcow = cow
74.         If cowpr = Null Then listfirstcow = cow
75. End Function