여러 개의 정렬 된 줄이있는 파일이 있습니다. 이제이 모든 줄을 새 파일의 한 병합 된 줄로 정렬하려고합니다. 한 번에 모든 숫자를로드하지 않고. .txt 파일의 Mergesort 행
12,86,280,304,350,359,371,391,405,548,
255,264,325,346,435,466,483,
39,114,214,298,317,377,428,438,575,
35,165,183,281,336,367,386,418,438,593,
44,77,97,117,122,156,251,415,533,
109,155,163,172,212,226,340,358,452,577,592,
33,74,91,204,256,307,357,388,534,552,554,570,
50,99,246,309,345,358,395,405,419,425,566,
이
내 파일의 일부이다. 그런 다음 첫 번째 요소를 모두 얻어 비교해야합니다. 가장 낮은 파일을 새로운 파일에 씁니다. 그런 다음 나는 방금 쓴 줄에서 두 번째 숫자를 얻어야합니다. 그리고 다른 라인의 첫 번째 숫자와 비교하십시오. 어떻게해야합니까? 나는 Arraylists에 대한 머지 소트를 작성했습니다 : //as long as there is unsorted data
while (listOfOutputs.size() > 0) {
//Set the lowest undefined
List<Integer> lowest = null;
for (List<Integer> list : listOfOutputs) {
//if the lowest is undefined, I'm the lowest
if (lowest == null) {
lowest = list;
//Else am I lower then the lowest? Then I'm the lowest
} else if (list.get(0) < lowest.get(0)) {
lowest = list;
}
}
//Finally the lowest is added to the sorted list and removed to from his own list.
assert lowest != null;
sortedList.add(lowest.remove(0));
//Is the size of the list which contained to lowest now 0, remove him from the listOfOutputs
if (lowest.size() == 0) listOfOutputs.remove(lowest);
}
하지만 내 파일을 정렬 하나에이를 다시 작성하는 방법을 모르겠어요. 목록에로드하지 않고 어떻게 수행합니까?
스벤
간단히 각 행을 읽고 각 행을 구문 분석하여 읽은 다음 분석 된 모든 정수를 목록에 추가 한 다음 마지막으로 전체 목록을 정렬 할 수 있습니까? – jarmod
데이터가 너무 커서 메모리에 저장할 수 있습니까? 그런 이유로 모든 데이터를 단일 배열에로드하고 정렬하지 않으려 고합니다. –