정수가 오름차순으로 정렬되었다고 가정하고 정렬 된 배열에 삽입하는 방법은 무엇입니까? 나는 바이너리 검색을 사용하라는 지시를 받았지만 엘리먼트의 위치 만 반환했다.정렬 된 목록에 재귀 적으로 삽입
의사 코드의 예제는 grate가됩니다.
정수가 오름차순으로 정렬되었다고 가정하고 정렬 된 배열에 삽입하는 방법은 무엇입니까? 나는 바이너리 검색을 사용하라는 지시를 받았지만 엘리먼트의 위치 만 반환했다.정렬 된 목록에 재귀 적으로 삽입
의사 코드의 예제는 grate가됩니다.
예를 들어 정적 배열을 사용한다고 가정합니다. 어떤 목록에 연결하지
당신이 당신의 요구 사항에 따라 사용자 정의 할 수 있습니다 문자열 배열 할 수있는 방법입니다 다음
// 항목의 정렬 된 목록 문자열 [] 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를 참조? –