常用一些排序算法

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	



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值