前言:在Java的开发工具包JDK中,已经提供了很多数据结构与算法的实现,比如List,Set,Map,Math等等,都是以API的方式提供,这种方式的好处在于一次编写,多处使用。我们应该借鉴JDK的方式,也把算法封装到某个类中,如果是这样,在我们写Java代码之前,就需要先进行API的设计,设计好之后,再对这些API进行实现。
1、简单的排序
Comparable接口介绍
-
Java提供了一个Comparable接口来定义排序规则
-
Comparable接口使用
package sort;
// 定义一个学生类,具有userName和age属性,并通过Comparable接口提供比较规则
public class Student implements Comparable<Student>{
private String userName;
private int age;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"userName='" + userName + '\'' +
", age=" + age +
'}';
}
// 比较规则
@Override
public int compareTo(Student o) {
return this.getAge() - o.getAge();
}
}
package test;
import sort.Student;
// 定义测试类Test,在测试类中定义测试方法Comparable getMap(Comparable c1, Comparable c2)完成测试
public class TestComparable {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
s1.setAge(18);
s1.setUserName("张三");
s2.setAge(20);
s2.setUserName("李四");
System.out.println(getMap(s1, s2));
}
public static Comparable getMap(Comparable c1, Comparable c2){
int result = c1.compareTo(c2);
// c1 > c2,result > 0;
// c1 = c2,result == 0;
// c1 < c2,result < 0;
if (result >= 0){
return c1;
} else {
return c2;
}
}
}
2、冒泡排序
- 排序原理
1、比较相邻的元素。如果前一个元素比后一个元素大,就交换这两个元素的位置。
2、对每一对相邻的元素做同样的工作,从开始第一对元素到结尾的最后一对元素。最终最终最后位置的元素就是最大值。
- 冒泡排序API设计
类名 | Bubble |
---|---|
构造方法 | Bubble():创建Bubble对象 |
成员方法 | 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处的值 |
- 冒泡排序代码实现
package com.wgh.sort;
// 冒泡排序
public class Bubble {
/*
对数组元素进行排序
*/
public static void sort(Comparable[] a){
for (int i = a.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if(greater(a[j], a[j+1])){
exch(a, j, j+1);
}
}
}
}
/*
判断v是否大于w
*/
private static boolean greater(Comparable v,Comparable w){
return v.compareTo(w) > 0;
}
/*
交换a数组中,索引i和索引j处的值
*/
private static void exch(Comparable[] a,int i,int j){
Comparable temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
// 测试代码
package com.wgh.test;
import com.wgh.sort.Bubble;
import java.util.Arrays;
public class BubbleTest {
public static void main(String[] args) {
Integer[] arr = {4, 5, 6, 3, 2, 1};
Bubble.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
- 冒泡排序时间复杂度:N^2 - N, 即O(N^2)
3、选择排序
- 排序原理
1、每一次遍历的过程中,都假定第一个索引处的元素是最小值,和其他索引处的值一次进行比较,如果当前索引处的值大于其他索引处的值,则假定其他某个索引处的值为最小值,最后可以找到最小值所在的索引
2、交换第一个索引处和最小值所在的索引处的值
- 选择排序的API设计
类名 | Selection |
---|---|
构造方法 | Selection():创建Selection对象 |
成员方法 | 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处的值 |
- 选择排序代码实现
package com.wgh.sort;
public class Selection {
/*
对数组元素进行排序
*/
public static void sort(Comparable[] a){
for (int i = 0; i <= a.length - 2; i++){
// 定义一个变量,记录最小元素所在的索引,默认为参与选择排序的第一个元素所在的位置
int minIndex = i;
for (int j = i+1; j < a.length; j++) {
// 需要将最小索引处的值和j索引处的值进行比较;
// 如果最小索引处的值比a[j]大,将j的索引赋给minIndex;
if(greater(a[minIndex], a[j])){
minIndex = j;
}
}
// 将最小索引minIndex处的值与i索引处的值做交换
if(i != minIndex){
exch(a, i, minIndex);
}
}
}
/*
判断v是否大于w
*/
private static boolean greater(Comparable v,Comparable w){
return v.compareTo(w) > 0;
}
/*
交换a数组中,索引i和索引j处的值
*/
private static void exch(Comparable[] a,int i,int j){
Comparable temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
package com.wgh.test;
import com.wgh.sort.Selection;
import java.util.Arrays;
public class SelectionTest {
public static void main(String[] args) {
Integer[] a = {10 ,20 , 12, 22, 1, 3, 23, 19};
Selection.sort(a);
System.out.println(Arrays.toString(a));
}
}
- 选择排序时间复杂度:N^2/2 + N/2 - 1, 即O(N^2)
4、插入排序
- 排序原理
1、把所有的元素分为两组,已经排序和未排序的;
2、找到未排序的组中的第一个元素,向已经排序的组中进行插入;
3、倒叙遍历已经排序的元素,依次和待排序的元素进行比较,直到找到一个元素<=待插入元素,那么就把待插入元素放到这个位置,其他的元素向后移动一位;
- 插入排序的API设计
类名 | Insertion |
---|---|
构造方法 | Insertion():创建Insertion对象 |
成员方法 | 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处的值 |
- 插入排序代码实现
package com.wgh.sort;
public class Insertion {
/*
对数组元素进行排序
*/
public static void sort(Comparable[] a){
// 共需要对a.length - 1个数进行插入排序
for (int i = 1; i < a.length; i++) {
// 与i之前的所有数进行比较
for (int j = i; j > 0; j--) {
// 比较j索引处的值和j-1索引处的值,如果j-1的大于j的,则交换位置
if(greater(a[j-1], a[j])){
exch(a, j, j-1);
}else{
break;
}
}
}
}
/*
判断v是否大于w
*/
private static boolean greater(Comparable v,Comparable w){
return v.compareTo(w) > 0;
}
/*
交换a数组中,索引i和索引j处的值
*/
private static void exch(Comparable[] a,int i,int j){
Comparable temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
package com.wgh.test;
import com.wgh.sort.Insertion;
import java.util.Arrays;
public class InsertTest {
public static void main(String[] args) {
Integer[] a = {4, 3, 2, 10, 12, 1, 5, 6};
Insertion.sort(a);
System.out.println(Arrays.toString(a));
}
}
- 插入排序时间复杂度 : N^2 - N,即O(N^2)