编程语言
首页 > 编程语言> > 为混乱带来秩序——Java排序

为混乱带来秩序——Java排序

作者:互联网

Java中最常用的几种算法如下:


具体实现代码如下:

public class NumberSort {

	public static void main(String[] args) {
		int[] selectionSortvalues = new int[] {9,10,6,5,0,7,8,3,2,4,1};
		selectionSort(selectionSortvalues);
		int[] insertionSortvalues = new int[] {9,10,6,5,0,7,8,3,2,4,1};
		insertionSort(insertionSortvalues);
		int[] bubbleSortvalues = new int[] {9,10,6,5,0,7,8,3,2,4,1};
		bubbleSort(bubbleSortvalues);
		int[]  quickSortvalues= new int[] {9,10,6,5,0,7,8,3,2,4,1};
		quickSort(quickSortvalues,0,quickSortvalues.length - 1);
		System.out.print("\n快速排序:");
		printValues(quickSortvalues);
		int[]  mergeSortvalues= new int[] {9,10,6,5,0,7,8,3,2,4,1};
		mergeSort(mergeSortvalues,0,mergeSortvalues.length - 1);
		System.out.print("\n归并排序:");
		printValues(mergeSortvalues);
		int[]  BucketSortvalues= new int[] {9,10,6,5,0,7,8,3,2,4,1};
		BuckeSort(BucketSortvalues,10);
		System.out.print("\n桶   排序:");
		printValues(BucketSortvalues);
	}
	
	/**
	 * 选择排序
	 * @param values
	 */
	public static void selectionSort(int[] values) {
		System.out.print("选择排序:");
		if (values.length <= 1) 
			return;
		int k,minIndex;
		for (int i = 0; i < values.length; i++) {
			minIndex = i;
			for (k = i + 1; k < values.length; k++) 
				if (values[k] < values[minIndex])
					minIndex = k;
			int temp = values[minIndex];
			values[minIndex] = values[i];
			values[i] = temp;
		}
		printValues(values);
	}
	/**
	 * 插入排序
	 * @param values
	 */
	public static void insertionSort(int[] values) {
		System.out.print("\n插入排序:");
		if (values.length <= 1) 
			return;
		int k,value;
		for (int i = 1; i < values.length; i++) {
			value = values[i];
			k = i - 1;
			while(k >= 0 && values[k] > value) {
				values[k + 1] = values[k];
				k = k - 1;
			}
			values[k + 1] = value;
			
		}
		printValues(values);
	}
	/**
	 * 冒泡排序
	 * @param values
	 */
	public static void bubbleSort(int[] values) {
		System.out.print("\n冒泡排序:");
		boolean swapped;
		for (int i = 0; i < values.length; i++) {
			swapped = false;
			for (int j = values.length - 1; j > i; j--) {
				if (values[j] < values[j - 1]) {
					int temp = values[j];
					values[j] = values[j - 1];
					values[j - 1] = temp;
					swapped = true;
				}
			}
			if(swapped == false)
				break;
		}
		printValues(values);
	}
	/**
	 * 快速排序
	 * @param values
	 * @param low
	 * @param hight
	 */
	public static void quickSort(int[] values,int low,int hight) {
		
		/*
		 * int low = 0; int hight= values.length - 1;
		 */
		if (low < hight) {
			int index = partition(values, low, hight);
			quickSort(values,low, index - 1);
			quickSort(values, index + 1, hight);
		}
		
		
	}
	
	public static int partition(int[] values,int low,int hight) {
		int privote = values[hight];
		int i = low - 1;
		for (int j = low; j <= hight - 1; j++) {
			if(values[j] <= privote) {
				i ++;
				int temp = values[i];
				values[i] = values[j];
				values[j] = temp;
			}
		}
		i ++;
		int temp = values[i];
		values[i] = values[hight];
		values[hight] = temp;
		return i;
	}
	
	public static void mergeSort(int[] values,int left,int right) {
		if (left == right) 
			return;
		if(left < right) {
			int middle = (left + right) / 2;
			mergeSort(values,left,middle);
			mergeSort(values, middle + 1, right);
			int[] temp = new int[values.length];
			for (int i = left; i <= right; i++) {
				temp[i] = values[i];
			}
			int index1 = left;
			int index2 = middle + 1;
			for (int i = left; i <= right; i++) {
				if(index1 == middle + 1)
					values[i] = temp[index2 ++];
				else if(index2 > right)
					values[i] = temp[index1 ++];
				else if(temp[index1] < temp[index2])
					values[i] = temp[index1 ++];
				else
					values[i] = temp[index2 ++];
			}
		}
	}
	/**
	 * 桶排序
	 * @param buckeSortvalues
	 * @param i
	 */
	private static void BuckeSort(int[] values, int maxVal) {
		int[] bucket = new int[maxVal + 1];
		int num = values.length;
		int bucketNum = bucket.length;
		for (int i = 0; i < bucketNum; i++) {
			bucket[i] = 0;
		}
		for (int i = 0; i < num; i++) {
			bucket[values[i]] ++;
		}
		int pos = 0;
		for (int i = 0; i < bucketNum; i++) {
			for (int j = 0; j < bucket[i]; j++) {
				values[pos ++] = i;
			}
		}
		
	}
	
	public static void printValues(int[] values) {
		for (int i = 0; i < values.length; i++) {
			System.out.print(values[i] + ",");
		}
	}

}

如有兴趣,请添加QQ群716106908交流。

标签:Java,int,++,length,values,low,秩序,排序
来源: https://blog.csdn.net/seven_lauseon/article/details/100181331