Java related basic algorithms

Execution efficiency from fast to slow: fast, hill, insert, select, bubble sort
#1. Array reverse
Realization idea: first increasing, last decreasing

//Array elements in reverse order
public static void receive(int[] arr){
	for (int start = 0, end = arr.length-1; start < end; start++,end--) {
		int temp = arr[start];
		arr[start] = arr[end];
		arr[end] = temp;
	}
}

#2. Select Sorting
Implementation idea: compare from left to right to find the minimum value, and discharge from left to right in turn.

// Select sort
   public static void select_sort(int[] arr) {
   	for (int i = 0; i < arr.length - 1; i++) {
   		//Optimization of selection sorting
   		int k = i;
   		for (int j = k + 1; j < arr.length - 1; j++) {
   			// Find the subscript of the minimum value
   			if (arr[j] < arr[k]) {
   				k = j;
   			}
   		}
   		if (k != i) {
   			int temp = arr[i];
   			arr[i] = arr[k];
   			arr[k] = temp;
   		}
   	}
   }

Deepen understanding: https://www.cnblogs.com/shen-hua/p/5424059.html
#3. Bubble sorting
Implementation idea: start from the beginning, compare next to next, and put the biggest one at the end

//Bubble sorting
public static void bubbleSort(int[] arr) {
	//function
	//Outer loop is used to control the number of loops of array loop
	for (int i = 0; i < arr.length-1; i++) {
		//j <  arr.length -1 to avoid the corner sign crossing the boundary
		//j <  arr.length -1-I in order to compare efficiency, avoid repeated comparison
		//Inner loop is used to complete element value comparison and exchange large element values to the back
		for (int j = 0; j < arr.length-1-i; j++) {
			if (arr[j] > arr[j+1]) {
				int temp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = temp;
			}
		}
	}
}

#4. General search
Implementation idea: traverse array, find the same elements

//General search
public static int getArrayIndex(int[] arr, int number) {
	//Compares elements in an array with specified values in turn
	for (int i = 0; i < arr.length; i++) {
		if (arr[i] == number) {
			//eureka
			return i;
		}
	}
	return -1;
}

#5. Binary search
Implementation idea: it is known to be an ordered array. Compare the intermediate element with the element to be searched. If it is large, take the left

//Binary search method (half search method)
public static int halfSearch(int[] arr, int number) {
	//Define three variables to record the position of min, min, mid
	int min = 0;
	int max = arr.length-1;
	int mid = 0;
		while (min <= max) {
           mid = (min+max)/2;
		//It's not found. Update the scope and continue the comparison
		//Update scope
		if (arr[mid] > number) {
			//on the left
			max = mid-1;
		} else if(arr[i] < number){
			//On the right
			min = mid+1;
		}
		else{
              return mid ;
          }
	 
	return -1;
}

https://www.cnblogs.com/kangyi/p/4262169.html
#6. Fast platoon
Implementation idea: put the small index first, find an index, put the smallest index, and cycle to get a minimum value

public static void quickSort(int[] arr) {
		if (null == arr) {
			System.out.println("The array passed in is empty!!!");
		}
		for (int i =0;i < arr.length;i++) {
			int index = i;
			for (int j = i;j < arr.length;j++) {
				if (arr[index] > arr[j]) {
					index = j;
				}
			}
			if (index != i) {
				int temp = arr[index];
				arr[index] = arr[i];
				arr[i] = temp;
			}
		}
	}

#7. Quick sorting
Realization idea: digging and filling number + divide and control method
Idea: first, take a cardinality, find the smaller subscript from right to left than the cardinality, find the larger subscript from left to right, find the interchange position with the cardinality, then carry out the next round of operation, and then call the fast sorting algorithm recursively.

public static void quick_sort(int[] arr, int l, int r) {
		if (l < r) {
			// Determine the range of array subscripts
			int i = l, j = r;
			// Determine the benchmark number first
			int flag = arr[l];
			while (i < j) {
				// The subscript j decreases from right to left to find a number smaller than the cardinality
				while (i < j && flag < arr[j])
					j--;
				if (i < j) {
					// Find and fill the base pit
					arr[i] = arr[j];
					i++;
				}
				// Index i increases from left to right to find a number larger than the base
				while (i < j && flag > arr[i])
					i++;
				if (i < j) {
					// Find and fill the new hole
					arr[j] = arr[i];
					j--;
				}

			}
			// Place the original reference value in the middle number
			arr[j] = flag;

			// Recursive call
			// Left recursive call of middle number
			quick_sort(arr, l, i - 1);
			// Recursive call to the right of middle number
			quick_sort(arr, i + 1, r);

		}
	}

#8. Recursive algorithm
advantage:
1. Simple code
2. In the tree traversal algorithm, there are more recursions than loops
Disadvantages:
1. Due to function call itself, time and space consumption are relatively large
2. Many calculations in recursion are repeated, and the efficiency is relatively low
Recursive optimization:
Use dynamic planning: calculate and save all possible results until the required results are obtained

int Fib(unsigned n)
{
    if(n==1)return 1;
    if(n==0)return 0;
    return Fib(n-1)+Fib(n-2);
}


int Fib(unsigned n)
{
    int* f=new int[n+1];
    f[1]=1;f[0]=0;
    for(int i=0;i<=n;i++);
        f[i]=f[i-1]+f[i-2];  
    int r=f[n];
    return r;
}

Posted on Thu, 11 Jun 2020 01:32:22 -0400 by CoffeeOD