//从小到大
public static void main(String[] args) {
int [] a = {8,6,5,9,11,2,4};
kuaipai(a);
for(int i = 0 ;i<a.length;i++){
System.out.println(a[i]);
}
}
//交换两个数
public static void swap(int[] a , int i , int j){
int t = a[i];
a[i] = a[j];
a[j] = t;
}
Bubble Sort
Every election one of the biggest bubbles into the back
Improvement of a bubble sort is: adding a flag variable exchange, if a trip is not exchanging data, then that is already sorted
public static void maopao(int[] a){
int len = a.length;
for(int i = 0;i<len-1;i++){
for(int j = 0;j<len-i-1;j++){
if(a[j]>a[j+1]) swap(a,j,j+1);
}
}
}
Selection Sort
From behind the smallest elected, to the front
public static void xuanze(int[] a){
int len = a.length;
for(int i = 0;i<len-1;i++){
for(int j = i+1;j<len;j++){
if(a[j]<a[i]) swap(a,i,j);
}
}
}
Direct insertion sort
Inserted before the current number of the ordered array
public static void zhijiecharu(int[] a){
int len = a.length;
for(int i = 1;i<len;i++){
for(int j = i;j>0 && a[j]<a[j-1];j--){
swap(a,j,j-1);
}
}
}
Merge sort
public static void guibing(int[] a){
int len = a.length;
mergesort(a,0,len-1);
}
public static void mergesort(int[] a , int start ,int end){
if(start<end){
int mid = (start+end)/2;
mergesort(a,start,mid);
mergesort(a,mid+1,end);
mergePaihaoxu(a,start,mid,mid+1,end);
}
}
public static void mergePaihaoxu(int[] a, int start1 , int end1 , int start2 ,int end2){
int i = start1;
int j = start2;
int [] t = new int[end2-start1+1];
int k = 0;
while (i<=end1 && j<=end2){
if(a[i]<a[j]){
t[k++] = a[i++];
}
else t[k++] = a[j++];
}
if(i<=end1) t[k++] = a[i++];
if(j<=end2) t[k++] = a[j++];
for(int m = 0;m<t.length;m++){
a[m+start1]=t[m];
}
}
Fast row
Unstable : Because our keyword comparison and exchange are jumping conducted, therefore, quick sort is an unstable sorting method.
Time complexity, preferably O (nlogn), the average O (nlogn),
Worst O (n * n), the time when the data is almost the worst order, then back to a bubbling space complexity O (nlogn)
On the spatial complexity, the use of the stack space is mainly caused by recursive, preferably, the depth of the tree is recursively log2n, the spatial complexity is also O (logn), the worst case, require recursion n-1 call, the spatial complexity is O (n), the average case space complexity is also O (logn).
There is error-prone points
public static void kuaipai(int [] a){
int len = a.length;
qsort(a, 0 , len -1);
}
public static void qsort(int[] a,int i , int j){
int pos;
if(i<j){
pos = partition(a,i,j);
qsort(a,i,pos-1);
qsort(a,pos+1,j);
}
}
public static int partition(int[] a , int low , int high){
//选择第一个元素作为基准
int key = a[low];
int i = low;
int j = high;
while (i<j){
while (a[i]<=key && i<j) i++;
//swap(a,i,j);
while (a[j]>=key && i<j) j--;
swap(a,i,j);
}
if(a[i]>a[low]) i--;//易错
swap(a,low,i);
return i;
}
The initial elements are not affected Sort by: Select, heap, merge
Arrays.sort the basic data types quick sort, to sort objects Improved merge array
Collection.sort (List <T> list) by natural ordering, comparability element must have realized the interface comaprable
Collection.sort(List<T> list , comparator<q super T>)
Excerpt - see blog: https://blog.csdn.net/qq_23179075/article/details/78753136
Map
, Set
, List
And other collections. They have put a total of a sorting method sort()
to sort the data directly using this method on the line, but to ensure that objects in the collection are comparable .
How to make an object is comparable , it would need to achieve the object Comparable<T>
interfaces friends. Then override inside compareTo()
method. We can see that many classes are Java class that implements this interface such as: Integer
, Long
and so on. . .
Suppose we have a class of students required by default according to the student's age field age
sort code is as follows:
public class Student implements Comparable<Student> {
private int id;
private int age;
private String name;
public Student(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
@Override
public int compareTo(Student o) {
//降序
//return o.age - this.age;
//升序
return this.age - o.age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
We said here about the rewrite of public int compareTo(Student o){}
this method, which returns three kinds of int
value types: negative integer , zero , positive integer .
return value | meaning |
---|---|
Negative integer | The current value of the object < value comparison object, the position of the top |
zero | The current value of the object = value comparison object, the same position |
Positive integer | The current value of the object > value comparison object, the position in the bottom |
Test code:
public static void main(String args[]){
List<Student> list = new ArrayList<>();
list.add(new Student(1,25,"关羽"));
list.add(new Student(2,21,"张飞"));
list.add(new Student(3,18,"刘备"));
list.add(new Student(4,32,"袁绍"));
list.add(new Student(5,36,"赵云"));
list.add(new Student(6,16,"曹操"));
System.out.println("排序前:");
for (Student student : list) {
System.out.println(student.toString());
}
//使用默认排序
Collections.sort(list);
System.out.println("默认排序后:");
for (Student student : list) {
System.out.println(student.toString());
}
}
Output log:
排序前:
Student{id=1, age=25, name='关羽'}
Student{id=2, age=21, name='张飞'}
Student{id=3, age=18, name='刘备'}
Student{id=4, age=32, name='袁绍'}
Student{id=5, age=36, name='赵云'}
Student{id=6, age=16, name='曹操'}
默认排序后:
Student{id=6, age=16, name='曹操'}
Student{id=3, age=18, name='刘备'}
Student{id=2, age=21, name='张飞'}
Student{id=1, age=25, name='关羽'}
Student{id=4, age=32, name='袁绍'}
Student{id=5, age=36, name='赵云'}
Using the comparator
This time demand again, the default is to sort by age, but sometimes need id to sort how to do? This time comparator: Comparator on rafts handy.
Comparator is used in two ways:
Collections.sort(list,Comparator<T>);
list.sort(Comparator<T>);
In fact, the key is how to achieve Comparator interface, which rewrites the compare method. code show as below:
//自定义排序1
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getId() - o2.getId();
}
});
compare(Student o1, Student o2)
Returns the value of the method with the Comparable<>
interface compareTo(Student o)
method return value of the same meaning. Another way as follows:
//自定义排序2
list.sort(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getId() - o2.getId();
}
});
Output log:
排序前:
Student{id=1, age=25, name='关羽'}
Student{id=2, age=21, name='张飞'}
Student{id=3, age=18, name='刘备'}
Student{id=4, age=32, name='袁绍'}
Student{id=5, age=36, name='赵云'}
Student{id=6, age=16, name='曹操'}
自定义排序后:
Student{id=1, age=25, name='关羽'}
Student{id=2, age=21, name='张飞'}
Student{id=3, age=18, name='刘备'}
Student{id=4, age=32, name='袁绍'}
Student{id=5, age=36, name='赵云'}
Student{id=6, age=16, name='曹操'}