「配置された位置」または配列内のパラメータの位置が記載されている場所の配列を印刷したいと思います。この位置は、配列のソートされた部分に最新の番号が挿入された場所です。これは "currPosition"と同じですが、配置されている場合は助けを得ることができます。 「i」は、配置された位置であり、「c」は、「currPosition」である。それは次のようになります。最終的に挿入された配列の位置
public static void displaySort(int[] items, int currPosition, int placedPosition) {
System.out.println("Iteration #"+currPosition);
String[][] arr = new String[currPosition][currPosition];
String i="i";
String c="c";
String ic="ic";
System.out.println(arr[placedPosition][placedPosition]=i);
System.out.println(arr[currPosition][currPosition]=c);
System.out.println(Arrays.toString(items));
}
はソート::
私は本当にこの取り掛かるするかどうかはわかりません、ここでのコードは、私がこれまで持っている
public static void insertionSort(int[] items) {
int currentPosition; // the number of items sorted so far
int currentItem; // the value being inserted
int backwardMovingIndex; // used to find where the current value gets inserted.
//displaySort(items, 0, 0); // use this call to display the list before the first iteration (iteration 0)
for (currentPosition = 1; currentPosition < items.length; currentPosition++) // Start with 1 (not 0)
{
currentItem = items[currentPosition];
for (backwardMovingIndex = currentPosition - 1; (backwardMovingIndex >= 0) && (items[backwardMovingIndex] > currentItem); backwardMovingIndex--) // Larger values are moving up
{
items[backwardMovingIndex + 1] = items[backwardMovingIndex];
}
items[backwardMovingIndex + 1] = currentItem; // Put the current value in its proper location
displaySort(items, currentPosition, backwardMovingIndex + 1); // use this call to display the list for all other iterations
}
}
をチェックする場合/他の構造に多くの枝を追加完了するために
String.format("%"+n+"d", item)
のようなものと全く
n
文字の文字列に整数に変換することができます混乱している。 私はあなたがしようとしているのは、ソートアルゴリズムの各反復で要素の配列内の位置を追跡することだと思います。まずソートアルゴリズムを表示する必要があります... – recurf