Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting an Array. Computer Memory shtGrades (short) Index0123 Values3291 Computer Memory shtGrades (short) Index0123 Values1239 We have… We want…

Similar presentations


Presentation on theme: "Sorting an Array. Computer Memory shtGrades (short) Index0123 Values3291 Computer Memory shtGrades (short) Index0123 Values1239 We have… We want…"— Presentation transcript:

1 Sorting an Array

2 Computer Memory shtGrades (short) Index0123 Values3291 Computer Memory shtGrades (short) Index0123 Values1239 We have… We want…

3 Why sort an Array? 574592 245579 Consider findmin, findmax, findrepeat on unsorted and sorted arrays.

4 MergeSortQuickSortInsertionSortShellSortUSA_Sort (We will learn BubbleSort )

5 First we need to learn how to swap two numbers.. 574592 579542

6 Dim intA As Integer = 1 Dim intB As Integer = 5 bntRedDemo.Text = "A is" & Str(intA) & ", and B is" & Str(intB) Dim intA As Integer = 1 Dim intB As Integer = 5 bntRedDemo.Text = "A is" & Str(intA) & ", and B is" & Str(intB)

7 Dim intA As Integer = 1 Dim intB As Integer = 5 bntRedDemo.Text = "A is" & Str(intA) & ", and B is" & Str(intB) intA = intB intB = intA bntRedDemo.Text = "Now A is" & Str(intA) & ", and B is" & Str(intB) Dim intA As Integer = 1 Dim intB As Integer = 5 bntRedDemo.Text = "A is" & Str(intA) & ", and B is" & Str(intB) intA = intB intB = intA bntRedDemo.Text = "Now A is" & Str(intA) & ", and B is" & Str(intB) Our first attempt is wrong!!

8 Dim intA As Integer = 1 Dim intB As Integer = 5 Dim intT As Integer bntRedDemo.Text = "A is" & Str(intA) & ", and B is" & Str(intB) intT = intA intA = intB intB = intT bntRedDemo.Text = "Now A is" & Str(intA) & ", and B is" & Str(intB) Dim intA As Integer = 1 Dim intB As Integer = 5 Dim intT As Integer bntRedDemo.Text = "A is" & Str(intA) & ", and B is" & Str(intB) intT = intA intA = intB intB = intT bntRedDemo.Text = "Now A is" & Str(intA) & ", and B is" & Str(intB) This works

9 shtS (short) Index0123 Values3291 We have seen how to swap two integers, let us see how to swap two elements of an array…

10 Dim intS() As Integer = {3, 2, 9, 1} Dim intT As Integer bntRedDemo.Text = "S0 is" & Str(intS(0)) & ", and S1 is" & Str(intS(1)) intT = intS(0) intS(0) = intS(1) intS(1) = intT bntRedDemo.Text = "Now S0 is" & Str(intS(0)) & ", and S1 is" & Str(intS(1)) Dim intS() As Integer = {3, 2, 9, 1} Dim intT As Integer bntRedDemo.Text = "S0 is" & Str(intS(0)) & ", and S1 is" & Str(intS(1)) intT = intS(0) intS(0) = intS(1) intS(1) = intT bntRedDemo.Text = "Now S0 is" & Str(intS(0)) & ", and S1 is" & Str(intS(1))

11 Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI As Integer For intI = 0 To UBound(intS) - 1 bntRedDemo.Text = "Adjacent pairs" & Str(intS(intI)) & " " & Str(intS(intI + 1)) Next intI Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI As Integer For intI = 0 To UBound(intS) - 1 bntRedDemo.Text = "Adjacent pairs" & Str(intS(intI)) & " " & Str(intS(intI + 1)) Next intI Note the “-1”

12 Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI As Integer For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then bntRedDemo.Text = "Do Swap" & Str(intS(intI)) & " " & Str(intS(intI + 1)) Else bntRedDemo.Text = "Don't Swap" & Str(intS(intI)) & "" & Str(intS(intI + 1)) End If Next intI Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI As Integer For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then bntRedDemo.Text = "Do Swap" & Str(intS(intI)) & " " & Str(intS(intI + 1)) Else bntRedDemo.Text = "Don't Swap" & Str(intS(intI)) & "" & Str(intS(intI + 1)) End If Next intI

13 Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI As Integer For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then intT = intS(intI) intS(intI) = intS(intI+1) intS(intI+1) = intT End If Next intI Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI As Integer For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then intT = intS(intI) intS(intI) = intS(intI+1) intS(intI+1) = intT End If Next intI After running this code, we have intS = 2, 3, 1, 9

14 Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI, intOuterLoop As Integer For intOuterLoop = 0 To UBound(intS) - 1 For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then intT = intS(intI) intS(intI) = intS(intI+1) intS(intI+1) = intT End If Next intI Next intOuterLoop Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI, intOuterLoop As Integer For intOuterLoop = 0 To UBound(intS) - 1 For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then intT = intS(intI) intS(intI) = intS(intI+1) intS(intI+1) = intT End If Next intI Next intOuterLoop After running this code, we have intS = 1, 2, 3, 9 !!

15 Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI, intOuterLoop As Integer For intOuterLoop = 0 To UBound(intS) - 1 For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then intT = intS(intI) intS(intI) = intS(intI+1) intS(intI+1) = intT End If Next intI Next intOuterLoop Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI, intOuterLoop As Integer For intOuterLoop = 0 To UBound(intS) - 1 For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then intT = intS(intI) intS(intI) = intS(intI+1) intS(intI+1) = intT End If Next intI Next intOuterLoop After running this code, we have intS = 1, 2, 3, 9 !! In the next slides I will represent all this text by a red box

16 Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI As Integer Dim blnIsSorted As Boolean = False While Not (blnIsSorted) blnIsSorted = True For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then intT = intS(intI) blnIsSorted = False End If Next intI End While Dim intS() As Integer = {3, 2, 9, 1} Dim intT, intI As Integer Dim blnIsSorted As Boolean = False While Not (blnIsSorted) blnIsSorted = True For intI = 0 To UBound(intS) - 1 If intS(intI) > intS(intI + 1) Then intT = intS(intI) blnIsSorted = False End If Next intI End While Swap 2 numbers


Download ppt "Sorting an Array. Computer Memory shtGrades (short) Index0123 Values3291 Computer Memory shtGrades (short) Index0123 Values1239 We have… We want…"

Similar presentations


Ads by Google