VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Basic bubble sort. Generates 20 random numbers, displays them in a text box, then sorts them and re

by Josh Tickell (1 Submission)
Category: Math/Dates
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sat 3rd November 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Basic bubble sort. Generates 20 random numbers, displays them in a text box, then sorts them and returns the number of times the array was

API Declarations


Dim swapmade As Boolean
Dim temp, randnum, val1, val2 As Integer
Dim countr4, countr3, countr2, countr As Integer

Rate Basic bubble sort. Generates 20 random numbers, displays them in a text box, then sorts them and re



Private Sub Command1_Click()

'Generates 20 random from 1 - 1000 numbers and stores them in the array, numarray

Randomize

Do While countr <= 20
    randnum = Int(1000 * Rnd)
    numarray(countr) = randnum
    List1.AddItem numarray(countr)
    countr = countr + 1
Loop

End Sub

Private Sub bubbleSort()

'Determines whether one number is larger than the next

countr2 = 1
swapmade = False

Do While countr2 < 20
    
    If numarray(countr2 + 1) < numarray(countr2) Then
        Call swap
    End If
    
    countr2 = countr2 + 1
Loop

End Sub

Private Sub swap()

'Swaps the numbers

temp = numarray(countr2)
numarray(countr2) = numarray(countr2 + 1)
numarray(countr2 + 1) = temp
swapmade = True

End Sub

Private Sub Command2_Click()

'Sorts array until no more swaps need to be made

Do
    Call bubbleSort
    countr3 = countr3 + 1
Loop Until swapmade = False

'Prints new sorted array in list2

Do While countr4 <= 20
    List2.AddItem numarray(countr4)
    countr4 = countr4 + 1
Loop

'Prints the amount of times the array was processed before it was in order

Label1.Caption = "The array was traversed " & countr3 & " times."

End Sub

Private Sub Command3_Click()

'Resets everything except numbers in array

List1.Clear
List2.Clear
countr = 1
countr2 = 1
countr3 = 0
countr4 = 1
Label1.Caption = ""
swapmade = True

End Sub

Private Sub Form_Load()

countr = 1
countr2 = 1
countr3 = 0
countr4 = 1
swapmade = True

End Sub


Download this snippet    Add to My Saved Code

Basic bubble sort. Generates 20 random numbers, displays them in a text box, then sorts them and re Comments

No comments have been posted about Basic bubble sort. Generates 20 random numbers, displays them in a text box, then sorts them and re. Why not be the first to post a comment about Basic bubble sort. Generates 20 random numbers, displays them in a text box, then sorts them and re.

Post your comment

Subject:
Message:
0/1000 characters