int pivotpos = low;
int Vector[] = null; for (int i = 0; i <= dataList.length; i++) {
Vector[i] = dataList[i];
}
#5
不好意思,看错了,楼主贴一下你的具体的错误信息,帮助分析
#6
Vector[] 木有new
#7
Exception in thread "main" java.lang.NullPointerException
at org.demo.QuickSort.partition(QuickSort.java:21)
at org.demo.QuickSort.quickSort(QuickSort.java:8)
at org.demo.QuickSort.main(QuickSort.java:49)
// private int dataList[] = {5,7,100,-2,0,10,121};
void quickSort(int dataList[], int left, int right) {
if (left < right) {
int pivotpos = partition(dataList, left, right);
quickSort(dataList, left, pivotpos - 1);
quickSort(dataList, pivotpos + 1, right);
}
//for (int i = 0; i <= dataList.length; i++) {//这里不能是 <= 会造成数组的越界,应该是 <
for (int i = 0; i < dataList.length; i++) {
System.out.println(dataList[i] + " ");
}
}
int partition(int dataList[], int low, int high) {
int pivotpos = low;
//int Vector[] = null;//你这里给int数组赋予的是空值,当然在后面的调用的时候会出错了。
int vector[] = new int[dataList.length];;
//for (int i = 0; i <= dataList.length; i++) {//这里不能是 <= 会造成数组的越界,应该是 <
for (int i = 0; i < dataList.length; i++) {
vector[i] = dataList[i];
}
int pivot = vector[low];
for (int i = low + 1; i <= high; i++) {
if (vector[i] < pivot) {
pivotpos++;
if (pivotpos != i)
swap(vector[pivotpos], vector[i]);
Exception in thread "main" java.lang.NullPointerException
at org.demo.QuickSort.partition(QuickSort.java:21)
at org.demo.QuickSort.quickSort(QuickSort.java:8)
at org.demo.QuickSort.main(QuickSort.java:49)
// private int dataList[] = {5,7,100,-2,0,10,121};
void quickSort(int dataList[], int left, int right) {
if (left < right) {
int pivotpos = partition(dataList, left, right);
quickSort(dataList, left, pivotpos - 1);
quickSort(dataList, pivotpos + 1, right);
}
//for (int i = 0; i <= dataList.length; i++) {//这里不能是 <= 会造成数组的越界,应该是 <
for (int i = 0; i < dataList.length; i++) {
System.out.println(dataList[i] + " ");
}
}
int partition(int dataList[], int low, int high) {
int pivotpos = low;
//int Vector[] = null;//你这里给int数组赋予的是空值,当然在后面的调用的时候会出错了。
int vector[] = new int[dataList.length];;
//for (int i = 0; i <= dataList.length; i++) {//这里不能是 <= 会造成数组的越界,应该是 <
for (int i = 0; i < dataList.length; i++) {
vector[i] = dataList[i];
}
int pivot = vector[low];
for (int i = low + 1; i <= high; i++) {
if (vector[i] < pivot) {
pivotpos++;
if (pivotpos != i)
swap(vector[pivotpos], vector[i]);
Exception in thread "main" java.lang.NullPointerException
at org.demo.QuickSort.partition(QuickSort.java:21)
at org.demo.QuickSort.quickSort(QuickSort.java:8)
at org.demo.QuickSort.main(QuickSort.java:49)
// private int dataList[] = {5,7,100,-2,0,10,121};
void quickSort(int dataList[], int left, int right) {
if (left < right) {
int pivotpos = partition(dataList, left, right);
quickSort(dataList, left, pivotpos - 1);
quickSort(dataList, pivotpos + 1, right);
}
//for (int i = 0; i <= dataList.length; i++) {//这里不能是 <= 会造成数组的越界,应该是 <
for (int i = 0; i < dataList.length; i++) {
System.out.println(dataList[i] + " ");
}
}
int partition(int dataList[], int low, int high) {
int pivotpos = low;
//int Vector[] = null;//你这里给int数组赋予的是空值,当然在后面的调用的时候会出错了。
int vector[] = new int[dataList.length];;
//for (int i = 0; i <= dataList.length; i++) {//这里不能是 <= 会造成数组的越界,应该是 <
for (int i = 0; i < dataList.length; i++) {
vector[i] = dataList[i];
}
int pivot = vector[low];
for (int i = low + 1; i <= high; i++) {
if (vector[i] < pivot) {
pivotpos++;
if (pivotpos != i)
swap(vector[pivotpos], vector[i]);
// private int dataList[] = {5,7,100,-2,0,10,121};
void quickSort(int dataList[], int left, int right) {
if (left < right) {
int pivotpos = partition(dataList, left, right);
quickSort(dataList, left, pivotpos - 1);
quickSort(dataList, pivotpos + 1, right);
}
//for (int i = 0; i <= dataList.length; i++) {//这里不能是 <= 会造成数组的越界,应该是 <
for (int i = 0; i < dataList.length; i++) {
System.out.println(dataList[i] + " ");
}
}
int partition(int dataList[], int low, int high) {
int pivotpos = low;
//int Vector[] = null;//你这里给int数组赋予的是空值,当然在后面的调用的时候会出错了。
int vector[] = new int[dataList.length];;
//for (int i = 0; i <= dataList.length; i++) {//这里不能是 <= 会造成数组的越界,应该是 <
for (int i = 0; i < dataList.length; i++) {
vector[i] = dataList[i];
}
int pivot = vector[low];
for (int i = low + 1; i <= high; i++) {
if (vector[i] < pivot) {
pivotpos++;
if (pivotpos != i)
swap(vector[pivotpos], vector[i]);