Home > Net >  How do I get the original array index after quick sort?
How do I get the original array index after quick sort?

Time:03-09

How do I get the original array index after quick sort?
 # Region "quick sort" 
Sub test ()
Dim the data () As an Integer={80, 74, 95, 106, 110, 46, 87, 100, 99, 54, 68}
Dim indexArr () As an Integer=Enumerable. Range (0, the data. The Length). ToArray 'data before ordering index
QuickSortRelaxImproved (data, indexArr)
'excuse me: how to join in the quick sort code below indexArr (?)=? That can record sorting index, so as to produce the other processing according to this index
End Sub
Public Sub QuickSortRelaxImproved (Of TKey) (data () As TKey, indexArr () As an Integer, Optional Ascending As Boolean=False)
QuickSortRelaxImproved (data, 0, UBound (data), indexArr, Ascending)
End Sub
Public Sub QuickSortRelaxImproved (Of TKey) (data () As TKey, low As an Integer, high As an Integer, indexArr () As an Integer, Ascending As Boolean)
If low & gt;=high Then Return
Dim Asc As Integer
Dim Desc As Integer
If Ascending=True Then Ascending
'Asc=1
Desc=1
The Else 'descending
Asc=1
Desc=1
End the If
Dim temp As TKey=data (+ high (low) \ 2)
Dim I As Integer=low - 1
Dim j As Integer=high + 1
Dim index As Integer=+ high (low) \ 2
Do
I +=1
The Do While Comparer.Default.Com pare said (temp, data (I))=Desc
I +=1
Loop
J -=1
The Do While Comparer.Default.Com pare said (temp, data (j)=Asc
J -=1
Loop
If I & gt;=j Then Exit the Do
Swap (data, I, j)
If I=index Then
The index=j
ElseIf j=index Then
The index=I
End the If
Loop
If j=I Then
QuickSortRelaxImproved (data, j + 1, high, indexArr, Ascending)
QuickSortRelaxImproved (data, low, I - 1, indexArr, Ascending)
The Else
If the index & gt; I Then=
If the index & lt; & gt; I Then
Swap (data, index, I)
End the If
QuickSortRelaxImproved (data, I + 1, high, indexArr, Ascending)
QuickSortRelaxImproved (data, low, I - 1, indexArr, Ascending)
The Else
If the index & lt; & gt; J Then
Swap (data, index, j)
End the If
QuickSortRelaxImproved (data, j + 1, high, indexArr, Ascending)
QuickSortRelaxImproved (data, low, j - 1, indexArr, Ascending)
End the If
End the If
End Sub
Private Sub Swap (Of TKey) (data () As TKey, low As an Integer, high As Integer)
Dim temp As TKey=data (low)
Data (low)=data (high)
Data (high)=temp
End Sub
# End Region
  • Related