根据《Problem Solving with Algorithms and Data Structures using Python》和《Java数据结构和算法》整理
排序1—冒泡排序、选择排序和插入排序
以数组的排序为例介绍排序算法。若数组的index为0到n,从头向尾排序是指最值按0到n的顺序归位,从尾向头排序则反之。
冒泡排序
多次遍历列表。每次遍历,最大值会一直往前挪,直到遍历结束。每遍历一次就有一个最大值或最小值排在正确的位置。最值像冒泡一样陆续归位,故称冒泡排序。
效率:交换次数O(n2),复杂度O(n2)
从头向尾 排序
# python实现
def bubbleSort2(alist2):
for passnum in range(0, len(alist2)-1, 1):
for i in range(len(alist2)-1, passnum, -1):
if alist2[i] < alist2[i-1]:
alist2[i], alist2[i-1] = alist2[i-1], alist2[i]
// java实现
class BubbleSort2 {
public static void main(String[] args) {
int[] bubarr2 = {19, 18, 17, 16, 15, 14, 13, 12, 11};
for (int out = 0; out < bubarr2.length-1; out++)
for (int in = bubarr2.length-1; in > out; in--)
if (bubarr2[in] < bubarr2[in-1]) {
int temp = bubarr2[in];
bubarr2[in] = bubarr2[in-1];
bubarr2[in-1] = temp;
}
}
}
结果
11 19 18 17 16 15 14 13 12
11 12 19 18 17 16 15 14 13
11 12 13 19 18 17 16 15 14
11 12 13 14 19 18 17 16 15
11 12 13 14 15 19 18 17 16
11 12 13 14 15 16 19 18 17
11 12 13 14 15 16 17 19 18
11 12 13 14 15 16 17 18 19
从尾向头 排序
# python实现
def bubbleSort(alist):
for passnum in range(len(alist)-1, 0, -1):
for i in range(passnum):
if alist[i] > alist[i+1]:
alist[i], alist[i+1] = alist[i+1], alist[i]
// java实现
class BubbleSort {
public static void main(String[] args) {
int[] bubarr = {19, 18, 17, 16, 15, 14, 13, 12, 11};
for (int out = bubarr.length-1; out > 0; out--)
for (int in = 0; in < out; in++)
if (bubarr[in] > bubarr[in+1]) {
int temp = bubarr[in];
bubarr[in] = bubarr[in+1];
bubarr[in+1] = temp;
}
}
}
结果
18 17 16 15 14 13 12 11 19
17 16 15 14 13 12 11 18 19
16 15 14 13 12 11 17 18 19
15 14 13 12 11 16 17 18 19
14 13 12 11 15 16 17 18 19
13 12 11 14 15 16 17 18 19
12 11 13 14 15 16 17 18 19
11 12 13 14 15 16 17 18 19
选择排序
在冒泡排序的基础上做了改进,每次遍历列表时只做一次交换。选择排序在每次遍历时寻找最大值,并记录最大值的位置,最后再跟本趟最后一项交换。和冒泡排序一样,第一次遍历后,最大的元素就位;第二次遍历后,第二大的元素就位,依此类推。
效率:交换次数O(n),复杂度O(n2)
从头向尾 排序
# python实现
def selectSort2(alist2):
for fillslot in range(len(alist2)-1):
positionOfMin = len(alist2)-1
for location in range(fillslot, len(alist2)-1):
if alist2[location] < alist2[positionOfMin]:
positionOfMin = location
alist2[fillslot], alist2[positionOfMin]= alist2[positionOfMin], alist2[fillslot]
// java实现
public class SelectSort2 {
public static void main(String[] args) {
int[] selarr2 = {21, 23, 25, 27, 29, 22, 24, 26, 28};
for (int out = 0; out < selarr2.length-1; out++) {
int min = out;
for (int in = out+1; in < selarr2.length; in++)
if (selarr2[in] < selarr2[min])
min = in;
int temp = selarr2[out];
selarr2[out] = selarr2[min];
selarr2[min] = temp;
}
}
}
从尾向头 排序
# python实现
def selectionSort(alist):
for fillslot in range(len(alist)-1, 0, -1):
positionOfMax = 0
for location in range(1, fillslot+1):
if alist[location] > alist[positionOfMax]:
positionOfMax = location
alist[fillslot], alist[positionOfMax] = alist[positionOfMax], alist[fillslot]
// java实现
class SelectSort {
public static void main(String[] args) {
int[] selarr = {11, 13, 15, 17, 19, 12, 14, 16, 18};
for (int out = selarr.length-1; out > 1; out--) {
int max = out;
for (int in = 0; in < out; in++)
if (selarr[in] > selarr[max])
max = in;
int temp = selarr[out];
selarr[out] = selarr[max];
selarr[max] = temp;
}
}
}
插入排序
插入排序是在列表较低的一端维护一个有序的子列表,再逐个将新元素插入这个子列表。插入排序比前两种排序方法更高效。
效率:复杂度O(n2)
从头向尾 排序
# python实现
def insertionSort(alist):
for index in range(1, len(alist)):
currentvalue = alist[index]
position = index
while position > 0 and alist[position-1] > currentvalue:
alist[position] = alist[position-1]
position = position - 1
alist[position] = currentvalue
// java实现
public class InsertSort {
public static void main(String[] args) {
int[] insarr = {19, 15, 11, 17, 16, 13, 18, 14, 12};
for (int out = 1; out < insarr.length; out++) {
int temp = insarr[out];
int in = out;
while (in > 0 && insarr[in - 1] > temp) {
insarr[in] = insarr[in - 1];
in = in - 1;
}
insarr[in] = temp;
}
}
}
结果
15 19 11 17 16 13 18 14 12
11 15 19 17 16 13 18 14 12
11 15 17 19 16 13 18 14 12
11 15 16 17 19 13 18 14 12
11 13 15 16 17 19 18 14 12
11 13 15 16 17 18 19 14 12
11 13 14 15 16 17 18 19 12
11 12 13 14 15 16 17 18 19
从尾向头 排序
# python实现
def insertSort(alist2):
for index in range(len(alist2)-1, 0, -1):
currentvalue = alist2[index]
position = index
while position < len(alist2)-1 and alist2[position+1] < currentvalue:
alist2[position] = alist2[position+1]
position = position + 1
alist2[position] = currentvalue
// java实现
public class InsertSort2 {
public static void main(String[] args) {
int[] insarr2 = {19, 15, 11, 17, 16, 13, 18, 14, 12};
for (int out = insarr2.length - 1; out >= 0; out--) {
int temp = insarr2[out];
int in = out;
while (in < insarr2.length - 1 && insarr2[in + 1] < temp) {
insarr2[in] = insarr2[in + 1];
++in;
}
}
}
}
结果
19 15 11 17 16 13 18 12 14
19 15 11 17 16 13 12 14 18
19 15 11 17 16 12 13 14 18
19 15 11 17 12 13 14 16 18
19 15 11 12 13 14 16 17 18
19 15 11 12 13 14 16 17 18
19 11 12 13 14 15 16 17 18
11 12 13 14 15 16 17 18 19