VERSION 5.00 Begin VB.Form frmBubbleSort Caption = "Bubble Sort" ClientHeight = 7035 ClientLeft = 60 ClientTop = 345 ClientWidth = 9120 LinkTopic = "Form1" ScaleHeight = 7035 ScaleWidth = 9120 StartUpPosition = 3 'Windows Default Begin VB.CommandButton cmdBESort Caption = "More Efficient Bubble Sort" Height = 495 Left = 6240 TabIndex = 5 Top = 3960 Width = 1215 End Begin VB.CommandButton cmdDone Caption = "Done" Height = 495 Left = 6240 TabIndex = 4 Top = 4800 Width = 1215 End Begin VB.CommandButton cmdSort Caption = "Bubble Sort" Height = 495 Left = 6240 TabIndex = 3 Top = 3120 Width = 1215 End Begin VB.TextBox txtNumElem Height = 495 Left = 6960 TabIndex = 1 Top = 960 Width = 735 End Begin VB.ListBox lstList Height = 4155 ItemData = "frmBubbleSort.frx":0000 Left = 480 List = "frmBubbleSort.frx":0002 TabIndex = 0 Top = 840 Width = 3615 End Begin VB.Label lblPrompt Caption = "# of Elements to sort" Height = 615 Left = 5040 TabIndex = 2 Top = 840 Width = 1575 End End Attribute VB_Name = "frmBubbleSort" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Option Explicit 'bubble sort 'm.booth '2003 12 19 Private Sub cmdBESort_Click() 'sort the elements in array 'numbers are randomly generated 'var/const Dim intDataArray() As Integer Dim intNumItems As Integer '#of array elements 'create array of random numbers intNumItems = txtNumElem.Text Call GenerateArray(intDataArray(), intNumItems) 'display original array in list box lstList.AddItem "Original Array" Call DisplayData(intDataArray(), lstList) 'sort array Call BubbleSortEfficient(intDataArray()) 'display sorted array lstList.AddItem "Sorted array" Call DisplayData(intDataArray(), lstList) End Sub Private Sub cmdDone_Click() Unload Me End Sub Private Sub Form_Load() Randomize End Sub Private Sub cmdSort_Click() 'sort the elements in array 'numbers are randomly generated 'var/const Dim intDataArray() As Integer Dim intNumItems As Integer '#of array elements 'create array of random numbers intNumItems = txtNumElem.Text Call GenerateArray(intDataArray(), intNumItems) 'display original array in list box lstList.AddItem "Original Array" Call DisplayData(intDataArray(), lstList) 'sort array Call BubbleSort(intDataArray()) 'display sorted array lstList.AddItem "Sorted array" Call DisplayData(intDataArray(), lstList) End Sub '********************************************** 'Modifies intArray() to contain intNumItem 'random integers ranging from 1 to intMax 'pre:Randomize called, array and numitem declared 'post:intArray() contains random ints up to max '*********************************************** Sub GenerateArray(ByRef intArray() As Integer, _ ByVal intNumItems As Integer) 'vars/const Const intMax As Integer = 100 Dim intCount As Integer 'resize array ReDim intArray(1 To intNumItems) 'generate a random number for each elements For intCount = LBound(intArray) To UBound(intArray) intArray(intCount) = Int(intMax * Rnd + 1) Next intCount End Sub '********************************************** 'displays the contents of intArray() in list box 'pre:intArray() contains at least 1 item 'post:intArray() items displayed in list box '********************************************** Sub DisplayData(ByRef intArray() As Integer, _ ByRef lstList As ListBox) 'vars/const Dim intCount As Integer 'display each item in list box For intCount = LBound(intArray) To UBound(intArray) lstList.AddItem intCount & vbTab & intArray(intCount) Next intCount End Sub '*********************************************** 'sorts array of numbers from low to high 'pre:intArray() has at least 1 element 'post:intArray() are sorted low to high '*********************************************** Sub BubbleSort(ByRef intArray() As Integer) 'vars/const Dim intItem As Integer Dim intCount As Integer Dim intTemp As Integer Dim sglStart As Single Dim sglFinish As Single Dim sglTime As Single 'start timer sglStart = Timer 'sort For intItem = LBound(intArray) To UBound(intArray) For intCount = LBound(intArray) To UBound(intArray) - 1 'compare adjacent elements If intArray(intCount) > intArray(intCount + 1) Then intTemp = intArray(intCount) intArray(intCount) = intArray(intCount + 1) intArray(intCount + 1) = intTemp End If 'comparison done Next intCount 'compare next 2 elements Next intItem 'repeat for each array element 'end timer sglFinish = Timer sglTime = sglFinish - sglStart 'display time in seconds MsgBox "Bubble Sort took: " & sglTime & " seconds" End Sub '*********************************************** 'sorts array of numbers from low to high 'pre:intArray() has at least 1 element 'post:intArray() are sorted low to high '*********************************************** Sub BubbleSortEfficient(ByRef intArray() As Integer) 'vars/const Dim intItem As Integer Dim intCount As Integer Dim intTemp As Integer Dim blnSwapReq As Boolean 'flag Dim sglStart As Single Dim sglFinish As Single Dim sglTime As Single 'start timer sglStart = Timer 'initialize flag blnSwapReq = True 'sort Do While blnSwapReq blnSwapReq = False For intCount = LBound(intArray) To UBound(intArray) - 1 'compare adjacent elements If intArray(intCount) > intArray(intCount + 1) Then intTemp = intArray(intCount) intArray(intCount) = intArray(intCount + 1) intArray(intCount + 1) = intTemp blnSwapReq = True 'swap made so more sorting End If 'comparison done Next intCount 'compare next 2 elements Loop 'repeat for each array element 'end timer sglFinish = Timer sglTime = sglFinish - sglStart 'display time in seconds MsgBox "Efficient Bubble Sort took: " & sglTime & " seconds" End Sub Private Sub txtNumElem_Change() 'clear the list box lstList.Clear End Sub