Java 合并排序并从文件中读取数据
我的代码有什么问题?我从一个每行包含1个数字的文件中读取数据,并使用合并排序对其进行排序。我遇到以下错误:Java 合并排序并从文件中读取数据,java,file,sorting,bufferedreader,mergesort,Java,File,Sorting,Bufferedreader,Mergesort,我的代码有什么问题?我从一个每行包含1个数字的文件中读取数据,并使用合并排序对其进行排序。我遇到以下错误: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at MergeSort.merge(MergeSort.java:67) at MergeSort.sort(MergeSort.java:30) at MergeSort.main(MergeSort.j
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at MergeSort.merge(MergeSort.java:67)
at MergeSort.sort(MergeSort.java:30)
at MergeSort.main(MergeSort.java:86)
文本文件:
300
40
512
234
100
我的mergeSort实现中有什么错误吗
这是我的密码:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MergeSort
{
void sort(int[] arr, int low, int high)
{
if(high > low)
{
int mid = (low + high)/2;
sort(arr,low,mid);
sort(arr,mid+1,high);
merge(arr, low, mid+1, high);
}
}
void merge(int arr[], int low,int mid, int high)
{
int helper[] = new int[arr.length];
//copy both halves into helper
for(int i=0;i< arr.length;i++)
{
helper[i] = arr[i];
}
//compare elements from left & right, initialize variables
int helperLeft = low;
int helperRight = high;
int curr = mid+1;
//while if left<=mid && mid+1 <=right && if left < right assign lowest elem to original array
while(helperLeft <= curr && curr <=helperRight)
{
if(helperLeft <= curr)
{
arr[helperLeft] = helperLeft;
helperLeft++;
}
else
{
arr[curr] = curr;
curr++;
}
helperRight++;
}
//copy all elements to arr
int rem = mid-helperLeft;
for(int i=0;i<rem;i++)
{
arr[helperRight+i] = helper[helperLeft+i];
}
}
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
MergeSort pb = new MergeSort();
InputStream inputStream = new FileInputStream("task.txt");
@SuppressWarnings("resource")
BufferedReader R = new BufferedReader(new InputStreamReader(inputStream));
int arraySize = Integer.parseInt(R.readLine());
int[] inputArray = new int[arraySize];
for (int i = 0; i < arraySize; i++) {
inputArray[i] = Integer.parseInt(R.readLine());
}
pb.sort(inputArray, 0, inputArray.length-1);
for (int j = 0; j < inputArray.length; j++) {
System.out.println(inputArray[j]);
}
}
}
我认为问题出在while条件下
while(helperLeft <= curr && curr <=helperRight)
while(helperLeft我认为问题出在while条件下
while(helperLeft <= curr && curr <=helperRight)
while(helperLeft我认为问题出在while条件下
while(helperLeft <= curr && curr <=helperRight)
while(helperLeft我认为问题出在while条件下
while(helperLeft <= curr && curr <=helperRight)
while(helperLeft除了例外,我认为您的merge
逻辑不正确。请查看您的代码:
while(helperLeft <= curr && curr <=helperRight)
{
if(helperLeft <= curr)
{
arr[helperLeft] = helperLeft;
helperLeft++;
}
else
{
arr[curr] = curr;
curr++;
}
helperRight++;
}
while(helperLeft除了例外,我认为您的merge
逻辑不正确。请查看您的代码:
while(helperLeft <= curr && curr <=helperRight)
{
if(helperLeft <= curr)
{
arr[helperLeft] = helperLeft;
helperLeft++;
}
else
{
arr[curr] = curr;
curr++;
}
helperRight++;
}
while(helperLeft除了例外,我认为您的merge
逻辑不正确。请查看您的代码:
while(helperLeft <= curr && curr <=helperRight)
{
if(helperLeft <= curr)
{
arr[helperLeft] = helperLeft;
helperLeft++;
}
else
{
arr[curr] = curr;
curr++;
}
helperRight++;
}
while(helperLeft除了例外,我认为您的merge
逻辑不正确。请查看您的代码:
while(helperLeft <= curr && curr <=helperRight)
{
if(helperLeft <= curr)
{
arr[helperLeft] = helperLeft;
helperLeft++;
}
else
{
arr[curr] = curr;
curr++;
}
helperRight++;
}
while(helperLeft尝试以下操作:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MergeSort {
public void mergeSort(Integer[] array, int lo, int n) {
int low = lo;
int high = n;
if (low >= high) {
return;
}
int middle = (low + high) / 2;
mergeSort(array, low, middle);
mergeSort(array, middle + 1, high);
int end_low = middle;
int start_high = middle + 1;
while ((lo <= end_low) && (start_high <= high)) {
if (array[low] < array[start_high]) {
low++;
} else {
int Temp = array[start_high];
for (int k = start_high - 1; k >= low; k--) {
array[k + 1] = array[k];
}
array[low] = Temp;
low++;
end_low++;
start_high++;
}
}
}
public static void main(String[] args) throws NumberFormatException, IOException {
MergeSort pb = new MergeSort();
try {
BufferedReader br = new BufferedReader(new FileReader("E:\\task.txt"));
List<Integer> lines = new ArrayList<Integer>();
String line;
while ((line = br.readLine()) != null) {
lines.add(Integer.parseInt(line));
}
br.close();
Integer[] inputArray = lines.toArray(new Integer[lines.size()]);
pb.mergeSort(inputArray, 0, inputArray.length - 1);
for (Integer i : inputArray) {
System.out.println(i);
}
} catch (IOException ie) {
System.out.print(ie.getMessage());
}
}
}
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
公共类合并排序{
公共void合并排序(整数[]数组,整数lo,整数n){
int低=低;
int高=n;
如果(低>=高){
返回;
}
int-middle=(低+高)/2;
合并排序(数组、低、中);
合并排序(数组,中间+1,高);
int end_低=中;
int start_high=中间+1;
而((lo试试这个:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MergeSort {
public void mergeSort(Integer[] array, int lo, int n) {
int low = lo;
int high = n;
if (low >= high) {
return;
}
int middle = (low + high) / 2;
mergeSort(array, low, middle);
mergeSort(array, middle + 1, high);
int end_low = middle;
int start_high = middle + 1;
while ((lo <= end_low) && (start_high <= high)) {
if (array[low] < array[start_high]) {
low++;
} else {
int Temp = array[start_high];
for (int k = start_high - 1; k >= low; k--) {
array[k + 1] = array[k];
}
array[low] = Temp;
low++;
end_low++;
start_high++;
}
}
}
public static void main(String[] args) throws NumberFormatException, IOException {
MergeSort pb = new MergeSort();
try {
BufferedReader br = new BufferedReader(new FileReader("E:\\task.txt"));
List<Integer> lines = new ArrayList<Integer>();
String line;
while ((line = br.readLine()) != null) {
lines.add(Integer.parseInt(line));
}
br.close();
Integer[] inputArray = lines.toArray(new Integer[lines.size()]);
pb.mergeSort(inputArray, 0, inputArray.length - 1);
for (Integer i : inputArray) {
System.out.println(i);
}
} catch (IOException ie) {
System.out.print(ie.getMessage());
}
}
}
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
公共类合并排序{
公共void合并排序(整数[]数组,整数lo,整数n){
int低=低;
int高=n;
如果(低>=高){
返回;
}
int-middle=(低+高)/2;
合并排序(数组、低、中);
合并排序(数组,中间+1,高);
int end_低=中;
int start_high=中间+1;
而((lo试试这个:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MergeSort {
public void mergeSort(Integer[] array, int lo, int n) {
int low = lo;
int high = n;
if (low >= high) {
return;
}
int middle = (low + high) / 2;
mergeSort(array, low, middle);
mergeSort(array, middle + 1, high);
int end_low = middle;
int start_high = middle + 1;
while ((lo <= end_low) && (start_high <= high)) {
if (array[low] < array[start_high]) {
low++;
} else {
int Temp = array[start_high];
for (int k = start_high - 1; k >= low; k--) {
array[k + 1] = array[k];
}
array[low] = Temp;
low++;
end_low++;
start_high++;
}
}
}
public static void main(String[] args) throws NumberFormatException, IOException {
MergeSort pb = new MergeSort();
try {
BufferedReader br = new BufferedReader(new FileReader("E:\\task.txt"));
List<Integer> lines = new ArrayList<Integer>();
String line;
while ((line = br.readLine()) != null) {
lines.add(Integer.parseInt(line));
}
br.close();
Integer[] inputArray = lines.toArray(new Integer[lines.size()]);
pb.mergeSort(inputArray, 0, inputArray.length - 1);
for (Integer i : inputArray) {
System.out.println(i);
}
} catch (IOException ie) {
System.out.print(ie.getMessage());
}
}
}
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
公共类合并排序{
公共void合并排序(整数[]数组,整数lo,整数n){
int低=低;
int高=n;
如果(低>=高){
返回;
}
int-middle=(低+高)/2;
合并排序(数组、低、中);
合并排序(数组,中间+1,高);
int end_低=中;
int start_high=中间+1;
而((lo试试这个:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MergeSort {
public void mergeSort(Integer[] array, int lo, int n) {
int low = lo;
int high = n;
if (low >= high) {
return;
}
int middle = (low + high) / 2;
mergeSort(array, low, middle);
mergeSort(array, middle + 1, high);
int end_low = middle;
int start_high = middle + 1;
while ((lo <= end_low) && (start_high <= high)) {
if (array[low] < array[start_high]) {
low++;
} else {
int Temp = array[start_high];
for (int k = start_high - 1; k >= low; k--) {
array[k + 1] = array[k];
}
array[low] = Temp;
low++;
end_low++;
start_high++;
}
}
}
public static void main(String[] args) throws NumberFormatException, IOException {
MergeSort pb = new MergeSort();
try {
BufferedReader br = new BufferedReader(new FileReader("E:\\task.txt"));
List<Integer> lines = new ArrayList<Integer>();
String line;
while ((line = br.readLine()) != null) {
lines.add(Integer.parseInt(line));
}
br.close();
Integer[] inputArray = lines.toArray(new Integer[lines.size()]);
pb.mergeSort(inputArray, 0, inputArray.length - 1);
for (Integer i : inputArray) {
System.out.println(i);
}
} catch (IOException ie) {
System.out.print(ie.getMessage());
}
}
}
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
公共类合并排序{
公共void合并排序(整数[]数组,整数lo,整数n){
int低=低;
int高=n;
如果(低>=高){
返回;
}
int-middle=(低+高)/2;
合并排序(数组、低、中);
合并排序(数组,中间+1,高);
int end_低=中;
int start_high=中间+1;
while((lo)哪一行是错误在?@Ra1nWarden检查我的编辑请哪一行是MergeSort.merge(MergeSort.java:67)是指?您的文本文件包含什么内容?您可以将该内容发布到哪一行是错误在?@Ra1nWarden检查我的编辑请哪一行是MergeSort.merge(MergeSort.java:67)参考?您的文本文件包含哪些内容?您可以将该错误发布到哪一行?@Ra1nWarden请检查我的编辑MergeSort.merge(MergeSort.java:67)参考?您的文本文件包含哪些内容?您可以将该错误发布到哪一行?@Ra1nWarden请检查我的编辑MergeSort.merge(MergeSort.java:67)参考?您的文本文件包含哪些内容?您是否可以将其发布到现在它不再是超出范围的数组索引,而是numberformatexception@fscore好的…你有调试器对吗?你有stacktrace对吗?然后修复我的合并逻辑在我使用数组而不是文件读取器对其进行排序时工作正常,所以我需要帮助现在它不再是Array索引超出范围,但改为numberformatexception@fscore好的…你有调试器对吗?你有stacktrace对吗?然后修复我的合并逻辑在我使用数组而不是文件读取器对它进行排序时工作正常,所以我需要帮助现在它不再是数组索引超出范围,而是numberformatexception@fscore好吧,你有黛布ugger对吗?你有stacktrace对吗?然后修复我的合并逻辑在使用数组而不是文件读取器对其进行排序时工作正常,因此我需要帮助现在不再是数组索引超出范围,而是数字格式exception@fscore好的…你有调试器对吗?你有stacktrace对吗?然后修复它我的合并逻辑在我使用数组并尝试对其进行排序,而不是使用文件读取器,因此我需要这方面的帮助