Algorithm related

1. Selection sort: unstable, time complexity O(n^2)
2. Insertion sort: stable, time complexity O(n^2)
3. Bubble sort: stable, time complexity O(n^2)
4. Heap sort: unstable, time complexity O(nlog n)
5. Merge sort: stable, time complexity O(nlog n)
6. Quick sort: unstable, time complexity is optimal O(nlogn) worst Time O(n^2)
7. Hill sort: unstable, time complexity average time O(nlogn) worst time O(n^s) 1<s<2




binary search:
package test_yhl.thread;

public class MinAbsTest{
    
	private static int findLocation(int[] testInt, int value) {
		
		int left = 0;
		int right = testInt.length-1;
		
		int LocationValue = 0;
		while(left<= right) {//=
			int middle = (right+left)/2;//+, put it inside while
			if (value < testInt[middle]) {
				right = middle-1;//-1
			}else if(value > testInt[middle]){
				left = middle+1;//+1
			} else{
				LocationValue= middle;
				break;//break;
			}
		}
		return LocationValue;
	}
	
    public static void main(String[] args) throws InterruptedException{  

    	int[] testInt = new int[]{3,10,29,43,48,74,86,300};//new int[]{}
    	System.out.println(findLocation(testInt,300));		
    }
}


Insertion sort:
http://blog.csdn.net/booirror/article/details/45339387
Two-layer loop, the first layer is looped sequentially, and the second layer compares the replacement values ​​from the back to the front in the unsorted sequence until the end of the unsorted sequence The value is replaced with this value.
Insertion sort principle: It works by constructing an ordered sequence, for unsorted data, scan from back to front in the sorted sequence, find the corresponding position and insert.
Insertion sorting core: Assuming that the first element is arranged, the elements after the arrangement are compared from the back to the front and moved one by one.
package test_yhl.thread;

import java.util.Arrays;

public class MinAbsTest{
    
	public static void main(String[] arg) {
		int[] testInts = new int[]{6,7,112,7,12,34};
		
		int y = 0;// y在for外
		for (int x=0; x<testInts.length; x++) {
			int tem = testInts[x]; //must define temporary testInts[x]
			for (y=x; y>0 && tem < testInts[y-1]; y--) {
				testInts[y] = testInts[y-1];//[y] [y-1]
			}
			testInts[y] = tem;// [y]
		}
		
		System.out.println(Arrays.toString(testInts));
	}
}

 8 2 4 9 3 6 First, let's consider the number 2. Assuming that the following number does not exist (there is only an 8 in the hand, and another 2 is caught), then obviously the 2 should be placed in front of the 8.

  2 8 4 9 3 6 Another 4 was caught, now everyone knows what to do, right?

  2 4 8 9 3 6 There is another 9, that's right, just don't need to change the order

  2 4 8 9 3 6 For the same reason, consider the position where 3 should be placed, obviously in the middle of 2 and 4

  2 3 4 8 9 6 The same is true for the last one. Finally, the sequence from small to large is obtained.

  2 3 4 6 8 9 Complete the sorting

. Selection sorting:
compare areas one by one from left to right, the left side is more and more sorted, and the right side is less and less sorted.
package test_yhl.thread;

import java.util.Arrays;

public class MinAbsTest{
    static int z;
	public static void main(String[] arg) {
		int[] a = new int[]{16,7,338,12,34,112};

		int n=a.length;
		int i,j,k;
		for(i=0; i<n; i++){
			k=i;
			for(j=i+1; j<n;j++){
				if(a[k] > a[j]){// [k] [j]
					k=j;
				}
			}
			if(a[k]!=a[i]) {
				int tem = a [i];
				a[i] = a[k];
				a[k] = has;
			}
		}

		System.out.printf(Arrays.toString(a));
	}
}


Bubble Sort:
Principle: Compare two adjacent elements and swap the element with the larger value to the right.
package test_yhl.thread;

import java.util.Arrays;

public class MinAbsTest{
    static int z;
	public static void main(String[] arg) {
		int[] a = new int[]{-13,-32,-8,5512,6,34,7,338,112};

		int i,j;
		for(i =0;i<a.length-1;i++) {
			for(j=0; j<a.length-1-i; j++) { // -1-i
				
				if(a[j]>a[j+1]) {
//If it is absolute value sorting if(Math.abs(a[j])>Math.abs(a[j+1])) {
					int tem = a [j];
					a[j] = a[j+1];
					a[j+1] = has;
				}
			}
		}
		
		System.out.println(Arrays.toString(a));
	}
}


Merge sort:
https://www.cnblogs.com/chengxiao/p/6194356.html
package org.xdemo.example.SpringActivemq.service.consumer;

import java.util.Arrays;

public class ConsumerTest {

	public static void main(String[] args) throws Exception {
        int []arr = {9,8,7,6,5,4,3,2,1};
        int []temp = new int[arr.length];//Before sorting, build a temporary array with a length equal to the length of the original array to avoid frequent opening of space in recursion
        sort(arr,0,arr.length-1,temp);
        System.out.println(Arrays.toString(arr));
    }

    private static void sort(int[] arr,int left,int right,int []temp){
        if(left<right){
            int mid = (left+right)/2;
            sort(arr,left,mid,temp);//Left merge sort, so that the left subsequence is ordered
            sort(arr,mid+1,right,temp);//Merge sort on the right so that the right subsequence is ordered
            merge(arr,left,mid,right,temp);//Merge two ordered subarrays
        }
    }
    private static void merge(int[] arr,int left,int mid,int right,int[] temp){
        int i = left;//Left sequence pointer
        int j = mid+1;//Right sequence pointer
        int t = 0; //temporary array pointer
        while (i<=mid && j<=right){ // <=
            if(arr[i]<=arr[j]){
                temp[t++] = arr[i++];
            }else {
                temp[t++] = arr[j++];
            }
        }
        while(i<=mid){//fill the remaining elements on the left into temp<=
            temp[t++] = arr[i++];
        }
        while(j<=right){//Fill the remaining elements of the right sequence into temp<=
            temp[t++] = arr[j++];
        }
        t = 0;
        //Copy all elements in temp to the original array
        while(left <= right){ //<=
            arr[left++] = temp[t++];
        }
    }
}


Heap sort:
https://www.cnblogs.com/jingmoxukong/p/4303826.html
https://www.cnblogs.com/chengxiao/p/6129630.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326270692&siteId=291194637