locked
Bubble sort RRS feed

  • Question

  • Can someone please post an example of a bubble sort?  I need to sort an array of integers.

    Specifically,
    the array is named straightFlush[] and contains z number of items.

    Also, is there an easy way to flush the contents of an array?  I have a subroutine that I am calling which creates an array.  I would like to make sure I am not getting junk data, so would like to initialize the array to a null state, if there is such a thing.

    Thanks!
    Monday, January 25, 2010 4:33 AM

Answers

  • bubblesort:
      swapped = "False"
      For i = 1 To z-1
        Object = straightFlush[i]
        If(straightFlush[i+1] > Object) Then
          swapped = "True"
          straightFlush[i] = straightFlush[i+1]
          straightFlush[i+1] = Object
        EndIf
      EndFor
      If(swapped = "True") Then
        Goto bubblesort
      EndIf
    The code above assumes the first index of the array is 1 (rather than 0). Keep in mind that bubble sort is not the fastest among sort routines, but it'll get you started.

    As for clearing an array, it is useful to know that internally all arrays are seen as strings. So assigning an empty string to an array clears it.
    straightFlush = ""
    Small Basic returns all uninitialised values as 0.
    • Marked as answer by litdev Tuesday, January 26, 2010 12:11 AM
    Monday, January 25, 2010 1:37 PM

All replies

  • bubblesort:
      swapped = "False"
      For i = 1 To z-1
        Object = straightFlush[i]
        If(straightFlush[i+1] > Object) Then
          swapped = "True"
          straightFlush[i] = straightFlush[i+1]
          straightFlush[i+1] = Object
        EndIf
      EndFor
      If(swapped = "True") Then
        Goto bubblesort
      EndIf
    The code above assumes the first index of the array is 1 (rather than 0). Keep in mind that bubble sort is not the fastest among sort routines, but it'll get you started.

    As for clearing an array, it is useful to know that internally all arrays are seen as strings. So assigning an empty string to an array clears it.
    straightFlush = ""
    Small Basic returns all uninitialised values as 0.
    • Marked as answer by litdev Tuesday, January 26, 2010 12:11 AM
    Monday, January 25, 2010 1:37 PM
  • Excellent, thanks for the code.  Now that I have that, I will have my program sort the possible straight flush cards in order, then subtract the first card from the last card.  If the result is 1, then the player has a straight flush.  

    There is very little on the net about smallbasic, as a matter of fact almost all the info is here.  I am going to try to introduce my 14 year old son and 7 year old daughter to it soon.  Thanks again for the help Magus, I'm sure I will be calling on you again!  :)
    Monday, January 25, 2010 2:46 PM