Advertisement
gnamp

Bubble Sort

Aug 19th, 2012
2,905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' Smallest to Biggest
  2. '==============
  3.  
  4. TextWindow.Title= "Biggest & Smallest Outta a set of Numbers
  5. ESC= Text.GetCharacter(27)  ' ASCII code for "Esc" key
  6. LF=  Text.GetCharacter(10)  ' Jumps 1 line (Line Feed)
  7.  
  8. 'Section 1
  9. 'Receives input for a set of numbers:
  10.  
  11. Start:
  12.  
  13. TextWindow.Clear()
  14. TextWindow.ForegroundColor= "Red
  15. TextWindow.Write("How many numbers to you want to order?: ")
  16. set = TextWindow.Read()
  17. TextWindow.WriteLine ("Type in numbers confirming each one with 'ENTER' key:" + LF)
  18. TextWindow.ForegroundColor= "Blue
  19.  
  20. For index= 1 To set
  21.  TextWindow.Write ("Number " + index + ": ")
  22.  number[index] = TextWindow.ReadNumber() * 1
  23. EndFor
  24.  
  25. 'Section 2
  26. 'Displays the 3 [SET of - DM Ed.]  typed numbers:
  27.  
  28. TextWindow.ForegroundColor= "DarkRed
  29. TextWindow.WriteLine (LF + "These are the numbers entered:")
  30. TextWindow.ForegroundColor= "DarkYellow
  31.  
  32. For index= 1 To set
  33.  TextWindow.WriteLine ( index + "-> " + number[index] )
  34. EndFor
  35.  
  36. 'Section 3
  37. 'Calculates the average outta the numbers:
  38. sum = 0
  39. For i = 1 To set
  40.  sum = sum + number [i]
  41. EndFor
  42.  average= sum / set
  43.  
  44. TextWindow.ForegroundColor= "Yellow
  45. TextWindow.WriteLine (LF + "Average for the numbers-> " + average + LF)
  46.  
  47. 'Section 4
  48. 'Algorithm to bubble-sort out from smallest to biggest outta typed numbers:
  49.  
  50. For times= 1 To set-1
  51.   For index= 1 To set-1
  52.     If number[index] > number[index+1] Then
  53.       aux= number[index]
  54.       number[index]=   number[index+1]
  55.       number[index+1]= aux
  56.     EndIf
  57.   EndFor
  58. EndFor
  59.  
  60. 'Section 5
  61. 'Displays the comparison result:
  62.  
  63. TextWindow.ForegroundColor= "Green
  64. TextWindow.WriteLine ("The ordered numbers from smallest to biggest:")
  65. TextWindow.ForegroundColor= "Cyan
  66.  
  67. For index= 1 To set
  68.   TextWindow.Write ( number[index] + ", " )
  69. EndFor
  70.  
  71. 'Section 6
  72. 'Decides whether to end or restart the program depending on the key pressed:
  73.  
  74. TextWindow.ForegroundColor= "Magenta
  75. TextWindow.WriteLine (LF + LF + "Press 'Q' or 'Esc' to quit or any other to restart...")
  76.  
  77. key= Text.ConvertToUpperCase ( TextWindow.ReadKey() )
  78.  
  79. If key = "Q" Or key = ESC Then
  80.   Program.End()
  81. Else
  82.   Goto start
  83.  
  84. EndIf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement