package com.wq.swap;
public abstract class SortAlgorithm {
//排序
public abstract void sort(int[] dataArr);
//打印输出
public void print(int[] arr) {
for (int i : arr) {
System.out.print(i + "\t");
}
System.out.println();
}
//交换对应位置数据
public void swap(int[] data, int i, int j) {
int temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
public class InsertSort extends SortAlgorithm{
//插入排序(从第二个数开始,用这个数和前边的数据进行比较
//如果其前面的数都大于这个数,交换位置
public void sort(int[] dataArr) {
for (int i = 1; i < dataArr.length; i++) {
for (int j = i; j > 0; j--) {
if (dataArr[j] < dataArr[j-1]) {
swap(dataArr, j , j-1);
}
}
}
print(dataArr);
}
}
package com.wq.swap;
public class SelectionSort extends SortAlgorithm {
//选择排序(假设第i个为最小数,获取其下标,用其后面所有数跟此数做比较
//如果小于此数,交换下标,直到其后所有数比较完为止
public void sort(int[] dataArr) {
for (int i = 0; i < dataArr.length; i++) {
int lowIndex = i;
for (int j = dataArr.length-1; j > i; j--) {
if (dataArr[j] < dataArr[lowIndex]) {
lowIndex = j;
}
}
swap(dataArr, lowIndex, i);
}
print(dataArr);
}
}
package com.wq.swap;
public class ShellSort extends SortAlgorithm {
//希尔排序(先设置增量,然后根据增量进行插入排序,最后在进行一次插入排序)
public void sort(int[] dataArr) {
//设置增量
for (int i = dataArr.length/2; i > 2; i/=2) {
for (int j = 0; j < i; j++) {
insertSort(dataArr, j, i);
}
}
insertSort(dataArr, 0, 1);
print(dataArr);
}
//进行插入排序
private void insertSort(int[] dataArr, int start, int inc) {
for (int i = start; i < dataArr.length; i+=inc) {
for(int j = i; j >= inc; j -=inc) {
if (dataArr[j] < dataArr[j-inc])
swap(dataArr, j, j-inc);
}
}
}
}
package com.wq.swap;
import java.util.Arrays;
public class SortAlgorithmClient {
public static void main(String[] args) {
int[] dataArr = { 9, 2, 6, 4, 1, 8, 5, 3, 7 };
// SortAlgorithm bubbleSort = new BubbleSort();
// bubbleSort.sort(dataArr);
// SortAlgorithm insertSort = new InsertSort();
// insertSort.sort(dataArr);
// SortAlgorithm selectionSort = new SelectionSort();
// selectionSort.sort(dataArr);
// SortAlgorithm shellSort = new ShellSort();
// shellSort.sort(dataArr);
printInfo(dataArr);
SortAlgorithm[] sorts = { new BubbleSort(), new InsertSort(),
new SelectionSort(), new ShellSort() };
for (SortAlgorithm sortAlgorithm : sorts) {
printSortName(sortAlgorithm);
sortAlgorithm.sort(dataArr);
}
}
private static void printSortName(SortAlgorithm sortAlgorithm) {
StringBuffer sb = new StringBuffer();
sb.append("---------------------------------");
sb.append(sortAlgorithm.getClass().getName().toUpperCase());
sb.append("---------------------------------");
printInfo(sb);
}
private static void printInfo(Object object) {
if (object instanceof int[]) {
System.out.println(Arrays.toString((int[])object));
} else if (object instanceof StringBuffer) {
System.out.println(object.toString());
}
}
}
[9, 2, 6, 4, 1, 8, 5, 3, 7]
---------------------------------COM.WQ.SWAP.BUBBLESORT---------------------------------
1 2 3 4 5 6 7 8 9
---------------------------------COM.WQ.SWAP.INSERTSORT---------------------------------
1 2 3 4 5 6 7 8 9
---------------------------------COM.WQ.SWAP.SELECTIONSORT---------------------------------
1 2 3 4 5 6 7 8 9
---------------------------------COM.WQ.SWAP.SHELLSORT---------------------------------
1 2 3 4 5 6 7 8 9