Sort 2D array using insertion sort

时间:2016-02-03 04:06:08

标签: java sorting multidimensional-array insertion-sort

I want to sort a 2D array of integers by a certain column using insertion sort. The following code works for 1D array.

private static void InsertionSort(int[] a, int n) {

    int key, j;
    for (int i = 1; i < n; i++){
            key = a[i];
            j = i - 1;
            while ((j >= 0) && (a[j] > key)){
                a[j+1] = a[j];
                j = j - 1;
            }
            a[j+1] = key;
    }
}

For 2D array, I specify an integer c for the column by which to sort the array. For example, if I sort by the first column,

{4, 1, 3},
{6, 0, 2},
{5, 9, 8}

becomes

{4, 1, 3},
{5, 9, 8},
{6, 0, 2}

This is what I got so far for sorting a 2D array by a specified column

private static void InsertionSort(int[][] a, int n, int c) {

    in key, j;
    for (int i = 1; i < n; i++){
        key = a[i][c];
        j = i - 1;
        while ((j >= 0) && (a[j][c] > key)){
            a[j+1][c] = a[j][c];
            j = j - 1;
        }
        a[j+1][c] = key;
    }
}

but the result for sorting by the first column is

{4, 1, 3}
{5, 0, 2}
{6, 9, 8}

It sorts the elements of the first column without keeping them together with their respective rows. How can I solve this?

2 个答案:

答案 0 :(得分:0)

您需要交换数据行,而不仅仅是数据元素。

var width = 360;
var height = 360;
var radius = Math.min(width, height) / 2;

var color = d3.scale.category20b();

var svg = d3.select('#chart')
  .append('svg')
  .attr('width', width)
  .attr('height', height)
  .append('g')
  .attr('transform', 'translate(' + (width / 2) +
    ',' + (height / 2) + ')');

var arc = d3.svg.arc()
  .outerRadius(radius);

var pie = d3.layout.pie()
  .value(function(d) {
    return d.Value;
  })
  .sort(null);

var dataset = {
  "data": [{
    "_id": {
      "$oid": "56afcea3243c48393e5b665f"
    },
    "idDatasource": {
      "$oid": "56afce8f243c48393e5b665a"
    },
    "Id": 5,
    "Value": 10,
    "Name": "Brock"
  }, {
    "_id": {
      "$oid": "56afcea3243c48393e5b665d"
    },
    "idDatasource": {
      "$oid": "56afce8f243c48393e5b665a"
    },
    "Id": 3,
    "Value": 5,
    "Name": "Peter"
  }, {
    "_id": {
      "$oid": "56afcea3243c48393e5b665e"
    },
    "idDatasource": {
      "$oid": "56afce8f243c48393e5b665a"
    },
    "Id": 4,
    "Value": 8,
    "Name": "John"
  }, {
    "_id": {
      "$oid": "56afcea3243c48393e5b665b"
    },
    "idDatasource": {
      "$oid": "56afce8f243c48393e5b665a"
    },
    "Id": 1,
    "Value": 8,
    "Name": "Ash"
  }, {
    "_id": {
      "$oid": "56afcea3243c48393e5b665c"
    },
    "idDatasource": {
      "$oid": "56afce8f243c48393e5b665a"
    },
    "Id": 2,
    "Value": 20,
    "Name": "Sarah"
  }]
};

var path = svg.selectAll('path')
  .data(pie(dataset.data))
  .enter()
  .append('path')
  .attr('d', arc)
  .attr('fill', function(d, i) {
    return color(d.data.Name);
  });

答案 1 :(得分:0)

从Java 8开始,您可以在一行中完成。

Arrays.sort(data, (a, b) -> a[COL] - b[COL]);

其中COL是要排序的列。