I am trying to make a pseudo-sorter to sort values from 1 array and paste the results to a second array smaller by shifting values down the second array
since the position of the original array list is important, I can't just sort the original list and paste the top 3 smallest values to the new array. This method is only permissible.
So lets say for the size of array2 = 3
NEW SMALLER VALUE FOUND in array1 = 1
as it iterates thru the 1st array, it checks if its smaller than the value of the 1st value of the second array, then it shifts all the value within second array by 1 (the last number of 2nd array remove out of the array) to make room in the first position of array2 for that smaller value.
This only goes thru the array1 once (does not loop to find the "true" smallest)
It skips every "nthsteps" when iterating thru array1 for the sake of efficiency.
This is the code I got so far, yet it doesn't really work:
End Function
The thing is im trying to make the second array size dynamic and able to to change based on number of values 2nd array requires.
I am no expert in VBA or coding in general, so I'd appreciate if the solution is similar to how I write my code.
the desired output is like this in excel for Pseudo__sorter_smallest(data, 3, 5):
any inputs will be much appreciated
Thanks in advance!
since the position of the original array list is important, I can't just sort the original list and paste the top 3 smallest values to the new array. This method is only permissible.
So lets say for the size of array2 = 3
Code:
array2 (1,1) = 2
array2 (2,1) = 4
array2 (3,1) = 5
NEW SMALLER VALUE FOUND in array1 = 1
Code:
array2 (1,1) = 1
array2 (2,1) = 2
array2 (3,1) = 4
as it iterates thru the 1st array, it checks if its smaller than the value of the 1st value of the second array, then it shifts all the value within second array by 1 (the last number of 2nd array remove out of the array) to make room in the first position of array2 for that smaller value.
This only goes thru the array1 once (does not loop to find the "true" smallest)
It skips every "nthsteps" when iterating thru array1 for the sake of efficiency.
This is the code I got so far, yet it doesn't really work:
VBA Code:
Function pseudo_sorter_smallest(range1 As Range, size As Integer, nthsteps As Integer)
Dim array1() As Double
Dim array2() As Double
ncount = range1.Cells.Count
ReDim array1(1 To ncount, 1 To 1) As Double
ReDim array2(1 To size, 1 To 2) As Double
For i = 1 To ncount
array1(i, 1) = range1(i, 1)
Next i
array2(1, 1) = array1(1, 1)
array2(1, 2) = i
For i = 1 To ncount Step nthsteps
If array1(i, 1) < array2(1, 1) Then
For j = 1 To size - 1
array2(j + 1, 1) = array2(j, 1)
array2(j + 1, 2) = array2(j, 2)
Next j
array2(1, 1) = array1(i, 1)
array2(1, 2) = i
End If
Next i
pseudo_sorter_smallest = array2()
End Function
End Function
The thing is im trying to make the second array size dynamic and able to to change based on number of values 2nd array requires.
I am no expert in VBA or coding in general, so I'd appreciate if the solution is similar to how I write my code.
the desired output is like this in excel for Pseudo__sorter_smallest(data, 3, 5):
values | position of values in data |
1 | 24 |
2 | 20 |
4 | 3 |
any inputs will be much appreciated
Thanks in advance!