Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. Imports System.Threading
  2. Imports System.Threading.Tasks
  3.  
  4. Module Module1
  5.  
  6. Sub Main()
  7. coreCount = Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS")
  8. Console.WriteLine("Number Of Cores: {0}", coreCount)
  9. InitializeArray()
  10. CheckSorting()
  11. Dim StartTimer As New Stopwatch
  12. StartTimer.Start()
  13. MergeSortSequential(0, Length)
  14. StartTimer.Stop()
  15. Console.WriteLine("Algorithm is done for {0} ms", StartTimer.ElapsedMilliseconds)
  16. StartTimer.Reset()
  17. CheckSorting()
  18. Array = New List(Of Integer)
  19. InitializeArray()
  20. CheckSorting()
  21. StartTimer.Start()
  22. MergeSortConcurrent(0, Length)
  23. StartTimer.Stop()
  24. Console.WriteLine("Algorithm is done for {0} ms", StartTimer.ElapsedMilliseconds)
  25. CheckSorting()
  26. Console.ReadLine()
  27. End Sub
  28.  
  29. Dim Length As Integer = 1000000
  30. Dim Array As List(Of Integer) = New List(Of Integer)
  31. Dim Temp As List(Of Integer) = New List(Of Integer)
  32. Dim coreCount As Integer
  33.  
  34. Public Sub InitializeArray()
  35. Randomize()
  36. For i = 0 To Length - 1
  37. Array.Add(CInt(Int((1000000 * Rnd()) + 1)))
  38. Temp.Add(0)
  39. Next
  40. End Sub
  41.  
  42. Public Sub PrintArray()
  43. Console.Write("Array: ")
  44. For i = 0 To Array.Count - 1
  45. Console.Write($"{Array(i)} ")
  46. Next
  47. Console.WriteLine()
  48. End Sub
  49.  
  50. Public Sub CheckSorting()
  51. For i = 0 To Array.Count - 2
  52. If Array(i) > Array(i + 1) Then
  53. Console.WriteLine("Array is not sorted")
  54. Return
  55. End If
  56. Next
  57. Console.WriteLine("Array is sorted")
  58. End Sub
  59.  
  60. Public Sub MergeSortConcurrent(ByVal Start As Integer, ByVal Length As Integer)
  61. If Length <= Module1.Length / coreCount Then
  62. MergeSortSequential(Start, Length)
  63. Return
  64. End If
  65. Dim Limit As Integer = If(Length Mod 2 = 1, Math.Floor(Length / 2) + 1, Math.Floor(Length / 2))
  66. Dim Branches As New List(Of Task)
  67. Branches.Add(Task.Run(Sub() MergeSortConcurrent(Start, Limit)))
  68. Branches.Add(Task.Run(Sub() MergeSortConcurrent(Start + Limit, Math.Floor(Length / 2))))
  69. Dim t As Task = Task.WhenAll(Branches)
  70. t.Wait()
  71. Merge(Start, Length)
  72. Return
  73. End Sub
  74.  
  75. Public Sub MergeSortSequential(ByVal Start As Integer, ByVal Length As Integer)
  76. If Length < 2 Then
  77. Return
  78. End If
  79. If Length = 2 Then
  80. If Array(Start) > Array(Start + 1) Then
  81. Dim Temp = Array(Start)
  82. Array(Start) = Array(Start + 1)
  83. Array(Start + 1) = Temp
  84. End If
  85. Return
  86. End If
  87. Dim Limit As Integer = If(Length Mod 2 = 1, Math.Floor(Length / 2) + 1, Math.Floor(Length / 2))
  88. MergeSortSequential(Start, Limit)
  89. MergeSortSequential(Start + Limit, Math.Floor(Length / 2))
  90. Merge(Start, Length)
  91. End Sub
  92.  
  93. Public Sub Merge(ByVal Start As Integer, ByVal Length As Integer)
  94. Dim i As Integer = Start
  95. Dim Limit As Integer = If(Length Mod 2 = 1, Math.Floor(Length / 2) + 1, Math.Floor(Length / 2))
  96. Dim j As Integer = Start + Limit
  97. Dim TmpList As New List(Of Integer)
  98. While i < Start + Limit And j < Start + Length
  99. If Array(i) < Array(j) Then
  100. TmpList.Add(Array(i))
  101. i += 1
  102. Else
  103. TmpList.Add(Array(j))
  104. j += 1
  105. End If
  106. End While
  107. While i < Start + Limit
  108. TmpList.Add(Array(i))
  109. i += 1
  110. End While
  111. While j < Start + Length
  112. TmpList.Add(Array(j))
  113. j += 1
  114. End While
  115. For k = Start To Start + Length - 1
  116. Array(k) = TmpList(k - Start)
  117. Next
  118. End Sub
  119.  
  120. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement