1 / 16

Sorting an Array

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 ).

dolf
Download Presentation

Sorting an Array

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sorting an Array

  2. We have… Computer Memory We want… Computer Memory

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

  4. MergeSort QuickSort InsertionSort ShellSort USA_Sort (We will learn BubbleSort)

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

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

  7. 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)

  8. 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)

  9. 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))

  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 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

  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 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 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 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 !!

  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 Swap 2 numbers

More Related