冒泡排序&选择排序&插入排序

冒泡排序

冒泡排序(Bubble Sort)是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端。

冒泡排序算法的运作如下:

  • 比较相邻的元素。如果第一个比第二个大,就交换他们两个。
  • 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。这步做完后,最后的元素会是最大的数。
  • 针对所有的元素重复以上的步骤,除了最后一个。
  • 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。

lua的实现方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
a = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48}
function ()
for i=#a-1, 1, -1 do
for j=1, i do
if a[j] > a[j+1] then
a[j], a[j+1] = a[j+1], a[j]
end
end
end
for i,v in ipairs(a) do
print(v)
end
end
sort()

java的实现方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class BubbleSort
{
public static void (int[] array)
{
for (int i = array.length - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (array[j] > array[j + 1])
{
swap(array, j, j + 1);
}
}
}
}

private static void swap(int[] array, int j, int i)
{
int temp = array[j];
array[j] = array[i];
array[i] = temp;
}

public static void main(String[] args)
{
int[] array = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48};
sort(array);
System.out.println(Arrays.toString(array));
}
}

选择排序

选择排序(Selection sort)是一种简单直观的排序算法。它的工作原理如下。首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。

lua的实现方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
a = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48}
function ()
for i=1,#a do
local minIndex = i
for j=i,#a do
if a[minIndex] > a[j] then
minIndex = j
end
end
if minIndex ~= i then
a[i], a[minIndex] = a[minIndex], a[i]
end
end

for i,v in ipairs(a) do
print(v)
end
end
sort()

java的实现方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class SelectionSort
{
public static void (int[] array)
{
for (int i = 0; i < array.length; i++)
{
int minIndex = i;
for (int j = i; j < array.length; j++)
{
if (array[minIndex] > array[j])
{
minIndex = j;
}
}
if (minIndex != i)
{
swap(array, i, minIndex);
}
}
}

private static void swap(int[] array, int j, int i)
{
int temp = array[j];
array[j] = array[i];
array[i] = temp;
}

public static void main(String[] args)
{
int[] array = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48};
sort(array);
System.out.println(Arrays.toString(array));
}
}

插入排序

插入排序(Insertion Sort)的算法描述是一种简单直观的排序算法。它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。插入排序在实现上,通常采用in-place排序(即只需用到O(1)的额外空间的排序),因而在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,为最新元素提供插入空间。

一般来说,插入排序都采用in-place在数组上实现。具体算法描述如下:

  1. 从第一个元素开始,该元素可以认为已经被排序
  2. 取出下一个元素,在已经排序的元素序列中从后向前扫描
  3. 如果该元素(已排序)大于新元素,将该元素移到下一位置
  4. 重复步骤3,直到找到已排序的元素小于或者等于新元素的位置
  5. 将新元素插入到该位置后
    重复步骤2~5

lua的实现方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
a = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48}
function ()
for i=2,#a do
for j=i,2,-1 do
if a[j] < a[j-1] then
a[j], a[j-1] = a[j-1], a[j]
else
break;
end
end
end

for i,v in ipairs(a) do
print(v)
end
end
sort()

java的实现方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class InsertionSort
{
public static void sort(int[] array)
{
for (int i = 1; i < array.length; i++)
{
for (int j = i; j > 0; j--)
{
if (array[j] < array[j - 1])
{
swap(array, j, j - 1);
}
}
}
}

private static void swap(int[] array, int j, int i)
{
int temp = array[j];
array[j] = array[i];
array[i] = temp;
}

public static void main(String[] args)
{
int[] array =
{ 3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48 };
sort(array);
System.out.println(Arrays.toString(array));
}
}

—EOF—