# subject

There is a group of numbers 12, 56, 45, 78, 90, 80, 23, 16, 8 and 63 stored in an array. Any number is received from the keyboard, and the number is searched in the array to give the information whether it is found. If found, it is required to output the position of the number in the array; If not found, output the prompt message that is not found!

# Problem solving steps

(1) Receiving;
(2) Search data;
(3) Comparison;
(4) Output results;

# Java

```import java.util.Scanner;

public class Demo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] array = {12, 56, 45, 78, 90, 80, 23, 16, 8, 63};
System.out.print("please enter the data you are looking for:");
int user = input.nextInt(), i;
for (i = 0; i <= 9; i++) {
if (user == array[i]) {
System.out.println(user + " " + "is element" + "[" + i + "]");
break;
}
if (i == 9)
System.out.println("no data " + user + " " + "found");
}
input.close();
}
}
```

# C language

## Sequential search

```#include <stdio.h>

int main()
{
int array[] = {12, 56, 45, 78, 90, 80, 23, 16, 8, 63}, TakeOver, Tag;
scanf("%d", &TakeOver);
for (int i = 0; i < 10; i++)
{
if (TakeOver == array[i])
{
printf("data %d location is array[%d];", TakeOver, i);
Tag = 1;
break;
}
else
Tag = 0;
}
if (Tag == 0)
printf("did not find %d", TakeOver);
return 0;
}
```

In this topic about "output of information not found", we can also think like this: if the last one of a group of data does not meet the conditions, it is deemed that there is no such data in the queue.
With this idea, we can make such changes: delete the Tag tag and add conditional judgment for direct output. Note the self increment of variable i here. In order to still enter the cyclic output information when i=10, the judgment condition should be modified to i < = 10.

```#include <stdio.h>

int main()
{
int array[] = {12, 56, 45, 78, 90, 80, 23, 16, 8, 63}, TakeOver;
scanf("%d", &TakeOver);
for (int i = 0; i <= 9; i++)
{
if (TakeOver == array[i])
{
printf("data %d location is array[%d];", TakeOver, i);
break;
}
else if (i == 9)
}
return 0;
}
```

## explain:

The most important thing in this question is how to compare the existing data with the user input data, that is, the operation of "search". Code 1 gives the simplest "sequential search" in the search algorithm. Note the if else statement here.

Since each time we enter the loop, the variable i increases and looks up one by one, if we only put a single output information in the statement without other judgment, it will cause an output error. Therefore, the variable Tag is added to judge the conditions: if the data are equal (found), the Tag value is set to 1. If not found, the Tag value is set to 0, and the judgment is carried out outside the loop to output the prompt information that is not found.
I tried to solve the problem with dichotomy, but I ignored that the premise of using dichotomy (halving) search must be sequential storage of ordered tables. Here are two versions of dichotomy, and pay attention to the applicable conditions of the algorithm.

## Binary search

```int Search1(int a[], int value, int n)
{
int low, high, mid;  //Define the lowest, highest and middle bits
low = 0;
high = n-1;   //If the data starts from 0, the last one is n-1, and the total number is still n
while(low<=high)
{
mid = (low+high)/2;
if(a[mid]==value)    //If the intermediate value is equal to the value to be found, it is returned directly
return mid;
if(a[mid]>value)     //The intermediate value is greater than the value to be found, the value to be found is on the left of the intermediate value, and the highest high is set to mid-1
high = mid-1;
if(a[mid]<value)     //The intermediate value is less than the value to be found. The value to be found is on the right of the intermediate value. The lowest value is low and set to mid+1
low = mid+1;
}
return -1;
}

//Recursive version
int Search2(int a[], int value, int low, int high)
{
int mid = low+(high-low)/2;      //The group median mid=(low+high)/2 is equivalent to mid = Low + 1 / 2 * (high low);
if(a[mid]==value)
return mid;
if(a[mid]>value)
return Search2(a, value, low, mid-1);  //recursive call
if(a[mid]<value)
return Search2(a, value, mid+1, high);
}
```

Added by keldorn on Sat, 25 Dec 2021 09:42:49 +0200