java冒泡排序法,选择法排序
选择法排序:把第一位和其他所有的进行比较,只要比第一位小的,就换到第一个位置来比较完后,第一位就是最小的然后再从第二位和剩余的其他所有进行比较,只要比第二位小,就换到第二个位置来比较完后,第二位就是第二小的
public class Demo02 {
public static void main(String[] args) {
int a [] = new int[]{18,62,68,82,65,9};
//排序前,先把内容打印出来
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//选择法排序
//第一步: 把第一位和其他所有位进行比较
//如果发现其他位置的数据比第一位小,就进行交换
for (int i = 1; i < a.length; i++) {
if(a[i]<a[0]){
int temp = a[0];
a[0] = a[i];
a[i] = temp;
}
}
//把内容打印出来
//可以发现,最小的一个数,到了最前面
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//第二步: 把第二位的和剩下的所有位进行比较
for (int i = 2; i < a.length; i++) {
if(a[i]<a[1]){
int temp = a[1];
a[1] = a[i];
a[i] = temp;
}
}
//把内容打印出来
//可以发现,倒数第二小的数,到了第二个位置
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//可以发现一个规律
//移动的位置是从0 逐渐增加的
//所以可以在外面套一层循环
for (int j = 0; j < a.length-1; j++) {
for (int i = j+1; i < a.length; i++) {
if(a[i]<a[j]){
int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
//把内容打印出来
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
}
}
执行结果:
18 62 68 82 65 9
9 62 68 82 65 18
9 18 68 82 65 62
9 18 62 65 68 82
``
冒泡排序法:
第一步:从第一位开始,把相邻两位进行比较如果发现前面的比后面的大,就把大的数据交换在后面,循环比较完毕后,最后一位就是最大的
第二步: 再来一次,只不过不用比较最后一位
冒泡排序法:
第一步:从第一位开始,把相邻两位进行比较如果发现前面的比后面的大,就把大的数据交换在后面,循环比较完毕后,最后一位就是最大的
第二步: 再来一次,只不过不用比较最后一位
public class Demo03 {
public static void main(String[] args) {
int a [] = new int[]{18,62,68,82,65,9};
//排序前,先把内容打印出来
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//冒泡法排序
//第一步:从第一位开始,把相邻两位进行比较
//如果发现前面的比后面的大,就把大的数据交换在后面
for (int i = 0; i < a.length-1; i++) {
if(a[i]>a[i+1]){
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
//把内容打印出来
//可以发现,最大的到了最后面
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//第二步: 再来一次,只不过不用比较最后一位
for (int i = 0; i < a.length-2; i++) {
if(a[i]>a[i+1]){
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
//把内容打印出来
//可以发现,倒数第二大的到了倒数第二个位置
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//可以发现一个规律
//后边界在收缩
//所以可以在外面套一层循环
for (int j = 0; j < a.length; j++) {
for (int i = 0; i < a.length-j-1; i++) {
if(a[i]>a[i+1]){
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
//把内容打印出来
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
}
}
执行结果:
18 62 68 82 65 9
18 62 68 65 9 82
18 62 65 9 68 82
9 18 62 65 68 82
Arrays工具类
使用sort方法:会按照自然排序的方法对数组元素进行排序,但是还提供了多个重载方法。既可以按照自然排序的方法进行排序,也可以传入比较器参数按照定制规则排序,同时还可以只支持选择排序的元素范围。
自然排序:sort (List list):根据元素的自然顺序对指定列表按升序进行排序
import java.util.Arrays;
public class Demo05 {
public static void main(String[] args) {
int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
System.out.println("排序之前 :");
System.out.println(Arrays.toString(a));
Arrays.sort(a);
System.out.println("排序之后:");
System.out.println(Arrays.toString(a));
}
}
执行结果:
排序之前 :
[18, 62, 68, 82, 65, 9]
排序之后:
[9, 18, 62, 65, 68, 82]
自定义排序:sort(List list, Comparator c):根据指定比较器产生的顺序对指定列表进行排序,也就是自定义排序,第一个参数是传入的要排序的列表,第二个参数是指定的比较器。
首先创建一个比较器,在这个比较器中定义好排序规则(升序或降序),此处为降序排序:
Comparator<Integer> c = new Comparator<Integer>(){
@Override
public int compare(Integer 01,02){
//升序 01-02
//降序02-01
return 02-01;
}
}
``