使用索引号排序

时间:2015-01-15 05:06:42

标签: vb.net

如何在vb.net中实现基于索引的排序

有人可以指导我。通过给我一个良好的开端

例如

array = 9 8 7 6 5 4 3 2 1

指数= 5

从9开始计数,第5个字符为5。

所以第一个排序的字符是5。

删除5并保持9 8 7 6 4 3 2 1

起始值应该是4(当前第5个字符)现在第5个字符是9,因为它的循环

未排序数组:8 7 6 4 3 2 1

排序数组:5 9

任何提示

2 个答案:

答案 0 :(得分:1)

首先,我绝对不会打电话给这个"排序&#34 ;;它有点像确定性(非随机)"改组"数组。但我会抓住这个......

基本上,首先使用List(Of T)而不是数组,因为它们允许您在列表中的任何位置轻松删除,而不是使用固定大小的数组。接下来,使用运行计数器跟踪输入列表中的当前位置,并使用Mod运算符有效地使索引“包裹”#39;在列表的末尾。使用While循环继续,直到处理完输入中的所有项目。看起来应该是这样的:

Dim input As New List(Of Integer) From { 9, 8, 7, 6, 5, 4, 3, 2, 1 }
Dim stepBy As Integer = 5
Dim index As Integer = 0
Dim output As New List(Of Integer)
While input.Count > 0
    index = (index + stepBy - 1) Mod input.Count
    output.Add(input(index))
    input.RemoveAt(index)
End While

在这种情况下,输出为:

5, 9, 3, 6, 7, 4, 1, 8, 2

答案 1 :(得分:1)

我认为这是一个有趣的问题,我喜欢p.s.w.g的答案。

...但我很想知道任何内置的.Net集合是否会促成一个相当简单的循环列表。

这是我使用LinkedList类提出的:

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim originalIntegers() As Integer = {9, 8, 7, 6, 5, 4, 3, 2, 1}

        Dim output As New List(Of Integer)
        Dim circleInts As New CircularIntegerList(originalIntegers)
        circleInts.MoveForward(4) ' move forward 4 from the 1st to get the 5th slot
        While circleInts.Count > 0
            If circleInts.Current.HasValue Then
                output.Add(circleInts.Current)
            End If
            circleInts.RemoveCurrent() ' next number in sequence becomes the current one
            circleInts.MoveForward(4) ' move forward another 4, net jump is 5 from the deleted one
        End While

        Dim result As String = String.Join(",", output.Select(Function(x) x.ToString).ToArray)
        Debug.Print(result)
    End Sub

End Class

Public Class CircularIntegerList

    Private values As New LinkedList(Of Integer)
    Private _current As LinkedListNode(Of Integer)

    Public Sub New(ByVal data() As Integer)
        If data.Length > 0 Then
            values = New LinkedList(Of Integer)(data)
            _current = values.First
        End If
    End Sub

    Public ReadOnly Property Current As Integer?
        Get
            If Not IsNothing(_current) Then
                Return _current.Value
            Else
                Return Nothing
            End If
        End Get
    End Property

    Public ReadOnly Property Count As Integer
        Get
            Return values.Count
        End Get
    End Property

    Public Sub RemoveCurrent()
        If Not IsNothing(_current) Then
            Dim tmp As LinkedListNode(Of Integer) = If(IsNothing(_current.Next), values.First, _current.Next)
            values.Remove(_current)
            If values.Count > 0 Then
                _current = tmp
            Else
                _current = Nothing
            End If
        End If
    End Sub

    Public Sub MoveForward(Optional ByVal NumberOfJumps As Integer = 1)
        If Not IsNothing(_current) Then
            For i As Integer = 1 To NumberOfJumps
                _current = If(IsNothing(_current.Next), values.First, _current.Next)
            Next
        End If
    End Sub

End Class