5、冒泡排序
(1)基本思想:
在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒。即:每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换。
(2)实例:
(3)用java实现
public class bubbleSort { public bubbleSort() { int[] a = { 1, 36, 48, 97, 83, 27, 34, 65, 72, 16 }; int temp = 0; for (int i = 0; i < a.length - 1; i++) { for (int j = 0; j < a.length - 1 - i; j++) { if (a[j] > a[j + 1]) { temp = a[j + 1]; a[j + 1] = a[j]; a[j] = temp; } } } for (int i = 0; i < a.length; i++) System.out.print(a[i] + " "); } public static void main(String[] args) { bubbleSort bs = new bubbleSort(); } }
6、快速排序
(1)基本思想:
选择一个基准元素,通常选择第一个元素或者最后一个元素,通过一趟扫描,将待排序列分成两部分,一部分比基准元素小,一部分大于等于基准元素,此时基准元素在其排好序后的正确位置,然后再用同样的方法递归地排序划分的两部分。
(2)实例:
(3)用java实现
public class quickSort { static int[] a = { 1, 36, 48, 76, 49, 97, 82, 27, 67, 71 }; public quickSort() { quick(a); for (int i = 0; i < a.length; i++) System.out.print(a[i] + " "); } public void quick(int[] a2) {// 查看数组是否为空 if (a2.length > 0) { quickSortTest(a2, 0, a2.length - 1); } } public void quickSortTest(int[] list, int low, int high) { if (low < high) { int middle = getMiddle(list, low, high);// 将list数组进行一分为二 quickSortTest(list, low, middle - 1);// 对低字表进行递归排序 quickSortTest(list, middle + 1, high);// 对高字表进行递归排序 } } public int getMiddle(int[] list, int low, int high) { int temp = list[low];// 数组的第一个作为中轴 while (low < high) { while (low < high && temp < list[high]) high--; list[low] = list[high];// 比中轴小的记录移到低端 while (low < high && list[low] < temp) low++; list[high] = list[low];// 比中轴大的记录移到高端 } list[low] = temp;// 中轴记录到尾 return low;// 返回中轴的位置 } public static void main(String[] args) { System.out.println("原数组为:"); for (int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println(); System.out.println("快速排序后数组为:"); quickSort qs = new quickSort(); } }
7、归并排序
(1)基本排序:
归并(Merge)排序法是将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列分为若干个子序列,每个子序列是有序的。然后再把有序子序列合并为整体有序序列。
(2)实例:
(3)用java实现
public
class
mergingSort {
int
a[]={
49
,
38
,
65
,
97
,
76
,
13
,
27
,
49
,
78
,
34
,
12
,
64
,
5
,
4
,
62
,
99
,
98
,
54
,
56
,
17
,
18
,
23
,
34
,
15
,
35
,
25
,
53
,
51
};
public
mergingSort(){
sort(a,
0
,a.length-
1
);
for
(
int
i=
0
;i<a.length;i++)
System.out.println(a[i]);
}
public
void
sort(
int
[] data,
int
left,
int
right) {
// TODO Auto-generated method stub
if
(left<right){
//找出中间索引
int
center=(left+right)/
2
;
//对左边数组进行递归
sort(data,left,center);
//对右边数组进行递归
sort(data,center+
1
,right);
//合并
merge(data,left,center,right);
}
}
public
void
merge(
int
[] data,
int
left,
int
center,
int
right) {
// TODO Auto-generated method stub
int
[] tmpArr=
new
int
[data.length];
int
mid=center+
1
;
//third记录中间数组的索引
int
third=left;
int
tmp=left;
while
(left<=center&&mid<=right){
//从两个数组中取出最小的放入中间数组
if
(data[left]<=data[mid]){
tmpArr[third++]=data[left++];
}
else
{
tmpArr[third++]=data[mid++];
}
}
//剩余部分依次放入中间数组
while
(mid<=right){
tmpArr[third++]=data[mid++];
}
while
(left<=center){
tmpArr[third++]=data[left++];
}
//将中间数组中的内容复制回原数组
while
(tmp<=right){
data[tmp]=tmpArr[tmp++];
}
System.out.println(Arrays.toString(data));
}
}