package com.ykk.trade.service.controller;
/**
* Created by Administrator on 2017/7/3.
*/
public class Sort {
public static void main(String[] args) {
int[] arr = {72, 6, 57, 88, 60, 42, 83, 73, 48, 85};
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
selectSort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
}
//一句话总结:将无序的一个值插入到已经有序的一个数组
private static void insertSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int t = arr[i];
int j;
for (j = i - 1; j >= 0; j--) {
if (arr[j] > t) {
arr[j + 1] = arr[j];
} else {
break;
}
}
arr[j + 1] = t;
}
}
//一句话总结:两两比较将较大的“冒泡”到两端
private static void bubbleSort(int[] arr) {
for (int i = arr.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
//一句话总结:选出剩余无序中最大的放到最“右边”
private static void selectSort(int[] arr) {
for (int i = arr.length - 1; i > 0; i--) {
int max = arr[i];
int max_i = i;
boolean found = false;
for (int j = 0; j < i; j++) {
if (arr[j] > max) {
max = arr[j];
max_i = j;
found = true;
}
}
if (found) {
int temp = arr[max_i];
arr[max_i] = arr[i];
arr[i] = temp;
}
}
}
}