An array of class algorithm and Arrays

Array algorithm

Selection Sort
int[] arr={6,2,8,0,1,9};
for (int i = 0; i < arr.length-1; i++)	
{
    for(int j=i+1;j<arr.length-1;j++)
    {
        if(arr[i]>arr[j])
        {
            int temp=0;
            temp=arr[i];
            arr[i]=arr[j];
            arr[j]=temp;
        }
    }
}
/*
将第一个元素与后面的元素依次比较,如果这个元素大于后面的元素,换位
第一次循环结束,最小值出现在了0角标位
我们再将第二个元素与后面元素依次比较,如果这个元素大于后面的元素,换位
第二次循环结束,第二小的值出现在了1角标位
......
就这样依次比较,最终将数组中的元素从小到大排序
此处i<arr.length-1是因为当遍历最后一个元素时,不需要进行比较了,该元素就是最大值。
哪怕此处循环条件为i<arr.length,i=arr.length-1时,j=arr.length,内循环也无法执行循环体
*/
Bubble Sort

int[] arr={6,2,8,0,1,9};
for (int i = 0; i < arr.length-1; i++)
{
	for(int j=0;j<arr.length-i-1;j++)
	{
		if(arr[j]>arr[j+1])
		{
			int temp=0;
			temp=arr[j];
			arr[j]=arr[j+1];
			arr[j+1]=temp;
		}
	}
}
/*
将两个相邻元素进行比较,如果前一元素比后一元素大,换位
第一次循环
将第一个元素与第二个元素进行比较,第一个元素大于第二个元素,进行换位
将第二个元素与第三个元素进行比较,第二个元素大于第三个元素,进行换位
将第三个元素与第四个元素进行比较,第三个元素大于第四个元素,进行换位
......
当第一次循环结束后,最大值出现在了最后
第二次循环依旧从第一个元素开始,但是只遍历到arr[arr.lenfgth-2],
即最后一个元素不遍历,次数第二大的元素在倒数第二个角标位上
第三次循环时,最后两个元素不遍历
......
就这样不断循环,每次将最大值放在遍历到的最后一个角标位上,每次少遍历一个角标位
将数组从小到大进行排序
j<arr.length-i-1的目的就是为了控制让每次循环少遍历一个元素。
*/
//在实际开发中我们会使用Java给我提供的排序方法,但这种排序思想还是要掌握
/*
使用Java提供的排序方法的步骤
1.导包
import java.util.*;
2.调用方法
Arrays.sort(arr);
*/
Insertion Sort

Get behind each element, an ordered sequence prior to insertion into and allowed to remain ordered
such an array {2,6,3,1,8}

  1. {2} 2 {6,3,1,8} 6 is inserted into the ordered sequence of this
  2. {2,6}, {3,1,8 2,6} 3 is inserted into the ordered sequence of this
  3. {2,3,6}, {2,3,6, 8} 1 is inserted into the ordered sequence of this
  4. {1,2,3,6}, {8 8 is inserted into the ordered sequence 1,2,3,6}
  5. The final array is: {1,2,3,6,8}
    code for:
import java.util.Arrays;
public class InsertSort {
    public static void insertSort(int[] arr)
    {
        //外循环的循环次数代表要插入几个数
        for (int i = 0; i < arr.length - 1; i++) {
            //temp代表需要插入的数
            int temp=arr[i+1];
            //因为在内循环外要使用变量j,所以将j定义在循环外
            int j;
            /*
            j=i代表从有序数组的最后一个元素开始遍历,直到第一个元素
            j>=0是为了防止数组越界
            如果temp<arr[j],即插入数小于有序数组中的元素,就将有序数组中的元素后移,直到不满足条件
            不满足条件时,第一种情况是j=-1,此时代表插入数小于有序数组的所有元素,应该在第一个元素的位置
            第二种情况就是插入数大于有序数组中的某个元素,但这时我们也找到了插入数应该插入的位置
            */
            for (j=i;j>=0&&temp<arr[j];j--)
            {
                arr[j+1]=arr[j];
            }
            //将插入数放入有序数组中,使这个数组依旧有序
            arr[j+1]=temp;
        }
    }
    public static void main(String[] args) {
        int[] arr={2,6,3,1,8};
        insertSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
Quick Sort
  1. Select a number from the array as a reference number
  2. Partition: the number in the array equal to or greater than the reference number of the reference number on the right, the reference number of the array is less than the number of the reference number on the left
  3. And then the second step is repeated around the interval, until each section is only a number

Zoning realization of ideas: digging fill in the number of

  1. The reference number first dig a pit is formed, a first number of marking a start bit, an end bit is the last number
  2. Looking from back to front as much as his number, this number after finding dug a pit filled before, this number marked the end of the previous number of bits
  3. From front to back than he's looking for a few, after finding also dug up a number of times before to fill the pit, the number of marks this number back to the start position
  4. Repeat steps 2 and 3 until the end position until the start bit or less

Code:

import java.util.Arrays;

public class QuickSort {
    public static void quickSort(int[] arr,int start,int end)
    {
        if(start<end)
        {
            //分组的索引
            int index=getIndex(arr,start,end);
            //递归调用
            //对左区进行分组
            quickSort(arr,start,index-1);
            //对右区进行分组
            quickSort(arr,index+1,end);
        }
    }

    private static int getIndex(int[] arr, int start, int end) {

        int i=start;
        int j=end;
        //用temp记录基准数
        int temp=arr[i];
        while(i<j)
        {
            //由后向前遍历,直到出现小于基准数的元素
            while(i<j&&arr[j]>=temp)
            {
                j--;
            }
            //将找到的数填到上一个坑中
            if(i<j)
            {
                arr[i]=arr[j];
                //将i自增1是为了跳过遍历已经被遍历过的数
                i++;
            }
            //由前向后遍历,直到出现大于基准数的元素
            while(i<j&&arr[i]<temp)
            {
                i++;
            }
            //将找到的数填到上一个坑中
            if(i<j)
            {
                arr[j]=arr[i];
                //将j自减1是为了跳过遍历已经被遍历过的数
                j--;
            }
        }
        //当i和j相等时,代表开始标志位和结束标志位重合,遍历结束
        //此时将基准数填到最后一个坑中
        arr[i]=temp;
        //此时i和j相等,分组已经完成,返回基准数所在的索引,对左右区进行递归,分组直至左右区只有一个元素位置
        return i;
    }

    public static void main(String[] args) {
        int[] arr={3,1,6,3,7};
        quickSort(arr,0,arr.length-1);
        System.out.println(Arrays.toString(arr));
    }
}

Binary search

We introduce a more efficient way to find, binary search
For example, if you specify a number between 1 and 100, you guess, guess the numbers, you will be prompted to guess the number of big or small.
The fastest way is to guess the middle value, so you can narrow down the fastest, and then find the fastest this number.
The binary search is to use this idea
to compare the middle element and the target value (key) the size of the relationship, and then step by step to narrow the scope, and finally determine the location of the target value in the array

import java.util.Arrays;
public class BinarySearch {
    public static int binarySearch(int[] arr,int key)
    {
        int max=arr.length-1;
        int min=0;
        int mid=(min+max)/2;
        //因为我们不知道max何时小于min,即不知道具体的循环次数,所以使用while循环
        while(max>=min)
        {
            //如果目标数等于中间元素,代表直接找到目标数索引
            if(key==arr[mid])
            {
                return mid;
            }
            else if(key>arr[mid])
                min=mid+1;
            else
                max=mid-1;
            mid=(min+max)/2;
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arr={1,2,3,4,5,6};
        int index=binarySearch(arr,4);
        System.out.println(index);
    }
}
 /*
 首先我们要判断key是否与arr[mid]相等,如果相等就找到了目标元素的角标,直接跳出循环
 如果不相等,判断key与arr[mid]的大小关系
 如果,key>arr[mid],说明猜的值小了,就要把下限提高,我们key已经和arr[mid]比较过了,所以下限变为mid+1
 如果,key<arr[mid],说明猜的值大了,就要把上限降低,所以下限变成max-1
 当范围发生改变后,要改变mid的值,不然就是死循环,然后再次循环比较,知道找到目标值的角标
 当min>max时,代表数组中不存在目标值,循环结束
 */
 //折半查找还有第二种表现方式
 while(key!=arr[mid])
 {
 	if(key>arr[mid])
 		min=mid+1;
    else
        max=mid-1;
    mid=(min+max)/2;
    if(min<max)
    	break;
 }
 /*
 我们把key!=arr[mid]作为控制循环体的条件
 如果min小于max,说明目标值在数组中不存在,直接跳出循环。
 */
 //注意:折半查找只适用于有顺序的数组,不适合乱序的数组!!

Arrays类

Outline

Tools that operate on the array.
It provides a sort, search and other functions.
Arrays class is used to import the package java.util.Arrays

method

public static String toString (int [] a)
returns the contents of the specified array string representation.
Incoming object can also be other types of arrays

public class Demo {
    public static void main(String[] args) {
        int[] arr={1,2,4,2,4,5};
        char[] ch={'d','3','a','g'};
        System.out.println(Arrays.toString(arr));
        System.out.println(Arrays.toString(ch));
    }
}
/*
运行结果:
[1, 2, 4, 2, 4, 5]
[d, 3, a, g]
*/

public static void sort (int [] a)
arranged in numerical order specified array.
public static void sort (int [] a, int fromIndex, int toIndex)
in ascending order of the array of the specified range.

Incoming object can also be other types of arrays

public class Demo {
    public static void main(String[] args) {
        int[] arr={1,2,4,2,4,5};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
/*
运行结果:
[1, 2, 2, 4, 4, 5]
*/
public class Demo {
    public static void main(String[] args) {
        int[] arr={1,2,4,2,4,5};
        Arrays.sort(arr,3,6);
        System.out.println(Arrays.toString(arr));
    }
}
/*
运行结果:
[1, 2, 4, 2, 4, 5]
*/

public static int binarySearch (int [] a, int key)
with the specified value binary search algorithm searches the specified int array.
Incoming object can also be other types of arrays

int[] arr={1,2,3,4,5,6};
int index=binarySearch(arr,4);

static boolean equals (int [] a , int [] a2)
the elements in the two arrays comparison, whether the same

public class Demo4 {
    public static void main(String[] args) {
        int[] arr1={1,2,3,4};
        int[] arr2={1,2,3,4};
        boolean b=Arrays.equals(arr1,arr2);
        System.out.println(b);
    }
}
/*
运行结果:
true
*/

public static int [] copyOf (int [] original, int newLength)
copy source elements of the array into a new array, a new array length newLength start copying the old array from 0
public static int [] copyOfRange (int [] original, int from, int to)
several elements of the specified range between the copy source array into a new array

public class Demo4 {
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5,6,7};
        int[] arr1=Arrays.copyOf(arr,3);
        int[] arr2=Arrays.copyOfRange(arr,3,6);
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));
    }
}
/*
运行结果:
[1, 2, 3]
[4, 5, 6]
*/
Published 26 original articles · won praise 1 · views 353

Guess you like

Origin blog.csdn.net/weixin_45919908/article/details/103769654