推荐网站:http://www.rmboot.com/
一、冒泡排序
1.1 API 设计
类名 | BubbleSort |
---|---|
构造方法 | BubbleSort():创建BubbleSort对象 |
成员方法 | 1.public static void sort(Comparable[] a):对数组内的元素进行排序 2.private static boolean greater(Comparable v,Comparable w):判断v是否大于w 3.private static void exch(Comparable[] a,int i,int j):交换a数组中,索引i和索引j处的值 |
1.2 实现
public class BubbleSort {
// 冒泡:相邻元素比较,前大于后则交换,升序
public BubbleSort() {
}
// 对传入的数组冒泡排序
public static void sort(Comparable[] a) {
int tmpIndex = 0;
while (tmpIndex < a.length){
for (int i = 0; i < a.length - 1; i++) {
if (greater(a[i], a[i+1])){
exch(a, i, i+1);
}
}
tmpIndex++;
}
}
// 比较v元素是否大于w元素
private static boolean greater(Comparable v, Comparable w) {
return v.compareTo(w) > 0;
}
// 交换
private static void exch(Comparable[] a, int i, int j){
Comparable t = a[i];
a[i] = a[j];
a[j] = t;
}
}
1.3 测试
public class BubbleSortTest {
public static void main(String[] args) {
String[] arr = {
"aa","bb","mm","zz","dd","cc"};
BubbleSort.sort(arr);
for (String s : arr) {
System.out.print(s+" ");
}
}
}
二、选择排序
1.每一次遍历的过程中,都假定第一个索引处的元素是最小值,和其他索引处的值依次进行比较,如果当前索引处的值大于其他某个索引处的值,则假定其他某个索引出的值为最小值,最后可以找到最小值所在的索引
2.交换第一个索引处和最小值所在的索引处的值
2.1 API设计
类名 | SelectSort |
---|---|
构造方法 | SelectSort():创建SelectSort对象 |
成员方法 | 1.public static void sort(Comparable[] a):对数组内的元素进行排序 2.private static boolean greater(Comparable v,Comparable w):判断v是否大于w 3.private static void exch(Comparable[] a,int i,int j):交换a数组中,索引i和索引j处的值 |
2.2 实现
public class SelectSort {
// 选择排序:每一次遍历都假设第一个索引处的元素是最小值,与后面的元素依次比较,前大于后则交换,升序
public SelectSort() {
}
// 对数组a中的元素进行选择排序
public static void sort(Comparable[] a) {
for (int i = 0; i < a.length; i++) {
int minIndex = i;
for (int j = i+1; j < a.length; j++) {
if (greater(a[minIndex],a[j])){
minIndex = j;
}
}
exch(a,minIndex,i);
}
}
// 比较v元素是否大于w元素
private static boolean greater(Comparable v, Comparable w) {
return v.compareTo(w) > 0;
}
// 交换
private static void exch(Comparable[] a, int i, int j){
Comparable t = a[i];
a[i] = a[j];
a[j] = t;
}
}
2.3 测试
public class SelectSortTest {
public static void main(String[] args) {
String[] arr = {
"aa","bb","mm","zz","dd","cc"};
SelectSort.sort(arr);
for (String s : arr) {
System.out.print(s+" ");
}
}
}
三、插入排序
3.1 API设计
类名 | InsertSort |
---|---|
构造方法 | InsertSort():创建InsertSort对象 |
成员方法 | 1.public static void sort(Comparable[] a):对数组内的元素进行排序 2.private static boolean greater(Comparable v,Comparable w):判断v是否大于w 3.private static void exch(Comparable[] a,int i,int j):交换a数组中,索引i和索引j处的值 |
3.2 实现
public class InsertSort {
// 插入排序:一个有序数组,一个无序,
// 将无序数组中的首个元素与有序数组比较,类似于冒泡方式插入到有序数组中
public static void sort(Comparable[] a) {
for (int i = 1; i < a.length; i++) {
//当前元素为a[i],依次和i前面的元素比较,找到一个小于等于a[i]的元素
for (int j = i; j > 0; j--) {
if (greater(a[j-1], a[j])){
exch(a,j-1,j);
}else {
//找到了该元素,结束
break;
}
}
}
}
private static boolean greater(Comparable v, Comparable w) {
return v.compareTo(w) > 0;
}
private static void exch(Comparable[] a, int i, int j){
Comparable t = a[i];
a[i] = a[j];
a[j] = t;
}
}
3.3 测试
public class InsertSortTest {
public static void main(String[] args) {
String[] arr = {
"aa","bb","mm","zz","dd","cc"};
InsertSort.sort(arr);
for (String s : arr) {
System.out.print(s+" ");
}
}
}