Insertion sort

Ausrine

New Member
Joined
Feb 8, 2017
Messages
3
Hello , i have an array A with element, i need to sort that array ( the lenght of array depend on n - cellB2)by taking element and putting it into the right place, by doing a loop it should give me an ordered list in column D. Can someone please tell me if my code is close to the correct code?
Sub InsertSort()
Dim i As Integer
n = Range("B2").Value
Dim test As Double
Dim temp As Double
Dim j As Integer
For i = 2 To n
j = A(i - 1, 1)
test = A(i, 1)
Do While j > 0
If A(j, 1) > test Then
temp = A(j, 1)
A(j, 1) = A(j + 1, 1)
A(j + 1, 1) = temp
j = A(j - 1, 1)
Else
test = A(j, 1)
End If
Loop
If j = 0 Then
test = A(1, 1)
End If
Next i
Range("D1:D100000").Value = A
End Sub
 

Forum statistics

Threads
1,226,871
Messages
6,193,443
Members
453,799
Latest member
shanley ducker

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top