数组:随机数、遍历、反转、冒泡排序、选择排序

数组:随机数、遍历、反转、冒泡排序、选择排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
public  class  Arr
{
public static void main(String[] args)
{
int b[][]=new int [5][5];
int []a = new int[10];
a[0]=(int)(Math.random()*100);//Math.random 生成0-1之间的随机数
a[1]=(int)(Math.random()*100);
a[2]=(int)(Math.random()*100);
a[3]=(int)(Math.random()*100);
a[4]=(int)(Math.random()*100);
a[5]=(int)(Math.random()*100);
a[6]=(int)(Math.random()*100);
a[7]=(int)(Math.random()*100);
a[8]=(int)(Math.random()*100);
a[9]=(int)(Math.random()*100);
System.out.print("随机数");
outPut(a);
max(a);
min(a);
reverse(a);
System.out.print("反转数组");
outPut(a);
selectSort(a);
System.out.print("选择排序后");
outPut(a);
bubbleSort(a);
System.out.print("冒泡排序后");
outPut(a);
init(b);
outPut(b);
max(b);
}
public static void init(int [][]b)
{
for(int i=0;i<b.length;i++)
for(int j=0;j<b[i].length;j++)
{
b[i][j]=(int)(Math.random()*100);

}
}
public static void max(int [][]a)
{ int max=0;
for(int i=0;i<a.length;i++)
for(int j=0;j<a[i].length;j++)
{
if(max<a[i][j])
max=a[i][j];
}
System.out.println("最大值:"+max);
}
public static void outPut(int [][]a)//二位数组遍历
{
for(int i=0;i<a.length;i++)
{for(int j=0;j<a[i].length;j++)
{
System.out.print(a[i][j]+" ");

}
System.out.println();
}
}

public static void bubbleSort(int []a)
{
int t=a[0];
for(int i=0;i<a.length-1;i++)
for(int j=0;j<a.length-i-1;j++)
{
if (a[j]<a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;

}

}


}
public static void selectSort(int []a)
{
int t=0;
for(int i=0;i<a.length-1;i++)
for(int j=a.length-1;j>i;j--)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;

}
}


}
public static void reverse(int []a)
{
for(int i=0;i<a.length/2;i++)//数组反转
{ int t=a[i];
a[i]=a[a.length-1-i];
a[a.length-1-i]=t;

}
}
public static void outPut(int []a)
{
System.out.print("遍历:");
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
/*
System.out.print("增强型for遍历:");
for(int each:a)
{
System.out.print(each+" ");
}
*/

}
public static void max(int []a)//求最大值函数
{
int max=a[0];
for(int j=0;j<a.length;j++)
{
if(a[j]>max)

{
max=a[j];
}
}
System.out.println("最大值是:"+max);
}

public static void min(int []a)//求最小值函数
{
int min=a[0];
for(int j=0;j<a.length;j++)
{
if(a[j]<min)

{
min=a[j];
}
}
System.out.println("最小值是:"+min);
}


}

运行图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值