Java排序
一、选择排序
/**
* <p>Title: SelectionSort</p>
* <p>Description: 选择排序</p>
*/
public class SelectionSort {
/**
* 主方法
* <p>Title: main</p>
* <p>Description: 主方法</p>
* @param args
*/
public static void main(String[] args) {
int[] array = newArray();
printArray("排序前:", array);
sortn(array);
printArray("排序后:", array);
}
/**
* 创建一个数组
* <p>Title: newArray</p>
* <p>Description: 创建一个数组</p>
* @return
*/
private static int[] newArray() {
int[] array = new int[] {5, 7, 1, 3, 9, 2 ,8, 4, 6, 10};
return array;
}
/**
* 排序
* <p>Title: sort</p>
* <p>Description: 排序</p>
* @param array
*/
private static void sort1(int[] array) {
for (int x = 0; x < array.length - 1; x++) {
for (int y = x + 1; y < array.length; y++) {
if (array[x] > array[y]) {
int temp = array[x];
array[x] = array[y];
array[y] = temp;
}
}
}
}
/**
* 排序
* <p>Title: sort</p>
* <p>Description: 排序</p>
* @param array
*/
private static void sort2(int[] array) {
for (int x = array.length - 1; x > 0; x--) {
for (int y = 0; y < x; y++) {
if (array[y] > array[y + 1]) {
int temp = array[y];
array[y] = array[y + 1];
array[y + 1] = temp;
}
}
}
}
/**
* 排序
* <p>Title: sort</p>
* <p>Description: 排序</p>
* @param array
*/
private static void sort3(int[] array) {
for (int x = 0; x < array.length - 1; x++) {
int index = x;
int num = array[x];
for (int y = x + 1; y < array.length; y++) {
if (num > array[y]) {
index = y;
num = array[y];
}
}
if (index != x) {
swap(array, x, index);
}
}
}
/**
* 打印数组
* <p>Title: printArray</p>
* <p>Description: 打印数组</p>
* @param str
* @param array
*/
private static void printArray(String str, int[] array) {
System.out.print(str);
for (int x = 0; x < array.length; x++) {
if (x != array.length -1)
System.out.print(array[x] + ", ");
else {
System.out.println(array[x]);
}
}
}
}
二、冒泡排序
/**
* <p>Title: SelectionSort</p>
* <p>Description: 选择排序</p>
*/
public class BubboleSort {
/**
* 主方法
* <p>Title: main</p>
* <p>Description: 主方法</p>
* @param args
*/
public static void main(String[] args) {
int[] array = newArray();
printArray("排序前:", array);
sort(array);
printArray("排序后:", array);
}
/**
* 创建一个数组
* <p>Title: newArray</p>
* <p>Description: 创建一个数组</p>
* @return
*/
private static int[] newArray() {
int[] array = new int[] {5, 7, 1, 3, 9, 2 ,8, 4, 6, 10};
return array;
}
/**
* 排序
* <p>Title: sort</p>
* <p>Description: 排序</p>
* @param array
*/
private static void sort1(int[] array) {
for (int x = 0; x < array.length - 1; x++) {
for (int y = 0; y < array.length - 1 - x; y++) {
if (array[y] > array[y + 1]) {
int temp = array[y];
array[y] = array[y + 1];
array[y + 1] = temp;
}
}
}
}
/**
* 排序
* <p>Title: sort</p>
* <p>Description: 排序</p>
* @param array
*/
private static void sort2(int[] array) {
for (int x = array.length - 1; x > 0; x--) {
for (int y = 0; y < x; y++) {
if (array[y] > array[y + 1]) {
int temp = array[y];
array[y] = array[y + 1];
array[y + 1] = temp;
}
}
}
}
/**
* 打印数组
* <p>Title: printArray</p>
* <p>Description: 打印数组</p>
* @param str
* @param array
*/
private static void printArray(String str, int[] array) {
System.out.print(str);
for (int x = 0; x < array.length; x++) {
if (x != array.length -1)
System.out.print(array[x] + ", ");
else {
System.out.println(array[x]);
}
}
}
}