1.排序的概念
(1).将一组数据按相应的规则排列顺序。
2.冒泡排序的实现
实现思路:
- 分析:int arr[] = {2, 5, 3, 6, 9};
- 假设数组的长度为为5,即arr.length=5,
- 第一次元素交换的次数为4 ,第一趟 j=0 0+4
- 第二次元素交换的次数为3 , 第二趟 j=1 1+3
- 第三次元素交换的次数为2 ,第三趟 j=2 2+2
- 第四次元素交换的次数为1 第四趟 j=3 1+3
- 趟数就等于arr.length-1
- 循环的次数:arr.length-j-1 (j为趟数) 具体代码实现,如下
public static void sortFinal(int[] arr) {
boolean flag = true; //标示减少循环的趟数
for (int j = 0; j < arr.length - 1; j++) { //循环的趟数
System.out.println("第"+(j+1)+"趟");
flag = true;
for (int i = 0; i < arr.length - 1 - j; i++) { //循环的次数
System.out.print("第"+(i+1)+"次");
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
flag = false;
}
System.out.println(Arrays.toString(arr));
}
if (flag) {
break;
}
}
}
3.内置引用数据类型的排序
内置类实现排序都是实现:java.lang.Comparable---->compareTo()
举例:Integer,String 底层实现
public int compareTo(Integer anotherInteger) {
return compare(this.value, anotherInteger.value);
}
public static int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
public int compareTo(String anotherString) {
int len1 = value.length;
int len2 = anotherString.value.length;
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
}
Integer(比较的是基本数据类型的大小),Character (比较的是Unicode码之差),Date(时间的大小) 。
String比较分为两种情况:
第一点:其中一个是另一个起始的字符窜,返回两个字符窜的长度差
第二点:否则就是返回的第一个不相等的Unicode的之差。
public static void InnerTypeTest(){
Integer integer=5;
System.out.println(integer.compareTo(5)); //比较的是基本数据类型大小
Character character='c';
System.out.println(character.compareTo('c')); //比较的Unicode码只差
//字符窜比较两种情况:
//其中一个是另一个起始的字符窜,返回两个字符窜的长度差
//否则返回Unicode之差
String str="abc"; //字符窜比较
System.out.println(str.compareTo("abcd")); //返回-1
System.out.println(str.compareTo("ccd")); //返回Unicode的码之差
Date date=new Date();
System.out.println(date.compareTo(new Date())); //比较时间大小
}
4.自定义实现数组和集合的排序
public class Utils {
public static void main(String[] args) {
String[] arrStr = {"ab", "rg", "e", "a", "abcd", "abc", "def"};
sort(Arrays.asList(arrStr), new StringCom());
System.out.println(Arrays.toString(arrStr));
}
/**
* 自定义排序规则实现对集合的排序
* @param list
* @param com
* @param <T>
*/
public static <T> void sort(List<T> list, Comparator<T> com) {
Object[] arr = list.toArray();
sort(arr, com);
for (int i = 0; i < arr.length; i++) {
list.set(i, (T) arr[i]);
}
}
/**
* 自定义排序规则实现对数组的排序
* @param <T>
*/
public static <T> void sort(Object[] arr, Comparator<T> com) {
Boolean flag = true;
for (int j = 0; j < arr.length - 1; j++) {
flag = true;
for (int i = 0; i < arr.length - 1 - j; i++) {
if (com.compare((T) arr[i], (T) arr[i + 1]) < 0) {
Object temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
flag = false;
}
}
if (flag) {
break;
}
}
}
/**
* 容器排序
* @param list
* @param <T>
*/
public static <T extends Comparable<T>> void sort(List<T> list) {
Object[] arr = list.toArray();
sort(arr);
for (int i = 0; i < arr.length; i++) {
list.set(i, (T) arr[i]);
}
}
/**
* 泛型数组排序
* @param arr
*/
public static <T extends Comparable<T>> void sort(T[] arr) {
Boolean flag = true;
for (int j = 0; j < arr.length - 1; j++) {
flag = true;
for (int i = 0; i < arr.length - 1 - j; i++) {
if (((Comparable) arr[i]).compareTo(arr[i + 1]) > 0) {
T temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
flag = false;
}
}
if (flag) {
break;
}
}
}
/**
* 数组排序
*/
public static void sort(Object[] arr) {
Boolean flag = true;
for (int j = 0; j < arr.length - 1; j++) {
flag = true;
for (int i = 0; i < arr.length - 1 - j; i++) {
if (((Comparable) arr[i]).compareTo(arr[i + 1]) > 0) {
Object temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
flag = false;
}
}
if (flag) {
break;
}
}
}
}
自定义规则类 StringCom
public class StringCom implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
int len1=o1.length();
int len2=o2.length();
return -(len1-len2);
}
}
5.TreeSet和TreeMap 实现排序
- Person类 (调用TreeSet和TreeMap无参构造)
public class Person implements Comparable<Person> {
private String name;
private String password;
public Person() {
}
public Person(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int compareTo(Person o) {
return -(this.password.length()-o.password.length());
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
测试方法
/**
* 使用TreeSet的空构造的方法实现排序,需要在自定义的类中实现java.lang.Comparable--->compareTo
* 的方法,然后自定义相应的排序规则
*/
public static void langComparableTest(){
Person p1=new Person("张三","123");
Person p2=new Person("张四","1234");
Person p3=new Person("张五","12345");
Person p4=new Person("张六","123456");
TreeSet<Person> treeSet=new TreeSet<Person>();
treeSet.add(p1);
treeSet.add(p2);
treeSet.add(p3);
treeSet.add(p4);
System.out.println(treeSet);
}
/**
* TreeMap类似TreeSet ,无参构造
*/
public static void langComparableTreeMapTest(){
Person p1=new Person("张三","123");
Person p2=new Person("张四","1234");
Person p3=new Person("张五","12345");
Person p4=new Person("张六","123456");
TreeMap<Person,String> treeMap=new TreeMap<Person,String>();
treeMap.put(p1,"adad");
treeMap.put(p2,"adad");
treeMap.put(p3,"adad");
treeMap.put(p4,"adad");
System.out.println(treeMap.keySet());
}
Uers(调用TreeSet和TreeMap有参构造)
public class User {
private String name;
private String passwprd;
public User() {
}
public User(String name, String passwprd) {
this.name = name;
this.passwprd = passwprd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPasswprd() {
return passwprd;
}
public void setPasswprd(String passwprd) {
this.passwprd = passwprd;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", passwprd='" + passwprd + '\'' +
'}';
}
}
测试方法
/**
* 使用TreeSet的有参构造的方法实现排序,需要在自定义的类中实现java.util.Comparator--->compare
*/
public static void UtilComparatorTest(){
User u1=new User("张三","123");
User u2=new User("张四","1234");
User u3=new User("张五","12345");
User u4=new User("张六","123456");
TreeSet<User> treeSet=new TreeSet<User>(new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
return -(o1.getPasswprd().length()-o2.getPasswprd().length());
}
});
treeSet.add(u1);
treeSet.add(u2);
treeSet.add(u3);
treeSet.add(u4);
System.out.println(treeSet);
}
/**
* TreeMap类似TreeSet 有参构造
*/
public static void UtilComparatorTreeMapTest(){
User u1=new User("张三","123");
User u2=new User("张四","1234");
User u3=new User("张五","12345");
User u4=new User("张六","123456");
TreeMap<User,String> treeMap=new TreeMap<User,String>(new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
return -(o1.getPasswprd().length()-o2.getPasswprd().length());
}
});
treeMap.put(u1,"adad");
treeMap.put(u2,"adad");
treeMap.put(u3,"adad");
treeMap.put(u4,"adad");
System.out.println(treeMap.keySet());
}
6.Collections工具 类排序
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("ab");
list.add("abcd");
list.add("abc");
//默认规则的排序
Collections.sort(list);
for (String str:list){
System.out.println(str);
}
//自定义规则的排序
Collections.sort(list,new StringCom());
for (String str:list){
System.out.println(str);
}
}
每天进步一丢地
完成