Java- and inserted into the binary search

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/xiaoke5491/article/details/101714473

Given a set of small to large arrays, a positive integer from keyboard input, and then inserted into the holder after the array is still ordered array.
Ideas: binary search data structures studied, also known as binary search.
Binary search (English: half-interval search), also known as binary search (English: binary search), logarithmic search (English: logarithmic search), a search algorithm to find a particular element in an ordered array.
Search process from the middle of the array elements, if the intermediate element the element is just looking for, the search process ends; if a particular element is greater than or less than the intermediate element in the array is greater or less than half that of the intermediate element lookup, and Like start start comparing the middle element. If the array is empty at some stage, it represents not found. This search algorithm so that each comparison search reduced by half.
Find FIG follows:
Here Insert Picture Description
implementation code:

import java.util.Scanner;
public class Thirty {
 static Scanner sc = new Scanner(System.in);
 static int h=sc.nextInt();
 static int[] a={1,2,5,10,15,20,30,35,45};
 static int low=0;
 static int heigh=a.length-1;
  static int mid=(low+heigh)/2;
  static int mid1;
  public static void main(String[] args) {
  System.out.println("数组a的大小"+a.length);
  System.out.print("数组a=[");
  for(int i=0;i<=a.length-1;i++){
   System.out.print(a[i]+" ");}
  System.out.print("]");
  System.out.println();
  System.out.print("经过折半查找后,找到的应该插入的下标=");
  System.out.println(BinarySearch(h));
  insert(a,h);}
  //二分查找,找到这个数应该在数组的位置
  public static int BinarySearch(int nums){
   while(low<heigh){
    if(nums<a[mid]){
     heigh=mid-1;
     mid=(low+heigh)/2;
    }else if(nums>a[mid]){
     low=mid+1;
     mid=(low+heigh)/2;
     }
    }
   mid1=mid+1;
   return mid1;}
   //写一个方法来进行插入新的值
  public static int[] insert(int[] a,int data){
    int[] b=new int[a.length+1];
    System.out.println("数组b的大小"+b.length);
    System.out.print("插入前的数组=[");
    for(int i=0;i<=b.length-2;i++){
     b[i]=a[i];
     System.out.print(b[i]+" ");}
    System.out.println("]");   //打印插入数值前的数组
    //插入数值
    if(mid1>=0||mid1<b.length-1){
    for(int i=b.length-2;i>=mid1;i--){
    b[i+1]=b[i];
    }
    b[mid1]=h;
    }else if(mid1==b.length-1){
     b[b.length-1]=h;
    }
   for(int i=0;i<=b.length-1;i++){
    System.out.print(b[i]+" ");
    }
   return b;}}

Test Results:

Here Insert Picture Description
Here Insert Picture Description

Here is a more important
int [] b = new int [ a.length + 1]; b is equal to the array size of the array a plus 1, then the sequence a [0] to a [a.length-1] Assignment to b [0] to b [b.length-2].
there is, find a insert subscript, how to move elements of it. This is similar to the linear form of insertion. There is a comment.

Guess you like

Origin blog.csdn.net/xiaoke5491/article/details/101714473