Description: Write a sorting function, that takes in an array of numbers (size n) and outputs the array of numbers as a list, in sorted sequence (highest value to lowest value)
Notes:
• Use any development language you wish
• Do not use any language’s in-built sorting function (example Javascript array.sort). We’re asking you to write the full sorting algorhythm yourself
Example
• simpleSort({1,2,3,4})
Output:
• 4,3,2,1
Question 8b. Bonus: Name the sorting algorhythm
Module Module1
Sub sortArray(ByVal arr As Integer())
Dim temp As Integer = 0
'Sort array using bubble sort.
For i As Integer = 0 To 4 Step 1
For j As Integer = 4 To i + 1 Step -1
If (arr(j) > arr(j - 1)) Then
temp = arr(j)
arr(j) = arr(j - 1)
arr(j - 1) = temp
End If
Next
Next
End Sub
Sub Main()
Dim arr As Integer() = New Integer(5) {}
Console.WriteLine("Enter array elements: ")
For i = 0 To 4 Step 1
Console.Write("Element[{0}]: ", i)
arr(i) = Integer.Parse(Console.ReadLine())
Next
sortArray(arr)
Console.WriteLine("Array after sorting: ")
For i = 0 To 4 Step 1
Console.Write("{0} ", arr(i))
Next
Console.WriteLine()
Console.ReadLine()
End Sub
End Module
Comments
Leave a comment