各种排序算法

/**
 * 冒泡排序
 * @author Q.Yuan
 *
 */
public class BubbleSort {
	public void bubleSort(int[] a) {
		boolean exactSort = true;
		for (int i = 1; i < a.length; i++) {
			for (int j = 0; j < a.length - i; j++) {
				if (a[j] > a[j + 1]) {
					int temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
					exactSort = false;
				}
				if (exactSort) {
					return;
				}
			}
		}
	}

	public static void main(String[] args) {
		int a[] = { 1, -1, 2, -3, 4, -5, 6, -7 };
		BubbleSort bs = new BubbleSort();
		bs.bubleSort(a);
		for (int i : a) {
			System.out.print(i + " ");
		}
	}
}




/**
 * 直接插入排序
 * @author Q.Yuan
 *
 */
public class DirectInsertSort {
	public void directInsertSort(int[] a) {
		int record;
		for (int i = 1; i < a.length; i++) {
			if (a[i] < a[i - 1]) {
				record = a[i];
				a[i] = a[i - 1];
				int j = i - 2;
				for (; j >= 0 && a[j] > record; j--) {
					a[j + 1] = a[j];
				}
				j++;
				a[j] = record;
			}
		}
	}

	public static void main(String[] args) {
		int a[] = { 1, -1, 2, -3, 4, -5, 6, -7 };
		DirectInsertSort dss = new DirectInsertSort();
		dss.directInsertSort(a);
		for (int i : a) {
			System.out.print(i + " ");
		}
	}
}



/**
 * 折半插入排序
 * 
 * @author Q.Yuan
 * 
 */
public class BinaryInsertSort {
	public void binaryInsertSort(int[] a) {
		int record;
		int low;
		int high;
		for (int i = 1; i < a.length; i++) {
			if (a[i] < a[i - 1]) {
				record = a[i];
				a[i] = a[i - 1];
				low = 0;
				high = i - 1;
				while (low <= high) {
					int mid = (low + high) / 2;
					if (record < a[mid]) {
						high = mid - 1;
					} else {
						low = mid + 1;
					}
				}
				for (int j = i - 2; j >= low; j--) {
					a[j + 1] = a[j];
				}
				a[low] = record;
			}
		}
	}

	public static void main(String[] args) {
		int a[] = { 1, -1, 2, -3, 4, -5, 6, -7 };
		BinaryInsertSort dss = new BinaryInsertSort();
		dss.binaryInsertSort(a);
		for (int i : a) {
			System.out.print(i + " ");
		}
	}
}





/**
 * 堆排序
 * @author Q.Yuan
 *
 */
public class HeapSort1 {
	public void adjustHeap(int[] a, int pos, int length) {
		int record = a[pos];
		for (int j = pos * 2 + 1; j < length; j = j * 2 + 1) {
			if (j + 1 < length && a[j + 1] > a[j]) {
				j++;
			}
			if (a[j] > record) {
				a[pos] = a[j];
				pos = j;
			}
		}
		a[pos] = record;
	}

	public void sort(int[] a) {
		for (int i = a.length / 2 - 1; i >= 0; i--) {
			adjustHeap(a, i, a.length);
		}
		for (int j = a.length - 1; j > 0; j--) {
			int temp = a[j];
			a[j] = a[0];
			a[0] = temp;
			adjustHeap(a, 0, j);
		}
	}

	public static void main(String[] args) {
		int a[] = { 1, -1, 2, -3, 4, -5, 6, -7 };
		HeapSort1 hs = new HeapSort1();
		hs.sort(a);
		for (int i : a) {
			System.out.print(i + " ");
		}
	}
}






/**
 * 简单选择排序
 * @author Q.Yuan
 *
 */
public class SelectSort {
	public void selectSort(int[] a){
		for(int i = 0;i < a.length - 1;i++){
			int index = selectMin(a, i);
			if(i != index){
				int temp = a[i];
				a[i] = a[index];
				a[index] = temp;
			}
		}
	}
	/**
	 * 
	 * @param a		数组
	 * @param pos   从pos位置开始向后查找
	 * @return		找出最小的元素下标
	 */
	public int selectMin(int[] a,int pos){
		int min = a[pos];
		int index = pos;
		for(int i = pos + 1;i < a.length;i++){
			if(a[i] < min){
				min = a[i];
				index = i;
			}
		}
		return index;
	}
	public static void main(String[] args) {
		int a[] = { 1, -1, 2, -3, 4, -5, 6, -7 };
		SelectSort ss = new SelectSort();
		ss.selectSort(a);
		for (int i : a) {
			System.out.print(i + " ");
		}
	}
}



/**
 * 二路归并排序
 * @author Q.Yuan
 *
 */
public class MergeSort {
	/**
	 * 归并a[low...mid],a[mid+1...high]
	 * @param a
	 * @param low
	 * @param mid
	 * @param high
	 */
	public void merge(int[] a,int low,int mid,int high,int[] union){
		int pLeftStart = low;
		int pRightStart = mid + 1;
		int pUnion = low;
		while(pLeftStart <= mid && pRightStart <= high){
			if(a[pLeftStart] <= a[pRightStart]){
				union[pUnion++] = a[pLeftStart++];
			}else{
				union[pUnion++] = a[pRightStart++];
			}
		}
		while(pLeftStart <= mid){
			union[pUnion++] = a[pLeftStart++];
		}
		while(pRightStart <= high){
			union[pUnion++] = a[pRightStart++];
		}
		for(int i = low;i <= high;i++){
			a[i] = union[i];
		}
	}
	
	public void mergeSort(int[] a,int[] union,int low,int high){
		if(low < high){
			int mid = (low + high)/2;
			mergeSort(a,union,low,mid);
			mergeSort(a, union, mid+1, high);
			merge(a,low,mid,high,union);
		}
	}
	public static void main(String[] args) {
		int[] a = { 1, -1, 2, -3, 4, -5, 6, -7 };
		int[] union = new int[a.length];
		MergeSort ms = new MergeSort();
		ms.mergeSort(a, union, 0, a.length-1);
		for (int i : a) {
			System.out.print(i + " ");
		}
	}
}




/**
 * 快速排序
 * @author Q.Yuan
 *
 */
public class QuickSort {
	public int partition(int[] a, int low, int high) {
		int pivot = a[low];
		while (low < high) {
			while (low < high && a[high] > pivot) {
				high--;
			}
			a[low] = a[high];
			while (low < high && a[low] < pivot) {
				low++;
			}
			a[high] = a[low];
		}
		a[low] = pivot;
		return low;
	}

	public void quickSort(int[] a, int low, int high) {
		if (low < high) {
			int p = partition(a, low, high);
			quickSort(a, low, p - 1);
			quickSort(a, p + 1, high);
		}
	}

	public static void main(String[] args) {
		QuickSort qSort = new QuickSort();
		int[] a = {1, -1, 2, -3, 4, -5, 6, -7};
		qSort.quickSort(a, 0, a.length - 1);
		for (int i : a) {
			System.out.print(i + " ");
		}
	}
}




/**
 * 希尔排序
 * @author Q.Yuan
 *
 */
public class ShellSort {
	public void shellSort(int[] a, int dk) {
		int record;
		for (int i = dk; i < a.length; i++) {
			if (a[i - dk] > a[i]) {
				record = a[i];
				a[i] = a[i - dk];
				int j = i - dk * 2;
				for (; j >= 0 && record < a[j]; j -= dk) {
					a[j + dk] = a[j];
				}
				a[j + dk] = record;
			}
		}
	}

	/**
	 * 
	 * @param a		要排序的数组            
	 * @param dk	增量数组,最后一个元素必须为1
	 *            
	 */
	public void sort(int[] a, int[] dk) {
		for (int i = 0; i < dk.length; i++) {
			shellSort(a, dk[i]);
		}
	}

	public static void main(String[] args) {
		int a[] = { 1, -1, 2, -3, 4, -5, 6, -7 };
		int dk[] = {5,3,1};
		ShellSort ss = new ShellSort();
		ss.sort(a,dk);
		for (int i : a) {
			System.out.print(i + " ");
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值