`
salever
  • 浏览: 255776 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

插入排序、选择排序、冒泡排序测试

阅读更多

   受软件工程“最简单的即最好的”思想的影响,某一直对算法这些东西不感冒,然后随着编程的深入,觉得有必要好好整理、学习一下这方面的知识了。毕竟算法是基础。下面针对三种最常见的O(N*N)时间复杂度的算法进行了测试,顺便分析一下它们的性能,以便加深理解。(MergeSort是归并排序,用来参考)

    测试代码为:

public static void main(String[] args) {
		int length = 100000;
	        testInsertSort(length);
	        testInsertSortWhenSorted(length);
		testSelectSort(length);
		testSelectSortWhenSorted(length);
		testMergeSort(length);
		testMergeSortWhenSorted(length);
		testBubbleSort(length);
		testBubbleSortWhenSorted(length);
		//testInsertSortRecu(length);
		//testInsertSortRecuWhenSorted(length);
}

public static void testInsertSort(int length){
		int[] array = SortUtil.createArray(length);
		SortUtil.printArray(array);
		long l1 = System.currentTimeMillis();
		SortUtil.insertSort(array);
		long l2 = System.currentTimeMillis();
		System.out.println("InsertSort time takes " + (l2 - l1) + " ms");
		SortUtil.printArray(array);
		
}
	
	public static void testInsertSortWhenSorted(int length){
		int[] array = SortUtil.createArray(length);
		SortUtil.mergeSort(array, 0, length -1);
		SortUtil.printArray(array);
		long l1 = System.currentTimeMillis();
		SortUtil.insertSort(array);
		long l2 = System.currentTimeMillis();
		System.out.println("InsertSortWhenSorted time takes " + (l2 - l1) + " ms");
		SortUtil.printArray(array);
		
}

    其中的array中的元素都是Java的Math.random()随机产生的,范围在0至1000以内。

    数组长度为:1000

InsertSort time takes 0 ms
InsertSortWhenSorted time takes 0 ms
SelectSort time takes 0 ms
testSelectSortWhenSorted time takes 0 ms
MergeSort time takes 0 ms
MergeSortWhenSorted time takes 0 ms
BubbleSort time takes 15 ms
BubbleSortWhenSorted time takes 0 ms

    数组长度为:10000

InsertSort time takes 63 ms
InsertSortWhenSorted time takes 0 ms
SelectSort time takes 109 ms
testSelectSortWhenSorted time takes 109 ms
MergeSort time takes 0 ms
MergeSortWhenSorted time takes 0 ms
BubbleSort time takes 250 ms
BubbleSortWhenSorted time takes 0 ms

    数组长度为:100000

InsertSort time takes 5453 ms
InsertSortWhenSorted time takes 0 ms
SelectSort time takes 11000 ms
testSelectSortWhenSorted time takes 11047 ms
MergeSort time takes 15 ms
MergeSortWhenSorted time takes 16 ms
BubbleSort time takes 25250 ms
BubbleSortWhenSorted time takes 0 ms

    再来一次数组长度为:100000

InsertSort time takes 5454 ms
InsertSortWhenSorted time takes 0 ms
SelectSort time takes 11031 ms
testSelectSortWhenSorted time takes 11031 ms
MergeSort time takes 32 ms
MergeSortWhenSorted time takes 16 ms
BubbleSort time takes 25172 ms
BubbleSortWhenSorted time takes 0 ms

    数组长度为:5000000

InsertSortWhenSorted time takes 15 ms
BubbleSortWhenSorted time takes 16 ms
 

总结一下:

    1,虽然这三种排序的时间复杂度都是O(n*n),但是还是有区别的。

    2,数组无序情况下,插入排序最快,选择排序次之,冒泡最慢。选择排序的时间复杂度就是O(n*n),而插入排序最坏的情况是O(n*n),所以平均下来只有它的一半。而冒泡排序因为移动数据较多,所以时间消耗更大

    3,数组有序情况下,冒泡(这里加上了哨兵判断,数组不移动时提前跳出循环)和插入排序(时间复杂度是O(n))都比较快。而选择排序则没什么影响。

 

    主要实现代码参考:

    public static int[] createArray(int length) {
		int[] array = new int[length];
		for (int index = 0; index < length; index++) {
			array[index] = (int) (Math.random() * 1000 +  Math.random() * 100 +Math.random() * 10);
		}
		return array;
    }

    public static void insertSort(int[] array) {
		for (int i = 1; i < array.length; i++) {
			int key = array[i];
			int j = i - 1;
			for (; j > -1 && key < array[j]; j--) {
				array[j + 1] = array[j];
			}
			array[j + 1] = key;
		}
	}

	public static void selectSort(int[] array) {
		for (int i = 0; i < array.length; i++) {
			int min = i;
			for (int j = i; j < array.length; j++) {
				if (array[min] < array[j]) {
					min = j;
				}
			}
			if (min != i) {
				int temp = array[i];
				array[i] = array[min];
				array[min] = temp;
			}
		}
	}

	public static void mergeSort(int[] array, int begin, int end) {
		if (end > begin) {
			int sep = (begin + end) / 2;
			mergeSort(array, begin, sep);
			mergeSort(array, sep + 1, end);
			merge(array, begin, sep, end);
		}
	}

	private static void merge(int[] array, int begin, int sep, int end) {
		int[] left = new int[sep - begin + 1];
		int[] right = new int[end - sep];
		System.arraycopy(array, begin, left, 0, sep - begin + 1);
		System.arraycopy(array, sep + 1, right, 0, end - sep);

		int i = 0, j = 0;

		// Copy left or right array to the array
		while (i < left.length && j < right.length) {
			if (left[i] <= right[j]) {
				array[begin + i + j] = left[i];
				i++;
			} else {
				array[begin + i + j] = right[j];
				j++;
			}
		}
		// If left array has more elements
		if (i < left.length) {
			System.arraycopy(left, i, array, begin + i + j, left.length - i);
		} else if (j < right.length) { // If right array has more elements
			System.arraycopy(right, j, array, begin + i + j, right.length - j);
		}
	}

	public static void bubbleSort(int[] array) {
		for (int i = 0; i < array.length; i++) {
			boolean isSorted = true;
			for (int j = 0; j < array.length - i - 1; j++) {
				int temp;
				if (array[j] > array[j + 1]) {
					isSorted = false;
					temp = array[j];
					array[j] = array[j + 1];
					array[j + 1] = temp;
				}
			}
			if(isSorted){
				break;
			}
		}
	}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics