# Algorithm learning Day01

Keywords: Java Algorithm

Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it

# 1, What is the time complexity?

The algorithm takes the highest order after adding the calculation times, ignoring the constant and the rest

If sorting is selected, n+n-1+n-2 +
The calculation formula of equal difference sequence can be obtained
anĀ²+bn+c
Remove the lower order and constant, leaving n ²
Therefore, the time complexity of the algorithm is O(n) ²)
(O) worst case time complexity
θ () average time complexity
Ω () time complexity in the best case

# 2, Simple sorting algorithm

## 1. Select Sorting

#### Basic idea:

First assume that bit 0 of the array is the minimum subscript, loop to the back to find the minimum number, and then exchange positions

#### Dynamic diagram demonstration:

The code is as follows:

```public class DemoApplication {

public static void main(String[] args) {
int [] arr = {1,2,4,5,6,11,5,53,24,666};
selectsort(arr);
for (int i : arr) {
System.out.println(i);
}
}

public static int[] selectsort(int[] arr){
if(arr.length<2){
return arr ;
}

for (int i = 0; i < arr.length-1; i++) {
int minIndex = i;
for (int j = i+1; j < arr.length  ; j++) {
minIndex = arr[j]> arr[minIndex]?minIndex:j;
}
swap(arr,i,minIndex);
}
return arr;
}
public static int [] swap(int[] arr,int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;

return arr;
}

}
```

## 2. Bubble sorting

#### Basic idea:

Two layers of loops are used to compare adjacent elements. If the latter is larger than the former, the position is exchanged. After the end of a loop, the largest one will be in the last position.

#### Dynamic diagram demonstration:

The code is as follows:

```package com.example.demo;

public class maopaosort {
public static void main(String[] args) {
int [] arr = {1,2,4,5,6,11,5,53,24,666,12,45,55,4562,6456,243234325,343,35345,45326,54353};
sort123(arr);
for (int i : arr) {
System.out.println(i);
}
}

public static int [] sort123(int [] arr){
for (int i = 0; i < arr.length ; i++) {
for (int j = 0 ; j < arr.length ; j++) {
if(arr[i]<arr[j]){
swap(arr,i,j);
}
}
}

return arr;
}

public static int [] swap(int[] arr,int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;

return arr;
}
}

```

## 3. Insert sort

#### Basic idea:

Ensure that a segment is orderly

#### Dynamic diagram demonstration:

```   public static void main(String[] args) {
int[] a = {17, 1, 24, 5, 566, 76, 456, 2366, 8632, 233};
sort(a);
for (int i : a) {
System.out.println(i
);
}
}

public static void sort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
for (int j = i - 1; j >= 0 && arr[j] > arr[j + 1]; j--) {
swap(arr, j, j + 1);
}
}

}

public static void swap(int[] arr, int i, int j) {
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
```

# 3, Fundamentals of XOR operation

&: 1 is taken as 1 during calculation
|: as long as there is a 1 in the calculation, take 1
~: reverse
^: the same takes 0 but different takes 1; it can be regarded as a non carry addition

# 4, A more elegant way to exchange two data: XOR

Normally, when exchanging data, you will think of a routine of carrying data with intermediate variables and then exchanging, as shown in the figure below:

```  public static int [] swap(int[] arr,int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
```

But perhaps we can choose a more elegant way to solve the problem at hand: XOR operation
XOR operation is to take 0 for the same and 1 for different. We can regard it as a non carry addition.
The XOR operation satisfies the commutative law and the associative law,
Then you can get through

```a = a ^ b;    //At this time: a = a ^ B B = b
b = a ^ b;    //      A = a ^ b B = a ^ b ^ b two b are the same, take 0 b = a
a = a ^ b;    //      a = a ^ b ^ a  a = b  b = a;
```

Highlight an elegance.
However, it should be noted that a and b must occupy different memory space****
In this case, you might as well try it in the future~

# 5, Exercise topic

###### 2) If there are two odd numbers and the others are even, how do you take them out?

Question 1: all XORs are enough

```   int [] a = {1,1,2,3,3};

for (int i = 0; i < a.length -1  ; i++) {

a[i] = a[i] ^ a[i+1];

}
for (int i : a) {
System.out.println(i);
}
```

Question 2:
First, if you XOR all, temp = a^b, because all even numbers are XORed out.
Well, because a and B still exist, they must have a different one, otherwise they will get 0
We try to take out a different number of a and B clock and classify it. Then all the numbers will be divided into two parts: one is 1 and the other is 0.
If the two parts are exclusive or, the others will be eliminated, and the rest is the number of odd bits.

How to get the rightmost different number:
Original data: 000001100
Reverse: 111110011
Plus one: 111110100
Original data and inverse plus one XOR: 000000100
In this way, the third bit can be used as the distinguishing bit.

Take 1 from 000000100 and the data and operation in the array.

Specific code implementation:

```        int [] a = {1,1,2,2,3,3,4,5};
int temp = 0;
for (int i : a) {
temp =  temp ^ i;
}
// temp = a ^ b
int rightone = temp &(~temp + 1);  //Drive the rightmost 1
int left = 0;
for (int i : a) {
if((i&rightone)==0){
left^=i;
}
}

System.out.println(left+" :" +(left^temp));
```

Posted by shylock on Thu, 21 Oct 2021 22:36:13 -0700