各种排序算法1
package demo;
import java.util.Arrays;
import java.util.Random;
import org.junit.Test;
public class Sort {
?/**
? * 冒泡排序
? * 若前一个元素大于后一个元素,将两者交换。
? */
?public int[] bubbleSort(int[] iAry) {
??int aryLen = iAry.length;
??for (int i = 1; i < aryLen; i++) {
???for (int j = 0; j < aryLen - i; j++) {
????if (iAry[j] > iAry[j + 1]) {
?????swap(iAry, j, j + 1);
????}
???}
??}
??return iAry;
?}
?/**
? * 选择排序
? */
?public int[] selectSort(int[] iAry) {
??int aryLen = iAry.length;
??int index = 0;
??for (int i = 1; i < aryLen; i++) {
???index = 0;
???for (int j = 1; j <= aryLen - i; j++) {
????if (iAry[j] > iAry[index]) {
?????index = j;
????}
???}
???swap(iAry, index, aryLen - i);
??}
??return iAry;
?}
?/**
? * 插入排序
? * 将一个记录插入到已排好序的有序表中,从而得到一个新的记录数增1的有序表。
? */
?public int[] insertSort(int[] iAry) {
??int aryLen = iAry.length;
??for (int i = 1; i < aryLen; i++) {
???for (int j = 0; j < i; j++) {
????if (iAry[j] > iAry[i]) {
?????swap(iAry, i, j);
????}
???}
??}
??return iAry;
?}