java arrays Exercise 3

/*

  • Sorting Algorithm
  • Usually sorted purpose is to quickly find
  • Sorting algorithm to measure the pros and cons
  • 1, the time complexity: number and mobile number of comparisons record of keyword analysis
  • 2, space complexity: how much memory is assisted analysis ranking algorithm needs
  • 3, stability: if both records A and B is equal to the key value, but the sort A, B priorities remain unchanged, this sorting algorithm is called stable

* Sort Classification Algorithm: Sort internal, external sorting
* 1, internal sorting: Sort No external reservoir during all sort operation is done in memory
* 2, external sorting: Sort the data involved in the very large amount of data is very large, the computer does not sort the whole process is completed in memory, requires the help of external memory,
* external sorting the most common is multiple merge sort, can be considered external sorting is composed of multiple internal sorting
*
* ten internal sorting algorithm
* 1 select sorting
* direct selection sort, heap sort
* 2, exchange sorting
* bubble sort (the simplest), (common fast speed) quicksort
* 3, insertion sort
* direct insertion sort, binary insertion sort, Shell sort (Shell sort)
* 4, merge sort
* 5, bucket sort
, 6, radix sort
*
* algorithm five characteristics
* 1, the input (input): zero or more input data, these inputs must have a clear descriptions and definitions
* 2 , the output (output): at least one output, the output can not
* 3, have poor resistance (Finiteness): algorithm may automatically end after a finite number of steps without the endless loop, and Each step may be acceptable
completed within the time *
* 4, deterministic (Definiteness): determining the meaning of each step of the algorithm does not occur ambiguity
* 5, feasibility (Effectiveness): algorithms each step is a clear and feasible, will allow users to calculate with pen and paper and find answers
* Description: deterministic algorithm is also referred to satisfy: deterministic algorithm, there are a much broader concept, consider a variety of non-deterministic algorithm, such as a parallel algorithms, probabilistic algorithms
*, etc. There is also not required to terminate the calculation described, this is also known as described procedure (procedure).
*
*
* comparison between the sorting algorithm
* 1, the average time to read, but the best fast sort algorithm in the worst case and not as heap sort merge sort
* 2, from the algorithm look simplicity, direct insertion sort, selection sort and bubble sort directly easiest, is considered to be a simple algorithm
* for shell sort, heap sort, merge sort and quick sort, algorithm complexity, that is the sort of complex
* 3, from stability See, directly into, and merge sort bubble is stable, direct selection, heap sort, quicksort shell sort is unstable and
* 4, the data to be sorted from the length N of view, should be a simple sorting N is small, compared with when using a large improvement sort
*
* selection algorithm
* 1, when n is small e.g. <= 50, or can be directly inserted directly select the sort
* when recording size is small, good direct insertion sort, NO Because direct selection is less than the number of mobile records directly into, select the direct selection sort
* 2, if the number of columns in the initial state of the basic order (n-order), is applied directly into, or random bubbling Quicksort
* 3, if the data size than big, time complexity is nlogn application of algorithms such as quick sort, heap sort, merge sort
*
* using arrays utility class
* java.util.Arrays provides an array of output, filling, assignment, reverse, binary search, sort, and other functions
* can query the API documentation
* /

/*

  • Bubble Sort
  • Description: Walk repeat the number of columns to be sorted, a comparison of two elements, put them in the wrong order if the exchange over
  • Sort idea:
  • 1, comparing adjacent elements, if the first greater than the second (ascending), the order of two or exchanged
  • 2, do the same for each adjacent element of work, from the beginning to the end of the first to the last pair, after this step is complete, the end of the number is the greatest.
  • 3, is repeated for all elements of the previous step, except the last one
  • 4. Repeat the above steps for fewer data until there is no need to swap the position of a pair of digital
  • Direct Selection Sort
  • 1, a first comparison element and the second element, if the second switching position is smaller than the first
  • 2, then compare the first and third up to a position such that the first element of the last minimum, of the array
  • 3, using the above steps for the second position, to ensure that the position of the second element in addition to the first element is smallest
  • 4, the above embodiment is repeated until the last position
  • Direct insertion sort
  • 1, the first and second comparison element if the second element is smaller than a first switching position, forming a ordered sequence of length 2
  • 2, comparing the third element and the second element size, small and if the second element is relatively forward, until it finds an element is not smaller than the former position.
  • 3, after elements of this position in order to move forward one, the third element into this position
  • 4. Repeat the above steps until the last one.
    * /

/*

  • Quick Sort
  • Other than the same quick sort algorithm is O (nlogn) faster, frequently used, and the use of ideological divide and conquer method
  • Invented by Tony Horare, the sort is by far one of the fastest algorithms, exchange sorting belongs, is
  • Bubble sort of upgrade.
  • Sort idea:
  • 1, an element selected from a number of columns in a reference (pivot).
  • 2, reorder columns, all elements smaller than the reference value is placed before the reference value, all the elements of great reference value in benchmarking
  • The same number can be placed on either side, the end of the partition, the reference position is in the middle of the series, this step is called partitioning (partition).
  • 3, the recursive (recursive This) is smaller than the reference value is greater than the number of columns a reference value and the number of sub-column sorting
  • 4, the bottom case of recursion is the size of the number of columns is 0 or 1, that is, always be a good sort ,, Although there has been recursion, but the end of the algorithm will always, because in each iteration (iteration)
  • , Which would at least one element placed to its final position.
  • Common abnormal array
  • 1, the array subscript out of bounds exception: ArrayIndexOutOfBoundsException
  • Subscript beyond the array subscripts range [0, length]
  • 2, null pointer exception: NullPointerException (common)
  • 1, case 1, the array is not initialized, or an array of stack space a new assignment is null, heap address is erased
  • int[] arr = new int[3];
  • arr = null;
  • System.out.println (arr); // null output
  • System.out.println (arr [0]); // Error
  • int[][] arr1 = new int[4][];
  • System.out.println (arr [0]); // default value of null output
  • System.out.println (arr [0] [0]); // Error
  • 2, Case 2, calling null
  • String[] arr2 = new String[]{null,“A”,“B”,“C”};
  • System.out.print (arr2 [0]); // null output
  • System.out.print (arr2 [0] .toString ( )); // call null, error
    * /
package com.atguigu.contact;
import java.util.*;
public class Array4 {
    public static void main(String[] args) {    	
    	Scanner scan = new Scanner(System.in); 
    	System.out.println("请输入随机数组的长度");
	    int length = scan.nextInt();
	    int[] array = new int[length];
	    for (int l = 0; l < array.length; l++) {
	    	array[l] = (int)(Math.random() * 201 - 100);//随机数组范围【-100,100】		    
	    }
	    for (int l = 0;l < array.length;l++) {
	    	System.out.print(array[l] + " ");	    	
		}
	    System.out.println();
	    
        //直接插入排序
	    for (int i = 1; i < array.length; i++) {
			int temp,k;
			temp = array[i];//待比较的元素存入临时变量中
			k = i;
			for (;k > 0 && array[k - 1] >= temp;) {
				 array[k] = array[k - 1];//依次移动之前比temp大的元素到下一位置
				 k--;//角标每次自减一,直到array[k - 1]小于待比较大元素
			}	
			array[k] = temp;//将要比较的元素插入位置k
		}
	    	    
	    for (int l = 0;l < array.length;l++) {
	    	System.out.print(array[l] + " ");	    	
		}
	    System.out.println();
	    
	    //直接选择排序
	    for (int i = 0; i < array.length -1; i++) {
			int k = i;
			int temp = 0;
			for (int j = i; j < array.length; j++) {
				if(array[j] < array[k]) {					
					k = j;//如果找到一个更小的值则K的角标变换为次值的角标,最后k是最小值的角标
				}
			}
			temp = array[i];
			array[i] = array[k];
			array[k] = temp;//将找到的最小值与本次外层循环的第一个数值交换位置
		}
	    for (int l = 0;l < array.length;l++) {
	    	System.out.print(array[l] + " ");	    	
		}
	    System.out.println();
	    
	    
	    //冒泡排序
	    
    	for (int i = 0; i < array.length -1; i++) {//外层循环控制轮数
			for (int j = 0; j < array.length - 1 - i; j++) {//内层循环控制比较交换次数,每一轮去掉最后一位,需要-i
				if(array[j] > array[j + 1]) { //array[j] < array[j + 1]表示降序
					int temp = array[j];
					array[j] = array[j + 1];
					array[j + 1] = temp;//如果顺序错误则交换位置
				}
			}
				
			}
	
    	for (int l = 0;l < array.length;l++) {
	    	System.out.print(array[l] + " ");	    	
		}
	    System.out.println();
	    
		//Arrays工具类的使用
	    //创建数组测试使用
	    System.out.println("请输入随机数组的长度");
	    int length1 = scan.nextInt();
	    int[] array1 = new int[length1];
	    for (int l = 0; l < array1.length; l++) {
	    	array1[l] = (int)(Math.random() * 201 - 100);//随机数组范围【-100,100】		    
	    }
	    for (int l = 0;l < array1.length;l++) {
	    	System.out.print(array1[l] + " ");	    	
		}
	    System.out.println();
	    int[] array2 = new int[0];//数组可以是空集,长度是0
	    int[] array3 = new int[] {1,2,3,4,5,6};
	    int[] array4 = new int[] {1,2,6,5,4,3};
	    int[] array5 = new int[] {23,-45,78,34,22,11,89,36,-34,-99,28,0,5,1};
	    //比较两个数组,判断是否相等
	    boolean mark0 = Arrays.equals(array3, array4);
	    boolean mark1 = Arrays.equals(array3, array4);//位置不同,数组也不同
	    System.out.println(mark0 + "" + mark1);
	    //输出数组元素
	    System.out.println(Arrays.toString(array2));
	    System.out.println(Arrays.toString(array3));
	    for (int i : array3) {//另一种输出方法
			System.out.print(i + " ");
		}
	    System.out.println();
	    //将指定数字填充到数组中
	    Arrays.fill(array3, 2);
	    System.out.println(Arrays.toString(array3));//数组中的所有元素都会被填充为指定数值
	    //对数组排序
	    Arrays.sort(array4);
	    System.out.println(Arrays.toString(array4));
	    Arrays.sort(array5);
	    System.out.println(Arrays.toString(array5));//底层调用双基准快速排序实现
	    //二分查找,输出被查找元素的角标,前提是数组已被排序好
	    int index = Arrays.binarySearch(array5, 11);
	    int index1 = Arrays.binarySearch(array5, 13);//返回正数代表角标,负数代表没找到不存在
	    System.out.println(index + "" + index1);
	    if(index1 >= 0) {
	    	System.out.println(index1);
	    }else {
	    	System.out.println("未找到");
	    }
	    //数组的复制
	    int[] array6 = new int[array5.length];
	    array6  = Arrays.copyOf(array5,array5.length);
	    System.out.println(Arrays.toString(array6));
	    int[] array7 = new int[array5.length];
	    array7 = array5.clone();//另一种方法
	    System.out.println(Arrays.toString(array7))}        
}
Published 47 original articles · won praise 1 · views 1083

Guess you like

Origin blog.csdn.net/wisdomcodeinside/article/details/103945502