Table of contents
Comparison of Three Algorithms
algorithm
Algorithm overview
Algorithms are series of program instructions used to solve specific arithmetic and logic problems
It can also be understood as inputting a data and obtaining the required output results through the algorithm
algorithmic complexity
There are good and bad algorithms, which are distinguished by time complexity and space complexity.
Time Complexity: It is a measure of the running time of an algorithm, represented by big O.
1. The common time complexity is sorted from low to high, including: O(1) < O(logn) < O(n) <O(nlogn) < O(n2), etc.
2. Differences in different time complexities
3. Space complexity: refers to the measurement of the size of the memory space temporarily occupied by an algorithm during the running process , represented by big O.
Many times, we have to make a trade-off between time and space complexity. Sometimes we need to sacrifice time for space, sometimes
Sometimes we need to sacrifice space for time. Most of the time, the time complexity is more important, and the storage space can be expanded if the storage space is insufficient.
Charge, but the execution speed of the program must be put in the first place .
data structure
Overview of Data Structures
Data structure is the organization, management and storage format of data, and its purpose is to modify and access data efficiently.
Data structure can be divided into physical structure and logical structure
physical structure
The physical structure is the real storage method in the computer, mainly including arrays and linked lists.
1. Array storage is to store data in continuous memory space:
2. Linked list storage can store data anywhere in the memory, and each is realized by a pointer pointing to the next element
logical structure
The logical structure is a model abstracted from specific problems, and it is a structure in the abstract sense, which can be defined according to the relationship between elements in the object
Departments are divided into two categories.
Common logical structures are as follows:
1. Linear structure (arranged in order)
2. Tree structure (hierarchy)
Note: Each element has only one parent element.
3. Graph structure (network structure)
Any logical structure is implemented through two physical structures, array and linked list . In the process of writing code, Wildfire designs some abstract data types (Abstract Data Type) to complete operations such as adding, deleting, and traversing.
simple sort
1. Selection sort
1.1 Algorithm description
First find the smallest (largest) element in the unsorted sequence and store it at the beginning of the sorted sequence,
Then, continue to find the smallest (largest) element from the remaining sorted elements, and then put it at the end of the sorted sequence.
And so on until all elements are sorted.
1.2 Algorithm implementation
Decomposition 1: Find the smallest element from an unsorted array
Decomposition 2: Exchange the minimum value with the first element of the unsorted array
Decomposition 3: Repeat the operations of Decomposition 1 and Decomposition 2 for the unsorted array, each time the starting position of the array is increased, and the sorting is completed.
Decomposition 4: Use loop operation instead of Start
Practice:
package SelectionSort;
public class SelectionSort001 {
public static void main(String[] args) {
int[] arrs ={8,6,1,7,2,5,4,12,9};
//存储最小值所在的位置
int minPos = 0;
//遍历数组找到最小的数字
for(int i = 0;i <arrs.length;i++){
if(arrs[minPos] > arrs[i]){
//保存最小值所在的位置
minPos = i ;
}
}
System.out.println("最小的数字为"+arrs[minPos]);
//交换
int temp = arrs[0];
arrs[0] = arrs[minPos];
arrs[minPos] = temp;
//输出结果
for(int i = 0; i < arrs.length;i++){
System.out.println(arrs[i]+",");
}
}
}
Put the smallest/largest element in the array first, find the index it is in, swap the first element to its place.
In the second round, you need to change the starting position of the loop, and set minPos=1;
We can summarize the rules:
package SelectionSort;
public class SelectionSort001 {
public static void main(String[] args) {
int[] arrs ={8,6,1,7,2,5,4,12,9};
for(int j = 0; j < arrs.length ; j++){
int minPos = j;
//遍历数组找到最小的数字
for(int i = j+1; i <arrs.length; i++){
if(arrs[minPos] > arrs[i]){
//保存最小值所在的位置
minPos = i ;
}
}
System.out.println("最小的数字为"+arrs[minPos]);
//交换
int temp = arrs[j];
arrs[j] = arrs[minPos];
arrs[minPos] = temp;
}
//输出结果
for(int i = 0; i < arrs.length;i++){
System.out.println(arrs[i]+",");
}
}
}
2 bubble sort
2.1 Algorithm description
First start at the first position of the unsorted array, compare it with the next adjacent numbers, and exchange if the previous one is larger than the latter one.
Next, compare the number in the second position with the next adjacent number. If it is larger, then exchange it until the largest number is exchanged to the end of the array.
Then start from the first place in the sorting, repeat the previous two parts to exchange the largest number to the end of the array (the number exchanged to the end is already sorted).
2.2 Algorithm implementation
Decomposition 1: Compare the first number with the next adjacent number, and exchange if it is larger
Decomposition 2: Exchange the largest number to the end of the array by comparing and exchanging
Decomposition 3: Repeat Decomposition 1 and Decomposition 2, swap the largest number to the end of the unsorted array until sorting is complete
Decomposition 4: Use loops instead of repeated operations
Practice:
package Sort;
public class BubbleSort002 {
public static void main(String[] args) {
int[] arrs ={8,6,1,7,2,5,4,12,9};
for(int j=1 ; j < arrs.length ;j++){
for(int i = 0;i < arrs.length - j;i++){
//比较两个元素
if(arrs[i]>arrs[i+1]){
int temp = arrs[i];
arrs[i] = arrs[i+1];
arrs[i+1] = temp;
}
}
}
//输出结果
for(int i = 0; i < arrs.length;i++){
System.out.println(arrs[i]+",");
}
}
}
3 insertion sort
3.1 Algorithm description
1. Starting from the first element, the element can be considered to have been sorted;
2. Take out the next element and scan from back to front in the sorted element sequence;
3. If the element (sorted) is larger than the new element, move the element to the next position;
4. Repeat step 3 until you find the position where the sorted element is less than or equal to the new element;
5. After inserting the new element into the position;
6. Repeat steps 2~5;
3.2 Algorithm implementation
Decomposition 1: Take out the first element from the unsorted array and compare it with the elements in the sorted collection, then move the compared element backwards to the head of the array or find the elements that are lower than the previous ones small location
Decomposition 2: Repeat the operation of Decomposition 1, and gradually expand the sorted queue.
Decomposition 3: Use the loop operation to optimize each round of the operation of finding the insertion position
Decomposition 4: Using loop operations to optimize multiple rounds of insertion operations
code:
package Sort;
public class InsertSort003 {
public static void main(String[] args) {
int[] arrs ={8,6,1,7,2,5,4,12,9};
for(int start=1;start < arrs.length;start++){
int insert = arrs[start];//待插入的元素
while (start > 0){
if(arrs[start-1]>insert){
//前面的数字比待插入的数字大,就把前面的数字往后移
arrs[start - 1]=arrs[start];
}else{
arrs[start]= insert;
//终止循环
break;
}
start--;
}
//如果start=0直接注入到首位
if(start == 0){
arrs[0]=insert;
}
}
//输出结果
for(int i = 0; i < arrs.length;i++){
System.out.println(arrs[i]+",");
}
}
}
Comparison of Three Algorithms
1. The above three kinds of sorting, bubbling, selection, and insertion, because the army uses nested loops, and the loop basically traverses all elements, so the big O notation requires (N^2) time levels
2. However, there are still some subtle differences between the three sorts (number of cycles, number of exchanges)
Bubble sort is the easiest to write, but the average performance is not as good as selection and insertion sort. Because it has more cycles and exchanges.
The number of comparisons in the selection sort cycle is large, but the number of exchanges is small
Insertion sort has more exchanges, but fewer cycles