整数が昇順にソートされていると仮定すると、ソートされた配列にどのように挿入しますか?バイナリ検索を使用するように言われましたが、要素の位置だけが返されます。ソートされたリストに再帰的に挿入
擬似コードの例はgrateです。
整数が昇順にソートされていると仮定すると、ソートされた配列にどのように挿入しますか?バイナリ検索を使用するように言われましたが、要素の位置だけが返されます。ソートされたリストに再帰的に挿入
擬似コードの例はgrateです。
たとえば、静的配列を使用しているとします。何のリストリンクされていない
あなたの要件ごとにカスタマイズすることができます文字列配列で行う方法を以下に示します
//項目の順序付きリスト のString [] sortedArray =新しいString [] { "アリとanarrayを作成します。 "、"バット "、"猫 "、"犬 "};
// Search for a non-existent item and then insert it
int index = Arrays.binarySearch(sortedArray, "cow");
if (index < 0) {
// Compute the insert index
int insertIndex = -index-1;
// Insert the new item into sortedArray. The example here creates
// a new larger array to hold the new item.
String[] newSortedArray = new String[sortedArray.length+1];
System.arraycopy(sortedArray, 0, newSortedArray, 0, insertIndex);
System.arraycopy(sortedArray, insertIndex,
newSortedArray, insertIndex+1,
sortedArray.length-insertIndex);
newSortedArray[insertIndex] = "cow";
sortedArray = newSortedArray;
}
はhttp://www.exampledepot.com/egs/java.util/coll_InsertInArray.html
を参照してください、それは宿題その後、宿題のタグを追加するのですか? –