一、冒泡排序
package com.xwiam.algorithms.sort;
public class BubbleSort {
private long[] instance;
private int nElems;
public BubbleSort(int maxSize) {
instance = new long[maxSize];
nElems = 0;
}
public void insert(long value) {
instance[nElems] = value;
nElems++;
}
public int find(long value) {
for (int findPoint = 0;findPoint < nElems;findPoint++) {
if (instance[findPoint] == value) {
return findPoint;
}
}
return nElems;
}
public boolean delete(long value) {
int deletePoint = find(value);
if (deletePoint == nElems) {
return false;
} else {
for (int i = deletePoint; i < nElems - 1; i++) {
instance[i] = instance[i + 1];
}
nElems--;
return true;
}
}
public void display() {
for (int i = 0;i < nElems;i++) {
System.out.print(instance[i] + " ");
}
System.out.println("");
}
public void sort() {
for (int out = nElems - 1;out > 0;out--) {
for (int in = 0;in < out;in++) {
if (instance[in] > instance[in + 1]) {
swap(in, in + 1);
}
}
}
}
private void swap(int source, int dest) {
long temp = instance[source];
instance[source] = instance[dest];
instance[dest] = temp;
}
public static void main(String[] args) {
BubbleSort bubbleSort = new BubbleSort(100);
bubbleSort.insert(20);
bubbleSort.insert(30);
bubbleSort.insert(10);
bubbleSort.display();
bubbleSort.sort();
bubbleSort.display();
}
}
二、选择排序
package com.xwiam.algorithms.sort;
public class SelectSort {
private long[] instance;
private int nElems;
public SelectSort(int maxSize) {
instance = new long[maxSize];
nElems = 0;
}
public void insert(long value) {
instance[nElems] = value;
nElems++;
}
public int find(long value) {
for (int searchPoint = 0;searchPoint < nElems;searchPoint++) {
if (instance[searchPoint] == value) {
return searchPoint;
}
}
return nElems;
}
public boolean delete(long value) {
int deletePoint = find(value);
if (deletePoint == nElems) {
return false;
} else {
for (int i = deletePoint; i < nElems - 1; i++) {
instance[i] = instance[i + 1];
}
nElems--;
return true;
}
}
public void display() {
for (int i = 0;i < nElems;i++) {
System.out.print(instance[i] + " ");
}
System.out.println("");
}
public void selectSort() {
for (int out = 0;out < nElems - 1;out++) {
int min = out;
for (int in = out + 1;in < nElems;in++) {
if (instance[in] < instance[min]) {
min = in;
}
}
swap(out, min);
}
}
private void swap(int source, int dest) {
long temp = instance[source];
instance[source] = instance[dest];
instance[dest] = temp;
}
public static void main(String[] args) {
SelectSort selectSort = new SelectSort(100);
selectSort.insert(10);
selectSort.insert(30);
selectSort.insert(40);
selectSort.insert(20);
selectSort.display();
selectSort.selectSort();
selectSort.display();
}
}
三、插入排序
package com.xwiam.algorithms.sort;
public class InsertSort {
private long[] instance;
private int nElems;
public InsertSort(int maxSize) {
instance = new long[maxSize];
nElems = 0;
}
public void insert(long value) {
instance[nElems] = value;
nElems++;
}
public void display() {
for (int i = 0;i < nElems;i++) {
System.out.print(instance[i] + " ");
}
System.out.println("");
}
public void insertSort() {
for (int out = 1;out < nElems;out++) {
long temp = instance[out];
int in = out;
while (in > 0 && instance[in - 1] > temp){
instance[in] = instance[in - 1];
in--;
}
instance[in] = temp;
}
}
public static void main(String[] args) {
InsertSort insertSort = new InsertSort(100);
insertSort.insert(10);
insertSort.insert(30);
insertSort.insert(40);
insertSort.insert(20);
insertSort.display();
insertSort.insertSort();
insertSort.display();
}
}
四、三种简单排序算法的比较
冒泡排序做了N*(N-1)/2次比较,如果数据是随机的,交换的次数是N*(N-1)/4。(最坏的情况下,即初始数据逆序时,每次比较都需要交换)
选择排序做了N*(N-1)/2次比较,交换的次数<=N。当N值较小时,特别是如果交换的时间比比较的时间级大得多时,选择排序是相当快的。
插入排序 数据随机的话,做了N*(N-1)/4次比较,复制次数和比较次数大致相等,如果数据基本顺序排序的话,比较次数和复制次数只需要O(n) 的时间。