简单的排序算法包括 冒泡排序、选择排序、插入排序。
1. 冒泡排序
思路:从队列的开头开始,对相邻的两个数比较大小,较大的数移动到后一位。经过一轮遍历后,最后一个数字即队列的最大值。重复这一过程(已确定的值除外),即可完成排序。
实现:
package com.test;
/**
* 冒泡排序
* @author xurongsheng
* @date 2016年8月12日 下午3:05:02
*
*/
public class BubbleSort {
private long[] target;
public BubbleSort() {
}
public BubbleSort(int maxLength) {
target = new long[maxLength];
}
public void setTargetValue(int index,long value){
target[index] = value;
}
public long getTargetValue(int index){
return target[index];
}
/**
* 冒泡排序
* 比较次数 N*(N-1)/2,交换次数 N²/4
* 时间复杂度 O(N²)
* @author xurongsheng
* @date 2016年8月12日 上午11:11:56
*/
public void bubbleSort(){
for (int i = (target.length - 1); i > 1 ; i--) {
for (int j = 0; j < i; j++) {
if(target[j] > target[j+1]){
swap(j,j+1);
}
}
}
}
private void swap(int fromIndex,int toIndex){
long from = target[fromIndex];
long to = target[toIndex];
target[fromIndex] = to;
target[toIndex] = from;
}
public String printTargetValue(){
StringBuffer sb = new StringBuffer();
for (long l : target) {
sb.append(l).append(" ");
}
return sb.toString();
}
public static void main(String[] args) {
BubbleSort bs = new BubbleSort(6);
bs.setTargetValue(0, 1);
bs.setTargetValue(1, 3);
bs.setTargetValue(2, 5);
bs.setTargetValue(3, 7);
bs.setTargetValue(4, 9);
bs.setTargetValue(5, 12);
bs.bubbleSort();
System.out.println(bs.printTargetValue());
}
}
效率:
冒泡排序的比较: 在第一轮遍历时执行了 N-1 次,第二轮 N--2 次,以此类推。即: (N-1)+(N-2)+(N-3)+... ...+1 = N*(N-1)/2
冒泡排序的交换: 只有在需要时才交换,大概有一半的数据需要交换。也就是N*(N-1)/4
比较和交换的时间复杂度都是 O(N²)
2. 选择排序
思路: 基本思路与冒泡排序相同,从队列开头开始,对相邻两个数进行比较,但不做位移。记录下较大值的下标,在一轮遍历结束后,将最大值交换至队尾。重复这一过程即可。
实现:
package com.test;
/**
* 选择排序
* @author xurongsheng
* @date 2016年8月12日 下午3:05:15
*
*/
public class SelectSort {
private long[] target;
public SelectSort() {
}
public SelectSort(int maxLength) {
target = new long[maxLength];
}
public void setTargetValue(int index,long value){
target[index] = value;
}
public long getTargetValue(int index){
return target[index];
}
/**
* 选择排序
* 比较次数 N*(N-1)/2,交换次数 小于N
* 时间复杂度 O(N²)
* @author xurongsheng
* @date 2016年8月12日 下午2:47:55
*/
public void selectSort(){
for (int i = 0; i < target.length; i++) {
int index = i;
for (int j = (i+1); j < target.length; j++) {
if(target[index] > target[j]){
index = j;
}
}
swap(i,index);
}
}
private void swap(int fromIndex,int toIndex){
long from = target[fromIndex];
long to = target[toIndex];
target[fromIndex] = to;
target[toIndex] = from;
}
public String printTargetValue(){
StringBuffer sb = new StringBuffer();
for (long l : target) {
sb.append(l).append(" ");
}
return sb.toString();
}
public static void main(String[] args) {
SelectSort ss = new SelectSort(6);
ss.setTargetValue(0, 134);
ss.setTargetValue(1, 32);
ss.setTargetValue(2, 568);
ss.setTargetValue(3, 73);
ss.setTargetValue(4, 955);
ss.setTargetValue(5, 122);
ss.selectSort();
System.out.println(ss.printTargetValue());
}
}
package com.test;
/**
* 插入排序
* @author xurongsheng
* @date 2016年8月12日 下午3:05:25
*
*/
public class InsertSort {
private long[] target;
public InsertSort(){
}
public InsertSort(int maxLength){
target = new long[maxLength];
}
public void setTargetValue(int index,long value){
target[index] = value;
}
public long getTargetValue(int index){
return target[index];
}
/**
* 插入排序
* 比较次数: N*(N-1)/4
* 时间复杂度O(N²)
*
* @author xurongsheng
* @date 2016年8月12日 下午3:38:31
*/
public void insertSort(){
for (int i = 1; i < target.length; i++) {
long temp = target[i];
int flag = i;
while(flag > 0 && target[flag-1] >= temp){
target[flag] = target[flag-1];
--flag;
}
target[flag] = temp;
}
}
public String printTargetValue(){
StringBuffer sb = new StringBuffer();
for (long l : target) {
sb.append(l).append(" ");
}
return sb.toString();
}
public static void main(String[] args) {
InsertSort is = new InsertSort(6);
is.setTargetValue(0, 134);
is.setTargetValue(1, 32);
is.setTargetValue(2, 568);
is.setTargetValue(3, 73);
is.setTargetValue(4, 955);
is.setTargetValue(5, 122);
is.insertSort();
System.out.println(is.printTargetValue());
}
}