Created
April 20, 2017 10:37
-
-
Save stden/1bcfdf4571658ab308e5c32c74b96611 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub SortingMain() | |
Dim area As Range, nrOfValues As Long | |
Set area = Range("A1").CurrentRegion | |
nrOfValues = area.Cells.Count | |
Call ShellSort(area, nrOfValues) | |
End Sub | |
Sub ShellSort(V As Range, nrOfValues&) | |
Dim gap&, i&, k&, tmp& | |
Dim noexchange As Boolean | |
gap = nrOfValues | |
Do While gap > 1 | |
gap = gap \ 2 'denominator = 2 | |
Do | |
noexchange = True | |
For i = 1 To nrOfValues - gap | |
k = i + gap | |
If V(k) < V(i) Then tmp = V(k): V(k) = V(i): V(i) = tmp: noexchange = False | |
Next i | |
Loop Until noexchange = True | |
Loop | |
' Almost (decreasingly) sorted – first and last items are exchanged: 1, 4, 3, 2, 5 | |
tmp = V(1): V(1) = V(nrOfValues): V(nrOfValues) = tmp | |
End Sub | |
Sub GenerateNumbersMain() | |
Dim nrOfValues As Long, first As Long, last As Long | |
Dim xy As Range, max As Long, pos As Long | |
Set xy = Range("A1") | |
first = Range("first").Value | |
last = Range("last").Value | |
nrOfValues = Range("nr_of_values").Value | |
Call DeleteValues(xy) | |
Call GenerateNumbers(xy, first, last, nrOfValues) | |
End Sub | |
Sub GenerateNumbers(xy As Range, first As Long, last As Long, howMany As Long) | |
Dim i | |
For i = 1 To howMany | |
xy(i, 1) = Int(Rnd() * (last - first + 1) + first) | |
Next i | |
End Sub | |
Sub DeleteValues(xy As Range) | |
xy.CurrentRegion.ClearContents | |
End Sub | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment