001-简单的java代码实现几种排序算法(插入,快速,冒泡,选择)

事先声明,本人小白,还望各位前辈不吝赐教。以下代码都在eclipse可以直接运行。

插入排序:

public class InsertSort {


	public void insertSort(int []array) {
		for (int i = 0; i < array.length; i++) {//第一层迭代,控制排序的趟数
			int j = i;
			while (j>0) {//第二层迭代,使得要插入的元素与之前的元素进行一一比较
				while (array[j]
快速排序:

public class QuickSort {

	public void quickSort(int[] array, int low, int high) {//快排控制方法,也就是递归的实现方法
		if (low < high) {
			int partion = partion(array, low, high);
			quickSort(array, low, partion - 1);
			quickSort(array, partion + 1, high);
		}
	}

	public int partion(int[] array, int low, int high) {//核心方法,对当前的数组进行割分
		int key = array[low];
		while (low < high) {
			while (low < high && array[high] > key) {
				high--;
			}
			int t = array[low];
			array[low] = array[high];
			array[high] = t;
			while (low < high) {
				while (low < high && array[low] <= key) {
					low++;
				}
				int t1 = array[low];
				array[low] = array[high];
				array[high] = t1;

			}
		}
		array[high] = key;
		return high;

	}
}

冒泡排序:

public class BubbleSort {

	public void bubbleSort(int []array) {
		int key ;
		int flag ;
		for (int i = 0; i < array.length; i++) {//第一层迭代,控制冒泡的次数
			flag = 1;//设置标记,一旦冒泡排序已经将数组排好顺序,跳出之
			System.out.println();
			System.out.println("第"+(i+1)+"趟冒泡:");
			key = 1;
			while (keyarray[key]) {
					int t = array[key-1];
						array[key-1] = array[key];
						array[key] = t;
						flag = 0;
				}
				key++;
			}
			for (int j = 0; j < array.length; j++) {
				System.out.print(array[j]+"\t");
			}
			if (flag==1) {//如果排序已结束,跳出
				System.exit(0);
			}
		}
	}
}

选择排序:

public class ChoiceSort {

	public void choiceSort(int []array) {
		for (int i = 0; i < array.length; i++) {
			for (int j = i; j < array.length; j++) {
				if (array[j]



你可能感兴趣的:(java,算法,排序,时间复杂度,eclipse)