如何找到数组对象的中位数?
How to find the median of an array object?
现在正在编程 class,我感到非常困惑。我们基本上需要 声明一个中值方法,该方法将找到数组对象 中包含的值的中值,但我不知道如何在这种情况下操作它。此外,我不知道如何分离数组的各个部分或如何获取数组的特定 "middle chunks",有点像合并排序,但我们甚至还没有接近它。
整个星期我都在为这个问题苦苦挣扎。这是我的所有代码。 任何提示或提示都会很棒。谢谢
class ArrayIns {
private long[] a;
private int nElems; // number of data items
public ArrayIns(int max) { // constructor
a = new long[max]; // create array
nElems = 0; // no items yet
}
public void insert(long value) {
a[nElems] = value;
nElems++;
}
public void display() {
for(int j=0; j<nElems; j++)
System.out.print(a[j] + " ");
System.out.println("");
}
public void insertionSort() {
int in, out;
for(out=1; out<nElems; out++) { // out is dividing the line
long temp = a[out]; // remove marked item
in = out; // start shifts at our
while(in>0 && a[in-1] >= temp) { // until one is smaller,
a[in] = a[in-1]; // shift item to right
--in; // go left one position
}
a[in] = temp; // insert marked item
} // end of for statement
} // end of insertion sort
} // end of ArrayIns
class InsertSortApp {
public static void main(String[] args) {
int maxSize = 100;
ArrayIns arr;
arr = new ArrayIns(maxSize);
arr.insert(77); // insert 10 items
arr.insert(99); // 10 is also even :)
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display();
arr.insertionSort();
arr.display();
} // end of main()
} // end of InsertSortApp class
方法中位数;
- 以数组作为输入
- sort content of array
- find length of array
- find the index of middle
- return 中间索引数组的值
首先,您是否测试过您的排序算法是否有效?它是否正确排序数组?
如果排序算法正常工作,那么获取中位数就很简单了。首先,判断它的元素个数是奇数还是偶数。如果它有奇数个元素,则中位数是长度为 / 2 的元素。如果它有偶数个元素,则中位数是长度为 / 2 - 1 和长度为 / 2 的元素的平均值。
将以下方法添加到 ArrayIns
public long median() {
if (nElems % 2 == 0) {
int index1 = nElems/2-1;
return (a[index1]+a[index1+1]) / 2;
}
return a[nElems/2];
}
排序后从 main() 调用:
long median = arr.median();
现在正在编程 class,我感到非常困惑。我们基本上需要 声明一个中值方法,该方法将找到数组对象 中包含的值的中值,但我不知道如何在这种情况下操作它。此外,我不知道如何分离数组的各个部分或如何获取数组的特定 "middle chunks",有点像合并排序,但我们甚至还没有接近它。
整个星期我都在为这个问题苦苦挣扎。这是我的所有代码。 任何提示或提示都会很棒。谢谢
class ArrayIns {
private long[] a;
private int nElems; // number of data items
public ArrayIns(int max) { // constructor
a = new long[max]; // create array
nElems = 0; // no items yet
}
public void insert(long value) {
a[nElems] = value;
nElems++;
}
public void display() {
for(int j=0; j<nElems; j++)
System.out.print(a[j] + " ");
System.out.println("");
}
public void insertionSort() {
int in, out;
for(out=1; out<nElems; out++) { // out is dividing the line
long temp = a[out]; // remove marked item
in = out; // start shifts at our
while(in>0 && a[in-1] >= temp) { // until one is smaller,
a[in] = a[in-1]; // shift item to right
--in; // go left one position
}
a[in] = temp; // insert marked item
} // end of for statement
} // end of insertion sort
} // end of ArrayIns
class InsertSortApp {
public static void main(String[] args) {
int maxSize = 100;
ArrayIns arr;
arr = new ArrayIns(maxSize);
arr.insert(77); // insert 10 items
arr.insert(99); // 10 is also even :)
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display();
arr.insertionSort();
arr.display();
} // end of main()
} // end of InsertSortApp class
方法中位数;
- 以数组作为输入
- sort content of array
- find length of array
- find the index of middle
- return 中间索引数组的值
首先,您是否测试过您的排序算法是否有效?它是否正确排序数组?
如果排序算法正常工作,那么获取中位数就很简单了。首先,判断它的元素个数是奇数还是偶数。如果它有奇数个元素,则中位数是长度为 / 2 的元素。如果它有偶数个元素,则中位数是长度为 / 2 - 1 和长度为 / 2 的元素的平均值。
将以下方法添加到 ArrayIns
public long median() {
if (nElems % 2 == 0) {
int index1 = nElems/2-1;
return (a[index1]+a[index1+1]) / 2;
}
return a[nElems/2];
}
排序后从 main() 调用:
long median = arr.median();