Java SE 035 bubble sort, exchange sort and quick sort principle and implementation

(1) As long as a person does not give up on himself, the whole world will not give up on you.
(2) I am born to be of great use . (3) If I
cannot bear the suffering of learning, I must bear the suffering of life. How painful it is Deep comprehension.
(4) You must gain from doing difficult things . (
5) Spirit is the real blade.
(6) Conquering opponents twice, the first time in the heart.
(7) Writing is really not easy. If you like it or have something for you Help remember to like + follow or favorite~

Java SE 035 bubble sort, exchange sort and quick sort principle and implementation

1.System class arraycopy (copy array)

public static void arraycopy(Object src,
                             int srcPos,
                             Object dest,
                             int destPos,
                             int length)

(1)Parameters:

  • src - the source array.
  • srcPos - starting position in the source array.
  • dest - the destination array.
  • destPos - starting position in the destination data.
  • length - the number of array elements to be copied.

(2) The main function of this method is to copy the elements of the source array to the target array.

  • The first parameter is: refers to the array to be copied
  • The second parameter is: is the starting position in the source array, from which element position in the source array is copied.
  • The third parameter is: which array to copy the source array to.
  • The fourth parameter is: specify the position of the target array to receive the copy.
  • The fifth parameter is: refers to the number of array elements to be copied.

Example:

public class ArrayTest2
{
    
    
	public static void main(String [] args){
    
    
		int [] a = new int[]{
    
    1,2,3,4};
		int [] b = new int[4];

		System.arraycopy(a,0,b,0,4);

		for(int i = 0; i < b.length; i++){
    
    
			System.out.println(b[i]);
		}
	}
}

2. Three-dimensional array

	type [][][] a = new type[2][3][4];
public class ThreeDimensionArrayTest
{
    
    
	public static void main(String [] args){
    
    
		int [][][] a = new int[2][3][4];
		System.out.println(a instanceof int[][][]);

		for(int i = 0 ; i < a.length; i++){
    
    
			for(int j = 0 ; j < a[i].length; j++){
    
    
				for(int k = 0; k<a[i][j].length; k++){
    
    
					a[i][j][k] = 100;
				}
			}
		}
	}
}

3. Bubble sort

package com.javareview.arraysim;

/**
 * 冒泡排序
 * @author xiongjie
 *
 */
public class ArraySort {
    
    

	public static void main(String[] args) {
    
    
		int [] a = new int[] {
    
    100,108,33,156,15,76,90};
		
		for(int i = 0 ; i < a.length - 1;i++) {
    
    //控制比较的轮数
			//控制每一次比较的元素的索引。为什么要-i-1呢,主要控制排完序之后,即找到的第三或第四大的元素就不用去比较它们了。也就是每一次比较的个数都比上一次少一个。
			for(int j = 0 ; j < a.length - i - 1;j++) {
    
    
				if(a[j] > a[j + 1]) {
    
    
					int temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
			}
		}
		
		for(int k = 0 ; k < a.length ; k++) {
    
    
			System.out.print(a[k] + " ");
		}
		
		System.out.println();

	}

}

Guess you like

Origin blog.csdn.net/xiogjie_67/article/details/108501170