在我的数组中找到中位数,同时将数字从最小到最大排序
Find the median in my array while also sorting the numbers from smallest to largest
这是我目前所做的;我能够计算平均值,但我不确定如何找到中位数。我也知道我需要对数组进行排序以使其更容易。
public class SAA {
public static void main(String[] args) {
int[] num = {60, 70, 82, 1216, 57, 82, 34, 560, 91, 86};
int total = 0;
for (int i = 0; i < num.length; i++) {
if ((num[i] > 0) && (num[i] < 100)) {
total += num[i];
}
}
System.out.println(total / 10);
}
}
这是我使用冒泡排序的尝试:
public class Bubblesort {
public static void main(String[] args) {
int[] num = {60, 70, 82, 1216, 57, 82, 34, 560, 91, 86};
int temp = 0;
int[] add = new int[num.length + 1];
for (int i = 0; i < num.length; i++) {
for (int j = i + 1; j < num.length; j++) {
if (num[i] > num[j]) {
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
for (int i = 0; i < num.length; i++) {
System.out.print(num[i] + " ");
}
}
}
如果数组大小为奇数,则中位数为排序数组的中间元素,否则为中间两个元素的平均值。以下代码片段查找数组的中位数。
public static void main(String[] args) {
int[]num = {60, 70, 82, 1216, 57, 82, 34, 560, 91, 86};
int median;
int len = num.length;
Arrays.sort(num); // sorts the array
//check if the length is odd
if(len%2 != 0)
median = num[len/2];
else // length is even
median = (num[(len - 1) / 2] + num[len / 2])/2;
System.out.println(median);
}
int min = 0;
int max = 0;
for (int i = 0; i < numbers.length; i++)
{
for (int j = 0; j < numbers.length-1; j++)
{
min = Math.min(numbers[j], numbers[j+1]); //using min and max methods
max = Math.max(numbers[j], numbers[j+1]);
numbers[j] = min; //placing the smaller value on the left
numbers[j+1] = max; //placing the larger value on the right
}
}
这是一个以简短的方式对数组进行冒泡排序的程序,您也应该将它与@Sr_33的答案一起使用!
这是我目前所做的;我能够计算平均值,但我不确定如何找到中位数。我也知道我需要对数组进行排序以使其更容易。
public class SAA {
public static void main(String[] args) {
int[] num = {60, 70, 82, 1216, 57, 82, 34, 560, 91, 86};
int total = 0;
for (int i = 0; i < num.length; i++) {
if ((num[i] > 0) && (num[i] < 100)) {
total += num[i];
}
}
System.out.println(total / 10);
}
}
这是我使用冒泡排序的尝试:
public class Bubblesort {
public static void main(String[] args) {
int[] num = {60, 70, 82, 1216, 57, 82, 34, 560, 91, 86};
int temp = 0;
int[] add = new int[num.length + 1];
for (int i = 0; i < num.length; i++) {
for (int j = i + 1; j < num.length; j++) {
if (num[i] > num[j]) {
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
for (int i = 0; i < num.length; i++) {
System.out.print(num[i] + " ");
}
}
}
如果数组大小为奇数,则中位数为排序数组的中间元素,否则为中间两个元素的平均值。以下代码片段查找数组的中位数。
public static void main(String[] args) {
int[]num = {60, 70, 82, 1216, 57, 82, 34, 560, 91, 86};
int median;
int len = num.length;
Arrays.sort(num); // sorts the array
//check if the length is odd
if(len%2 != 0)
median = num[len/2];
else // length is even
median = (num[(len - 1) / 2] + num[len / 2])/2;
System.out.println(median);
}
int min = 0;
int max = 0;
for (int i = 0; i < numbers.length; i++)
{
for (int j = 0; j < numbers.length-1; j++)
{
min = Math.min(numbers[j], numbers[j+1]); //using min and max methods
max = Math.max(numbers[j], numbers[j+1]);
numbers[j] = min; //placing the smaller value on the left
numbers[j+1] = max; //placing the larger value on the right
}
}
这是一个以简短的方式对数组进行冒泡排序的程序,您也应该将它与@Sr_33的答案一起使用!