用JAVA,以数组为一个方法的参数,在该方法内对数组进行排序,并验证结果
3个回答
展开全部
public class Test {
public static void main(String[] args) {
int[] num = { 3, 1, 4, 2, 0, 5 };
sort(num);
for (int n : num) {
System.out.println(n);
}
}
public static void sort(int[] num) {
for (int i = 0; i < num.length - 1; i++) {
for (int j = i + 1; j < num.length; j++) {
if (num[i] > num[j]) {
int temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
}
}
public static void main(String[] args) {
int[] num = { 3, 1, 4, 2, 0, 5 };
sort(num);
for (int n : num) {
System.out.println(n);
}
}
public static void sort(int[] num) {
for (int i = 0; i < num.length - 1; i++) {
for (int j = i + 1; j < num.length; j++) {
if (num[i] > num[j]) {
int temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
}
}
展开全部
public class Paixu {
public void Comp(int[] a)
{
for(int j=0;j<a.length-j;j++)
{
for(int i=0;i<a.length-1;i++)
{
if(a[i]>a[i+1])
{
int temp;
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
}
public void print(int[] a)
{
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
public static void main(String[] args)
{
int[] a = {2,3,4,1,5};
Paixu p = new Paixu();
p.Comp(a);
p.print(a);
}
}
public void Comp(int[] a)
{
for(int j=0;j<a.length-j;j++)
{
for(int i=0;i<a.length-1;i++)
{
if(a[i]>a[i+1])
{
int temp;
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
}
public void print(int[] a)
{
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
public static void main(String[] args)
{
int[] a = {2,3,4,1,5};
Paixu p = new Paixu();
p.Comp(a);
p.print(a);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询