Download presentation
Presentation is loading. Please wait.
Published byIsabel Welch Modified over 8 years ago
1
Bubble Sort Notes David Beard CIS220
2
Bubble Sort for Strings Double pass algorithm to sort a single dimensional array. Inner loop “bubbles” largest element to top/end of the array Outer loop runs inner loop enough times to “bubble” each element to its correct location Subsequent slides contain variation of bubble sort for string array using a switch subroutine.
3
Bubble Sort for Strings Dim c(4) as string, I as integer. S as string = “” c(0) = “dog” c(1) = “cat” c(2) = “house” c(3) = “mouse” c(4) = “car” bubbleSortStrings(b) for i = 0 to c.getUpperBound(0) s = s & c(I) & ” “ next I Msgbox(s)
4
Bubble Sort for Strings Sub bubbleSortStrings(c as string()) dim bubblePass as integer, I as integer for I = 0 to c.getUpperBound(0)-1 for bubblePass = 1 to ubound(c) – (1+I) If c(bubblePass) > c(bubblePass+1) then Temp = c(bubblePass) c(bubblePass) = c(bubblePass+1) c(bubblePass+1) = temp endif next bubblePass next I end sub
5
SwapString Subroutine Sub swapString(byref a as string, _ byref b as string) dim foo as integer ‘temp holding place foo = a a = b b = foo end sub
6
Bubble Sort Algorithm with Integers Initial ordering: 2, 4, 6, 4, 8, 7, 9, 3 After first bubblePass (– next 2 slides): 2, 4, 4, 6, 7, 8, 3, 9 After second bubblepass: 2, 4, 4, 6, 7, 3, 8, 9 After third bubblepass: 2, 4, 4, 6, 3, 7, 8, 9 After fourth bubblepass: 2, 4, 4, 3, 6, 7, 8, 9 After fifth bubblepass: 2, 4, 3, 4, 6, 7, 8, 9 After sixth bubblepass: 2, 3, 4, 6, 7, 8, 9
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.