1. Data structure classification
we divide data structures into logical structures and physical structures.
(1) Logical structure
- Set structure: the data elements in the set structure have no other relationship except that they belong to the same set.
- Linear structure: there is a one-to-one relationship between data elements in a linear structure
- Tree structure: there is a one to many hierarchical relationship between data elements in the tree structure
- Graphic structure: the data elements of the graphic structure are many to many relationships
(2) Physical structure
- Sequential storage structure: put data elements into storage units with continuous addresses. An array is a sequential storage structure.
- Linked list storage structure: data elements are stored in any storage unit. This group of storage units can be continuous or discontinuous. In the chain storage structure, a pointer is introduced to store the address of the data element, so that the location of the associated data element can be found through the address.
3. The goal of the algorithm
- Take the least time to complete the requirements
- Use the least memory space to meet the requirements
4. Rules of algorithm time complexity
- Constants in algorithm functions can be ignored
- The constant factor of the highest power in the algorithm function can be ignored
- The smaller the highest power in the algorithm function, the higher the efficiency of the algorithm
5. Large time complexity notation
- Replace all addition constants in the run time with constant 1
- In the modified run times, only high-order items are retained
- If the highest order term exists and the constant factor is not 1, the constant multiplied by this term is removed
(1) Linear order, time complexity O(n)
public static void main(String[] args) { int sum = 0; int n=100; for (int i = 1; i <= n; i++) { sum += i; //The loop body code was executed n times } System.out.println("sum=" + sum); }
(2) Square order, time complexity O(n^2)
public static void main(String[] args) { int sum=0,n=100; for (int i = 1; i <=n ; i++) { for (int j = 1; j <=n ; j++) { sum+=i; } } System.out.println(sum); }
(3) Cubic order, time complexity O(n^3)
public static void main(String[] args) { int x=0,n=100; for (int i = 1; i <=n ; i++) { for (int j = 1; j <=n ; j++) { for (int z = i; z <=n ; j++) { x++; } } } System.out.println(x); }
(4) Logarithmic order, time complexity O(logn)
for the logarithmic order, as the input scale n increases, regardless of the base, their growth trend is the same, so we will ignore the base.
public static void main(String[] args) { int i=1,n=100; while(i<n){ i = i*2; } }
(5) Constant order, time complexity O(1)
public static void main(String[] args) { int n=100; int i=n+2; System.out.println(i); }
The order of their complexity from low to high is O (1) < o (logn) < o (n) < o (nlogn) < o (n ^ 2) < o (n ^ 3)
6. Spatial complexity of the algorithm: generally not used
7. Sorting_ Comparable interface
you can sort the objects of all classes that implement the Comparable interface
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @AllArgsConstructor @Data @NoArgsConstructor public class Student implements Comparable<Student> { private String username; private int age; @Override public int compareTo(Student o) { return this.getAge()-o.getAge(); } }
public class TestComparable { public static void main(String[] args) { Student s1 = new Student("Zhang San",13); Student s2 = new Student("Li Si",23); Comparable max = getMax(s1, s2); System.out.println(max); } //Gets the larger of the two elements public static Comparable getMax(Comparable c1,Comparable c2) { int result = c1.compareTo(c2); //If result < 0, C1 < C2 //If result > 0, C1 > C2 //If result=0, c1=c2 if (result>=0) { return c1; }else { return c2; } } }
8. Sorting_ Bubble sorting
compare adjacent elements. If the previous element is larger than the latter, the positions of the two elements are exchanged.
import java.util.Arrays; public class Bubble { //Sort the elements in array a 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); } } } } //Compare whether the v element is larger than the w element private static boolean greater(Comparable v,Comparable w) { return v.compareTo(w)>0; } //Swap the values at index i and index j in the a array private static void exch(Comparable[] a,int i,int j) { Comparable temp; temp = a[i]; a[i] = a[j]; a[j] = temp; } public static void main(String[] args) { Integer[] a= new Integer[] {5,4,6,2,1}; sort(a); System.out.println(Arrays.toString(a)); } }
according to the big O derivation rule, the time complexity of bubble sorting is O(N^2)
9. Sorting_ Select sort
import java.util.Arrays; public class Selection { public static void sort(Comparable[] a) { for (int i=0;i<a.length-1;i++) { //Assuming this traversal, the index of the minimum value is i int minIndex=i; for (int j=i+1;j<a.length;j++) { if (greater(a[i],a[j])) { minIndex = j; } } exch(a,i,minIndex); } } private static boolean greater(Comparable v,Comparable w) { return v.compareTo(w)>0; } private static void exch(Comparable[] a,int i,int j) { Comparable temp; temp = a[i]; a[i] = a[j]; a[j] = temp; } public static void main(String[] args) { Integer[] arr = new Integer[] {7,6,4,5,3,2,1}; sort(arr); System.out.println(Arrays.toString(arr)); } }
according to the big O derivation rule, the time complexity of sorting is O(N^2)
10. Sorting_ Insert sort
divide all elements into two groups: sorted and unordered
import java.util.Arrays; public class Insertion { public static void sort(Comparable[] a) { for (int i=1;i<a.length;i++) { //i represents the element to be sorted 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 temp; temp = a[i]; a[i] = a[j]; a[j] = temp; } public static void main(String[] args) { Integer[] arr = new Integer[] {8,7,6,5,4,3,2,1}; sort(arr); System.out.println(Arrays.toString(arr)); } }
according to the big O derivation rule, the time complexity of insertion sorting is O(N^2)
11. Advanced sorting_ Shell Sort
Hill sort is an improved version of insert sort.
group the data first, and then insert and sort each group of data
determination of growth H: for each fixed rule of the value of growth h, we adopt the following rules:
int h=1; while(h<Length of array/2) { h=2*h+1; } //At the end of the cycle, we can determine the maximum value of h; // The reduction rule of h is: h=h/2;
import java.util.Arrays; public class Shell { public static void sort(Comparable[] a) { int h = 1; while (h<a.length/2) { //The initial value of the growth amount h is determined according to the length of array a h=2*h+1; } while (h>0) { for (int i=h;i<a.length;i++) { //Find the element to insert for (int j=i;j>=h;j-=h) { if (greater(a[j-h],a[j])) { exch(a,j-h,j); }else { break; } } } h/=2; } } private static boolean greater(Comparable v,Comparable w) { return v.compareTo(w)>0; } private static void exch(Comparable[] a,int i,int j) { Comparable temp; temp = a[i]; a[i] = a[j]; a[j] = temp; } public static void main(String[] args) { Integer[] arr = new Integer[] {9,8,7,6,6,5,4,3,2,1}; sort(arr); System.out.println(Arrays.toString(arr)); } }
according to the comparison of post analysis method, the performance of Hill sort is higher than that of insert sort
12. Sorting_ recursion
use recursion to complete the hierarchy of N
public class Test { public static void main(String[] args) { int result = factorial(4); System.out.println(result); } public static int factorial(int N) { if (N==1) { return 1; } return factorial(N-1)*N; } }