package com.canofy.structure;
import org.apache.log4j.Logger;
public class Sort {
static Logger logger=Logger.getLogger(Sort.class);
//插入排序法
private static void insertSort(int[] arraySort) {
int in, out;
for (out = 1; out < arraySort.length; out++) {
int tmp = arraySort[out];
for(in=out;in>0;in--){
if(tmp<arraySort[in-1]){
arraySort[in]=arraySort[in-1];
}else{
break;
}
}
// in = out;
// while (in > 0 && tmp <= arraySort[in - 1]) {
// arraySort[in] = arraySort[in - 1];
// in--;
// }
arraySort[in] = tmp;
}
}
//选择排序法
private static void selectSort(int[] arraySort) {
int in, out;
for (out = 0; out < arraySort.length-1; out++) {
int min = out;
for (in = out+1; in < arraySort.length; in++) {
if (arraySort[in] < arraySort[min]) { //选择最小的数
min = in;
}
}
swap(out, min, arraySort);
}
}
//冒泡排序法
private static void bubbleSortBigger(int[] arraySort) {
int in, out;
for (out = arraySort.length - 1; out > 0; out--) {
for (in = 0; in < out; in++) {
if (arraySort[in] > arraySort[in + 1]) {
//logger.info("arraySort"+in+":"+arraySort[in]);
swap(in, in + 1, arraySort);
}
}
}
}
private static void swap(int a, int b, int[] arraySort) {
int tmp = arraySort[a];
arraySort[a] = arraySort[b];
arraySort[b] = tmp;
}
public static void main(String[] args) {
Logger logger=Logger.getLogger(Sort.class);
Sort sort=new Sort();
int[] arrayBub = { 43, 36, 11, 10, 29, 58, 15, 9 };
insertSort(arrayBub);
for(int i=0;i<arrayBub.length;i++){
logger.info("arrayBub"+i+":"+arrayBub[i]);
}
}
}
插入 选择 冒泡排序
最新推荐文章于 2024-08-18 22:24:50 发布