none
如何给二维数组排序? RRS feed

  • 问题

  • 最近在这个问题上郁闷了好几天,也是自己一直都没解决的问题。即如何给二维数组快速排序?
    如有一个数组:
        a(0, 0) = 7: a(0, 1) = 20
        a(1, 0) = 3: a(1, 1) = 7
        a(2, 0) = 6: a(2, 1) = 8
        a(3, 0) = 9: a(3, 1) = 7
        a(4, 0) = 1: a(4, 1) = 11
        a(5, 0) = 34: a(5, 1) = 4
        a(6, 0) = 5: a(6, 1) = 6
        a(7, 0) = 7: a(7, 1) = 34
        a(8, 0) = 23: a(8, 1) = 78
        a(9, 0) = 33: a(9, 1) = 9
        a(10, 0) = 0: a(10, 1) = 5
        a(11, 0) = 7: a(11, 1) = 23
    能通过一个函数,先将其按0列排序,然后在0列的基础上,即将0列中相同的元素在第一列排序,拍完后为:
        a(0, 0) =0: a(0, 1) = 5
        a(1, 0) = 1: a(1, 1) = 11
        a(2, 0) = 3: a(2, 1) = 7
        a(3, 0) = 5: a(3, 1) = 6
        a(4, 0) = 6: a(4, 1) = 8
        a(5, 0) =7: a(5, 1) = 20---
        a(6, 0) = 7: a(6, 1) = 23---第二次仅对0列中相同的项再排序
        a(7, 0) = 7: a(7, 1) = 34---
        a(8, 0) = 9: a(8, 1) = 7
        a(9, 0) = 23: a(9, 1) = 78
        a(10, 0) = 33: a(10, 1) = 9
        a(11, 0) = 34: a(11, 1) = 4
    我已经用冒泡法做到了,但效率低,不用Arrary.sort一类的现成函数,请教有没有什么别的好办法,如快排。谢谢
    2009年1月12日 6:24

答案

  • 实际上

     

    Arrary.sort一类的现成函数也是冒泡

    所以快不了。。。

    2009年1月12日 7:54
  • 先转成一维的,排序后再转回来

    Code Snippet

    Dim a(11, 1) As Integer
            a(0, 0) = 7 : a(0, 1) = 20
            a(1, 0) = 3 : a(1, 1) = 7
            a(2, 0) = 6 : a(2, 1) = 8
            a(3, 0) = 9 : a(3, 1) = 7
            a(4, 0) = 1 : a(4, 1) = 11
            a(5, 0) = 34 : a(5, 1) = 4
            a(6, 0) = 5 : a(6, 1) = 6
            a(7, 0) = 7 : a(7, 1) = 34
            a(8, 0) = 23 : a(8, 1) = 78
            a(9, 0) = 33 : a(9, 1) = 9
            a(10, 0) = 0 : a(10, 1) = 5
            a(11, 0) = 7 : a(11, 1) = 23

     

            Dim i As Integer
            Dim b(11) As Long
            For i = 0 To 11
                b(i) = a(i, 0) * 10000 + a(i, 1)
            Next

     

            Array.Sort(b)

     

            For i = 0 To 11
                a(i, 0) = b(i) \ 10000
                a(i, 1) = b(i) - a(i, 0) * 10000
            Next

            For i = 0 To 11
                Console.WriteLine("a({0}, 0)={1}:a({0}, 1)={2}", i, a(i, 0), a(i, 1))
            Next

     

     

     

     

    2009年1月12日 8:26
    版主

全部回复

  • 实际上

     

    Arrary.sort一类的现成函数也是冒泡

    所以快不了。。。

    2009年1月12日 7:54
  • 先转成一维的,排序后再转回来

    Code Snippet

    Dim a(11, 1) As Integer
            a(0, 0) = 7 : a(0, 1) = 20
            a(1, 0) = 3 : a(1, 1) = 7
            a(2, 0) = 6 : a(2, 1) = 8
            a(3, 0) = 9 : a(3, 1) = 7
            a(4, 0) = 1 : a(4, 1) = 11
            a(5, 0) = 34 : a(5, 1) = 4
            a(6, 0) = 5 : a(6, 1) = 6
            a(7, 0) = 7 : a(7, 1) = 34
            a(8, 0) = 23 : a(8, 1) = 78
            a(9, 0) = 33 : a(9, 1) = 9
            a(10, 0) = 0 : a(10, 1) = 5
            a(11, 0) = 7 : a(11, 1) = 23

     

            Dim i As Integer
            Dim b(11) As Long
            For i = 0 To 11
                b(i) = a(i, 0) * 10000 + a(i, 1)
            Next

     

            Array.Sort(b)

     

            For i = 0 To 11
                a(i, 0) = b(i) \ 10000
                a(i, 1) = b(i) - a(i, 0) * 10000
            Next

            For i = 0 To 11
                Console.WriteLine("a({0}, 0)={1}:a({0}, 1)={2}", i, a(i, 0), a(i, 1))
            Next

     

     

     

     

    2009年1月12日 8:26
    版主