我该如何排序这个数组?请帮我! ! ! [英] How do I sort this array? Please help me! ! !

查看:83
本文介绍了我该如何排序这个数组?请帮我! ! !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.Arrays; //启动和导入数组

公共类峰值

{

public static void main(String [] args){

float [] x = {0.2f,0.5f,0.1f,0.15f,0.2f,0.13f,0.3f,0.25f,0.3f,0.3f,

0.7f ,0.2f,0.45f,0.15f,0.2f,0.85f,0.3f,0.65f,0.2f,0.1f};

//阵列列表



System.out.println(Peaks(x,2)); //明确什么是峰值和数组



}



static int峰值(浮点数) [] x,int range){//对于特殊情况

//如果有任何特殊情况,那么它将返回0

if(x == null)

{

返回0;

}



int result = 0,l ,r; //结果必须从数字零开始,

//输入是l和r



for(int i = 0; i< x.length; i ++){

boolean aPeak = true;

l = Math.max(0,i - range);

r = Math.min(x.length - 1,i + range); //排列数组

for(int j = l; j< = r; j ++){//如果我们在当前的话,请跳过

如果(i == j){

继续;

}

如果(x [i]< x [j]){

aPeak = false;

休息;

}

}



if(aPeak){

System.out.println(Peak at+ i +=+ x [一世]); //峰值和峰值数量

结果++;

i + =范围;



}

}

System.out.print(峰数:);

返回结果; //打印结果,即峰值数量

}

}



我是什么尝试过:



我试过java.util.Arrays.sort(x)

这样可行但是它没有打印未分类的。我希望它打印未排序和排序的数组。

解决方案

Quote:

这可以工作,但它不会打印未排序的。我希望它打印未排序和排序的数组。



是的,这是原则。 Sort正在替换其排序副本提供的数组。

要使数组未排序和排序,请复制并对副本进行排序。

复制数组 x 到数组 y (使其成为真正的副本)并排序数组 y

因此 x 未排序且 y已排序


排序是按一些指定标准重新排序数据的过程:人们根据高度排成一排,奶酪变成最有气味的第一,或日期按降序排列。

当我们对现实世界的对象或人物执行这些操作时,我们不保留原始订单,除非我们在开始排序操作之前特别记下它们。所以对于人们来说,在我们按高度排列之前,我们可能会说记住你左边和右边的人,所以我们可以让他们稍后恢复原来的位置。或者我们可以在开始闻到它们之前写下Cheddar,Stinking Bishop,Brie。



如果你想保留阵列的原始顺序以及排序它,你需要在开始排序之前复制数组: Java中的System.arraycopy() - GeeksforGeeks [ ^ ]可能有所帮助。

import java.util.Arrays; //to initiate and import arrays
public class Peak
{
public static void main(String[] args) {
float [] x = {0.2f, 0.5f, 0.1f, 0.15f, 0.2f, 0.13f, 0.3f, 0.25f, 0.3f, 0.3f,
0.7f, 0.2f, 0.45f, 0.15f, 0.2f, 0.85f, 0.3f, 0.65f, 0.2f, 0.1f};
//list of arrays

System.out.println(Peaks(x, 2)); //to make it clear what peak is and the arrays

}

static int Peaks(float[] x, int range) {// for special cases
//if there is any special case then it will return 0
if (x == null)
{
return 0;
}

int result = 0, l, r; // result has to start with the number zero and the
//inputs that are the l and r

for (int i = 0; i < x.length; i++) {
boolean aPeak = true;
l = Math.max(0, i - range);
r = Math.min(x.length - 1, i + range);//arrange the arrays
for (int j = l; j <= r; j++) {// Skip if we are on current
if (i == j) {
continue;
}
if (x[i] < x[j]) {
aPeak = false;
break;
}
}

if (aPeak) {
System.out.println("Peak at " + i + " = " + x[i]); //the peaks and number of the peak
result++;
i += range;

}
}
System.out.print("Number of peaks: ");
return result; //print the result that is the number of peaks
}
}

What I have tried:

I have tried java.util.Arrays.sort (x)
This works but then it doesn't print the unsorted one. I want it to print the unsorted as well as the sorted arrays.

解决方案

Quote:

This works but then it doesn't print the unsorted one. I want it to print the unsorted as well as the sorted arrays.


Yes that is the principle. Sort is replacing the array provided by its sorted copy.
To get the array unsorted and sorted, make a copy and sort the copy.
Copy array x to array y ( make it a real copy) and sort array y.
So that x is unsorted and y is sorted


Sorting is the process of reordering data by some specified criteria: people into a row based on height, cheeses into "smelliest first", or dates into descending order.
When we perform these operations on real world objects or people, we do not preserve the original order unless we specifically take a note of them before we begin the sorting operation. So with the people, we might say "remember who is to your left and your right" before we arrange them by height, so we can ask them to resume the original position later. Or we could write down "Cheddar, Stinking Bishop, Brie" before we start to smell them.

If you want to retain the original order of your array as well as sorting it, you need to copy the array before you begin sorting: System.arraycopy() in Java - GeeksforGeeks[^] may help.


这篇关于我该如何排序这个数组?请帮我! ! !的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆