VB icon

Bubble Sort

Email
Submitted on: 1/6/2015 2:58:00 AM
By: Roman Blachman (from psc cd)  
Level: Intermediate
User Rating: By 8 Users
Compatibility: VB 5.0, VB 6.0
Views: 817
 
     This code should explain the bubble sort ! Bubble sort is a sort algorithm that sortes string in very fast way. There are many sort algorithms but this is the best !
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :Bubble Sort
'**************************************
no apis.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Bubble Sort
' Description:This code should explain the bubble sort !
Bubble sort is a sort algorithm that sortes string in very fast way. There are many sort algorithms but this is the best !
' By: Roman Blachman (from psc cd)
'
' Inputs:This code needs only one parameter:
The input array (will be used for output also)
'
' Returns:This sub won't return anything. It will change the input array
'
' Assumes:If you have problems running it try this code.
(I assume that you created a list box that his name is: lstItems)
Place this code where you want (for example: command button)
Dim inArray() As String ' Input array
ReDim inArray(lstItems.ListCount - 1) ' Redim the array to the size of the list count items
For ic = 0 To lstItems.ListCount - 1
inArray(ic) = lstItems.List(ic) ' Put all the values from the list box to the array
Next
cBubbleSort inArray ' Sort the array
lstItems.Clear ' Clear the list
For ic = 0 To UBound(inArray)
lstItems.AddItem inArray(ic) ' Put the sorted items from the array
Next
'**************************************

Public Sub cBubbleSort(inputArray As Variant)
	Dim lDown As Long, lUp As Long
	For lDown = UBound(inputArray) To LBound(inputArray) Step -1
		For lUp = LBound(inputArray) + 1 To lDown
			If inputArray(lUp - 1) > inputArray(lDown) Then SwapValues inputArray(lUp - 1), inputArray(lDown)
		Next lUp
	Next lDown
End Sub
Public Sub SwapValues(firstValue As Variant, secondValue As Variant)
	Dim tmpValue As Variant
	tmpValue = firstValue
	firstValue = secondValue
	secondValue = tmpValue
End Sub
This is the same code but with explainations:
Public Sub cBubbleSort(inputArray As Variant)
	Dim lDown As Long, lUp As Long ' Two variables that will be used in the fors
	For lDown = UBound(inputArray) To LBound(inputArray) Step -1 ' One variable will go from the upper bound of the array
		For lUp = LBound(inputArray) + 1 To lDown ' and the second one will go from the lowest bound to the top
			If inputArray(lUp - 1) > inputArray(lDown) Then SwapValues inputArray(lUp - 1), inputArray(lDown) ' This line check if the value from the up-to-down for is higher than the value from the down-to-up for, if so the sub call a swap sub that switches the values places
		Next lUp ' Continue to the next value from down-to-up
	Next lDown ' Continue to the next value from up-to-down
End Sub
Public Sub SwapValues(firstValue As Variant, secondValue As Variant) ' This sub switches the values
	Dim tmpValue As Variant ' Temp variable to store the first value
	tmpValue = firstValue ' put the first value into a temp variable
	firstValue = secondValue ' put the second value into the first
	secondValue = tmpValue ' and then put the first value, that stored in a temp variable, into the second
End Sub
If this code wasn't helpful and you still want to know how the bubble sort algorithm works so go to this links:
I hope this code was helpful if so please vote for me.
1) http://technology.niagarac.on.ca/courses/comp435/labs/bubblesort.html
2) http://www.cis.ufl.edu/~ddd/cis3020/summer-97/lectures/lec16/tsld042.htm
3) http://www.enm.maine.edu/Courses/C/SourceCode/BUBBLE.html
4) http://www-ee.eng.hawaii.edu/Courses/EE150/Book/chap10/subsection2.1.2.2.html
5)http://www.scism.sbu.ac.uk/law/Section5/chap2/s5c2p13.html


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.