log 最小 比较 tostring [] 重复 name 10月10 ++
一、冒泡排序算法的运作如下:
比较相邻的元素。如果第一个比第二个大,就交换他们两个。
对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。
针对所有的元素重复以上的步骤,除了最后一个。
持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较
二、选择排序:
选择排序是从冒泡排序演化而来的,每一轮比较得出最小的那个值,然后依次和每轮“无序区”中参与比较的第一个值进行交换。
具体代码:
- package com.study.sort;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Random;
- /**
- * @ClassName: MaoPao
- * @date 2017年10月10日 下午5:15:21
- */
- public class MaoPao {
- public static void main(String[] args) {
- int[] arr = new int[20];
- Random rd = new Random();
- for (int i = 0; i < 20; i++) {
- int a = rd.nextInt(101); //100以内的随机整数
- arr[i] = a;
- // System.out.println(a);
- }
- //[19, 84, 47, 69, 28, 20, 79, 53, 17, 67, 96, 39, 49, 66, 1, 8, 92, 27, 63, 94]
- System.out.println(Arrays.toString(arr));
- // mao(arr);
- xuan(arr);
- System.out.println(Arrays.toString(arr));
- }
三、冒泡排序
具体代码:
- lic static int[] mao(int[] arr) {
- for (int i = 0; i < arr.length - 1; i++) {
- for (int j = 0; j < arr.length - 1 - i; j++) {
- if (arr[j] > arr[j + 1]) {
- int temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- }
- }
- }
- return arr;
- }
- //选择排序
- public static int[] xuan(int[] arr) {
- for (int i = 0; i < arr.length; i++) {
- int min = i;
- for (int j = i + 1; j < arr.length; j++) {
- if (arr[min] > arr[j]) {
- min = j;
- }
- }
- if (min != i) {
- int temp = arr[min];
- arr[min] = arr[i];
- arr[i] = temp;
- }
- }
- return arr;
- }
- }
参照图解分析:
Java数组排序
log 最小 比较 tostring [] 重复 name 10月10 ++
原文:http://www.cnblogs.com/zengsiqi/p/7991908.html
来源: http://www.bubuko.com/infodetail-2418989.html