1.排序
这篇博客本不是为了给别人来看,只是想让自己养成一个积累自己学习知识的习惯。大学没有好好学习这些基础知识,待到了遇到了职业的天花板的时候才追悔莫及,痛定思痛后,自己打算唯有重新学会这些才能真的提高自己,并让自己内心认可自己。种一棵树,最好的时机是十年前,其次是现在。
言归正传,今天的学习开始于一个在线的数据结构和算法的演示网站http://zh.visualgo.net/en/sorting?slide=1,如果有兴趣可以在这里学习。
今天我从最基础的排序学起。
先开始翻译下网站中的一段对排序的综述:
Sorting is a very classic problem of reordering items that can be compared (Integers, floating-point numbers, strings, etc) of an array (or a list) in a certain order (increasing, non-decreasing, decreasing, non-increasing, lexicographical, etc). There are many different sorting algorithms, each has its own advantages and limitations. Sorting is commonly used as the introductory problem in various Computer Science classes to showcase a range of algorithmic ideas.
Sorting是一个非常经典的将一个数组或者列表中的可以比较的项目(比如 整形,浮点型,字符串等等)以某种顺序(如递增,非递减,非递增,递减 或按照字典等等)进行重新排列的问题
排序有许多不同的算法,每一种都有它的优势和限制。
Sorting 通常在计算机科学课程中用于入门问题去展示一系列算法思想。
第一种排序,Bublle Sorting,冒泡排序也是最简单的排序方法。
先一段自己的意识流Java冒泡排序实现:
private static int[] bubbleSort(int[] jumbledArray) {
int calcCount = 0;
for(int i = jumbledArray.length -1 ;i > 0;i--){
for (int j = 0; j < i; j++) {
if(jumbledArray[j] > jumbledArray[j + 1]) {
int temp = jumbledArray[j];
jumbledArray[j] =jumbledArray[j+1];
jumbledArray[j+1] = temp;
}
calcCount ++;
}
}
System.out.println("calc count= " + calcCount);
return jumbledArray;
}
运行结果:
System.out.println("bubbleSort1: calc count= " + calcCount);
System.out.println("bubbleSort1: iterate count= " + iterateCount);
网站教程中的伪代码
do swapped = false for i = 1 to indexOfLastUnsortedElement-1 if leftElement > rightElement swap(leftElement, rightElement) swapped = true while swapped
伪代码的Java实现:
int calcCount = 0; int iterateCount = 0; int indexOfLastUnsortedElement = jumbledArray.length; boolean swappedFlag; do { swappedFlag = false; for (int j = 0; j < indexOfLastUnsortedElement - 1; j++) { if (jumbledArray[j] > jumbledArray[j + 1]) { int temp = jumbledArray[j]; jumbledArray[j] = jumbledArray[j + 1]; jumbledArray[j + 1] = temp; swappedFlag = true; calcCount++; } iterateCount++; } indexOfLastUnsortedElement--; } while (swappedFlag); System.out.println("bubbleSort2: calc count= " + calcCount); System.out.println("bubbleSort2: iterate count= " + iterateCount); return jumbledArray;
运行结果: bubbleSort2: calc count= 43 bubbleSort2: iterate count= 95 [2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50]
通过结果证明两周冒泡排序真正的交换次数是一致的,但是遍历的次数却是网站实例上更优。现在我尝试将数组扩大一个数量级,由15变成150,来看看运行结果:
数组长度为50的结果:
bubbleSort1: calc count= 565
bubbleSort1: iterate count= 1225
bubbleSort2: calc count= 565
bubbleSort2: iterate count= 1197
数组长度为500的结果:
bubbleSort1: calc count= 61877
bubbleSort1: iterate count= 124750
bubbleSort2: calc count= 61877
bubbleSort2: iterate count= 124519
这里的数组由随机产生,可见随着数组长度的增加,两种冒泡排序的性能趋近于相等。
附上Java代码:
package com.bk.corejava.algorithms.sort; import java.util.Arrays; import java.util.Random; /** * Created by tim on 2017/7/11. */ public class BubbleSort { public static void main(String[] args) { // int[] jumbledArray = new int[]{3, 38, 44, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48}; int[] jumbledArray = new int[500]; Random random = new Random(); for (int i = 0; i < jumbledArray.length; i++) { jumbledArray[i] = random.nextInt(100); } System.out.println(Arrays.toString(jumbledArray)); int[] sortedArray1 = bubbleSort1(Arrays.copyOf(jumbledArray, 500)); System.out.println(Arrays.toString(sortedArray1)); System.out.println(); int[] sortedArray2 = bubbleSort2(Arrays.copyOf(jumbledArray, 500)); System.out.println(Arrays.toString(sortedArray2)); } private static int[] bubbleSort1(int[] jumbledArray) { int iterateCount = 0; int calcCount = 0; for (int i = jumbledArray.length - 1; i > 0; i--) { for (int j = 0; j < i; j++) { if (jumbledArray[j] > jumbledArray[j + 1]) { int temp = jumbledArray[j]; jumbledArray[j] = jumbledArray[j + 1]; jumbledArray[j + 1] = temp; calcCount ++; } iterateCount++; } } System.out.println("bubbleSort1: calc count= " + calcCount); System.out.println("bubbleSort1: iterate count= " + iterateCount); return jumbledArray; } private static int[] bubbleSort2(int[] jumbledArray) { int calcCount = 0; int iterateCount = 0; int indexOfLastUnsortedElement = jumbledArray.length; boolean swappedFlag; do { swappedFlag = false; for (int j = 0; j < indexOfLastUnsortedElement - 1; j++) { if (jumbledArray[j] > jumbledArray[j + 1]) { int temp = jumbledArray[j]; jumbledArray[j] = jumbledArray[j + 1]; jumbledArray[j + 1] = temp; swappedFlag = true; calcCount++; } iterateCount++; } indexOfLastUnsortedElement--; } while (swappedFlag); System.out.println("bubbleSort2: calc count= " + calcCount); System.out.println("bubbleSort2: iterate count= " + iterateCount); return jumbledArray; } }

浙公网安备 33010602011771号