简单排序
这里的简单排序分为三种:冒泡排序,选择排序、直接插入排序
下面先给出生成数组的类:
生成数据
package per.lihao.sort;
import java.util.Random;
/**
* Author: LiHao
* Time: 2018/12/5 10:09
*/
public class SortSequence {
private int MAXSIZE = 10;
private int[] mem = new int[MAXSIZE];
/**
* 给一数组数据构造
* @param array
*/
public SortSequence(int... array){
if(array.length>MAXSIZE){
System.out.println("输入参数大于最大长度,自动扩增...");
MAXSIZE = array.length;
mem = new int[MAXSIZE];
}
for (int i =0;i<MAXSIZE;i++){
mem[i] = array[i];
}
}
/**
* 最大长度构造
* @param maxlength
*/
public SortSequence(int maxlength){
MAXSIZE = maxlength;
mem = new int[MAXSIZE];
Random random = new Random();
for (int i=0;i<MAXSIZE;i++){
mem[i] = random.nextInt(200);
}
}
/**
* 无参构造
*/
public SortSequence(){
Random random = new Random();
for (int i=0;i<MAXSIZE;i++){
mem[i] = random.nextInt(200);
}
}
/**
* 获取数据
* @return
*/
public int[] getMem(){
return mem;
}
/**
* 打印原始数据
*/
public void printMem(){
for (int i=0;i<MAXSIZE;i++){
System.out.print(mem[i]+" ");
}
System.out.println();
}
}
以下排序均默认为逆序
冒泡排序
流程:冒泡排序是交换排序的一种,他是从尾部开始遍历,两两数据之间进行比较,若前者小于后者,则交换,这样最大的数据就会不断的往前,直到达到首部。不断的重复,则数据就会进行排序。
package per.lihao.sort.simplesort;
import per.lihao.sort.SortSequence;
/**
* 冒泡排序
* 交换排序之一
* 时间复杂度是O(n^2)
* Author: LiHao
* Time: 2018/12/5 10:07
*/
public class BubbleSort {
public static void printArray(int[] a){
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
/**
* 最简单的版本
* 从上到下依次循环
* 若遇到比当前指针所指数据大(小)的数据,直接交换
* 直到没有比当前指针数据大(小)的数据时,指针往下走
* 结束循环
* 分析:该方法较为低效;票好序的序列对未排序的序列无帮助
* @param array
* @param reverse
* @return
*/
public static int[] sort_one(int[] array,boolean reverse){
int[] result = array.clone();
for(int i=0;i<result.length-1;i++){
for (int j=i+1;j<result.length;j++){
if (reverse){
if (result[j]>result[i]){
int t = result[j];
result[j] = result[i];
result[i] = t;
}
}else {
if (result[j]<result[i]){
int t = result[j];
result[j] = result[i];
result[i] = t;
}
}
}
}
return result;
}
/**
* 修改后的版本
* 进行两两比较,将较大(小)的一方不断地往上排 --- 标准的冒泡排序
* 效率会比第一种高一些
* @param array
* @return
*/
public static int[] sort_two(int[] array,boolean reverse){
int[] result = array.clone();
for (int i=0;i<result.length;i++){
for (int j=result.length-1;j>i;j--){
if (reverse){
if(result[j]>result[j-1]){
int t = result[j];
result[j] = result[j-1];
result[j-1] = t;
}
}
else {
if(result[j]<result[j-1]){
int t = result[j];
result[j] = result[j-1];
result[j-1] = t;
}
}
}
}
return result;
}
/**
* 加入了对子序列是否为没有被排序的判断
* 若上次循环已经判断子序列排序notsorted为false,这说明该子序列已经有序,不用再进行比较了
* @param array
* @param reverse
* @return
*/
public static int[] sort_three(int[] array,boolean reverse){
int[] result = array.clone();
boolean notsorted = true;
for (int i=0;(i<result.length) && notsorted;i++){
notsorted = false;
for (int j=result.length-1;j>i;j--){
if (reverse){
if(result[j]>result[j-1]){
int t = result[j];
result[j] = result[j-1];
result[j-1] = t;
notsorted = true;
}
}
else {
if(result[j]<result[j-1]){
int t = result[j];
result[j] = result[j-1];
result[j-1] = t;
notsorted = true;
}
}
}
}
return result;
}
public static void main(String[] args) {
SortSequence ss = new SortSequence(82,195,19,108,153,39,120,125,136,181);
ss.printMem();
System.out.println("****************************");
int[] a = BubbleSort.sort_two(ss.getMem(),false);
BubbleSort.printArray(a);
}
}
简单选择排序
选择排序的一种
流程:指针指定到第i个位置(i=0),并与其后的所有数据进行比较,选出最大的那一个进行交换。循环继续,i+1,则每个位置上都会选取第i个大的数据,最终完成排序。
package per.lihao.sort.simplesort;
import per.lihao.sort.SortSequence;
/**
* 选择排序
* 优点是:无需那么多次数据的移动,时间效率为:O(n^2)
* Author: LiHao
* Time: 2018/12/5 13:54
*/
public class SelectSort {
public static int[] sort(int[] array,boolean reverse){
int[] result = array.clone();
int cursor,cvalue;
for (int i=0;i<result.length;i++){
cursor = i;
cvalue = result[i];
for (int j=i;j<result.length;j++){
if (reverse){
if(result[j]>cvalue){
cvalue = result[j];
cursor = j;
}
}else {
if(result[j]<cvalue){
cvalue = result[j];
cursor = j;
}
}
}
if(cursor!=i){
int t = result[cursor];
result[cursor] = result[i];
result[i] = t;
}
}
return result;
}
public static void main(String[] args) {
SortSequence ss = new SortSequence(82,195,19,108,153,39,120,125,136,181);
ss.printMem();
System.out.println("****************************");
int[] a = SelectSort.sort(ss.getMem(),false);
BubbleSort.printArray(a);
}
}
直接插入排序
流程:循环选取i=1之后的数据,每次插入时默认前面的序列是有序的,找到当前数据插入的位置(从尾部依次该位置前的数据比较,找到大于该值的索引x,则x+1后的的数据每个都需要往后移动一位,再将该值赋给x+1索引处),最终完成排序。
package per.lihao.sort.simplesort;
import per.lihao.sort.SortSequence;
/**
* 直接插入排序
* 时间复杂度 O(n^2)
* Author: LiHao
* Time: 2018/12/5 14:11
*/
public class InsertSort {
public static int[] sort(int[] array,boolean reverse){
int[] result = array.clone();
for (int i = 1;i<result.length;i++){
if (reverse){
if(result[i]>result[i-1]){
int j=i;
int temp = result[i];
while (j>0 && result[j-1]<temp){
result[j] = result[j-1];
j--;
}
result[j] = temp;
}
}else {
if(result[i]<result[i-1]){
int j=i;
int temp = result[i];
while (j>0 && result[j-1]>temp){
result[j] = result[j-1];
j--;
}
result[j] = temp;
}
}
}
return result;
}
public static void main(String[] args) {
SortSequence ss = new SortSequence(82,195,19,108,153,39,120,125,136,181);
ss.printMem();
System.out.println("****************************");
int[] a = InsertSort.sort(ss.getMem(),false);
BubbleSort.printArray(a);
}
}