字符多维数组在Java中排序 [英] char multidimensional array sorting in java

查看:138
本文介绍了字符多维数组在Java中排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的教授已要求按列进行排序二维字符数组所以连接的阵列,他希望排序是

my professor has asked to sort a 2d char array by column so the attached array he wants sorted is

  unsorted          sorted last column
Lcekoeddhoffbmg     Balgfcaelhfkgeb
Lkcmggjcdhhglif     Kmlhmhcddfoeilc
Cgldjhcekjigcdd     Cgldjhcekjigcdd
Cgldjhcekjigcdn     Lkcmggjcdhhglif
Bffmdbkcenlafjk     Lcekoeddhoffbmg
Fggdijijegfblln     Jjlncnimjldfedj
Jjlncnimjldfedj     Bffmdbkcenlafjk
Amliglfohajcdmm     Amliglfohajcdmm
Balgfcaelhfkgeb     Fggdijijegfblln
Kmlhmhcddfoeilc     Cgldjhcekjigcdn

但美中不足的是,他希望整个行是相同的字符,所以如果我们正在排序最后一列我们只是移动的行向上或向​​下相应。我对如何,甚至得到这个启动的任何帮助将非常AP preciated不知道

but the catch is that he wants the entire row to be the same characters so if we are sorting the last column we would just move the row up or down accordingly. i have no idea on how to even get this started any help would be much appreciated

推荐答案

假设你知道如何排序一维数组(否则看它),这是相当类似的。

Assuming you know how to sort a 1d array (otherwise look it up), this is fairly similar.

相反换两个字符(当你使用冒泡排序或基于交换项目的任何其他排序算法),你交换两个完整的行。
所以,你得到这样的事情(为冒泡排序):

Instead of swapping two chars (when you use bubble sort or any other sorting algorithm based on swapping items), you swap the two complete rows. So you get something like this (for bubble sort):

for char1 of each_last_row_char
  for char2 of each_last_row_char_after_char1
    if char2 < char2 then
      swap rows of char1 and char 2
    end
  end
end

交换完整的行并不难也。你迭代项的行中的量(假设他们有字符的相同量),和交换两行的项目:

Swapping complete rows is not that difficult as well. You iterate over the amount of items in the rows (assuming they have the same amount of chars), and swap the items of both rows:

for index of row_items
  tmp = row1[index]
  row1[index] = row2[index]
  row2[index] = tmp
end

就像一个普通的交换实现,但所有项目。

Just like a regular swap implementation, but then for all items.

这篇关于字符多维数组在Java中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆