160 likes | 286 Vues
Sorting an Array. We have…. Computer Memory. We want…. Computer Memory. Why sort an Array?. Consider findmin, findmax, findrepeat on unsorted and sorted arrays. MergeSort QuickSort InsertionSort ShellSort USA_Sort (We will learn BubbleSort ).
E N D
We have… Computer Memory We want… Computer Memory
Why sort an Array? Consider findmin, findmax, findrepeat on unsorted and sorted arrays.
MergeSort QuickSort InsertionSort ShellSort USA_Sort (We will learn BubbleSort)
Dim intA As Integer = 1 Dim intB As Integer = 5 bntRedDemo.Text ="A is" & Str(intA) & ", and B is" & Str(intB)
Our first attempt is wrong!! 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)
This works 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)
We have seen how to swap two integers, let us see how to swap two elements of an array…
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, 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”
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 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
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 !!
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 In the next slides I will represent all this text by a red box After running this code, we have intS = 1, 2, 3, 9 !!
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