The top ten algorithms that dominate the world:
- Merge sort, quick sort, heap sort
- Fourier transform and fast Fourier transform
- Dijkstra's algorithm
- RSA asymmetric encryption algorithm
- Hash security algorithm (Secure Hash Algorithm)
- Integer factorization algorithm (Integer factorization)
- Link Analysis Algorithm (Link Analysis)
- Proportional calculus algorithm
- Data compression algorithm
- Random number generation algorithm
Three simple sorting algorithms (time complexity O (n ^ 2))
(1) Direct insertion sort
Each record is inserted into the appropriate position of a sorted sub-file in turn, and the inserted file is still in order. Stable sorting algorithm, time complexity O (n ^ 2).
class Solution {
//直接插入排序
public int[] sortArray(int[] nums) {
int temp=0;
for(int i=0;i<nums.length-1;i++){
temp=nums[i+1]; //暂存第i+1个值,将其插入合适的位置
int j=i;
while(j>=0 && nums[j]>temp){
nums[j+1]=nums[j];
j--;
}
nums[j+1]=temp;
}
return nums;
}
}
(2) Direct selection and sorting
Unstable sorting algorithm, select the record with the smallest keyword among all records, exchange it with the first record for storage location, and then select the record with the smallest keyword and the second record among the remaining records Swap storage locations. And so on, until all records become an ordered sequence. (Sort n-1 times)
class Solution {
//直接选择排序
public int[] sortArray(int[] nums) {
for(int i=0;i<nums.length;i++){ //选第i大的元素放到第i个位置
int small=i;
for(int j=i+1;j<nums.length;j++){
if(nums[j]<nums[small])
small=j;
}
if(small!=i)
swap(nums,i,small);
}
return nums;
}
public void swap(int[] nums,int i,int j){
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
}
(3) Bubble sort
Idea: constantly compare two adjacent records. If the sorting requirements are not met, exchange the adjacent records until after the n-1th record is compared with the nth record and exchanged, a bubble sort is completed , So that the record with the largest keyword is placed in the last position. Then, the same operation is performed on the first n-1 records until n-1 passes are made.
class Solution {
//冒泡排序,O(n^2)
public int[] sortArray(int[] nums) {
int flag=1;
for(int i=1;i<nums.length && flag==1;i++){ //循环n-1次
flag=0;
for(int j=0;j<nums.length-i;j++){
if(nums[j]>nums[j+1]){
flag=1;
swap(nums,j,j+1);
}
}
}
return nums;
}
public void swap(int[] nums,int i,int j){
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
}
Comparison of three simple sorting algorithms
Direct insertion sort | Direct selection sort | Bubble Sort | ||||
---|---|---|---|---|---|---|
Keywords than the more times | Record move moving number | Keywords than the more times | Record move moving number | Keywords than the more times | Record move moving number | |
Best (positive order) | O (n) | O (n) | O (n2) | 0 | O (n) | 0 |
Worst (reverse order) | O (n2) | O (n2) | O (n2) | O (n) | O (n2) | O (n2) |
average value | O (n2) | O (n2) | O (n2) | O (n) | O (n2) | O (n2) |
Average time complexity | O (n2) | O (n2) | O (n2) | |||
Space complexity | O (1) | O (1) | O (1) |