Correlation lookup algorithm of sequential table in data structure

Source: Internet
Author: User
Tags array length comparison data structures

Today brings to you the relevant data structure of the search algorithm, I believe that read the previous several related data structure construction of readers should be able to understand the two commonly used data structures related to the basic knowledge.

Finding the basis of the algorithm requires data structure support, it is clear that we need to master the sequential table and dynamic list lookup method, I will answer each one.

In this article, we'll discuss the first . The lookup algorithm for sequential tables is simple, that is, the operation of an array subscript, which derives from three kinds of search algorithms:

They are simple sequential lookups, binary lookups of ordered tables, and index order table lookups .

1. Simple Order Lookup:

Requirements: The characteristics of the data table are not required, can be ordered or unordered.

method: A lookup is a process of traversal, starting at one end of the table, comparing elements individually, and, if successful, returning the record (the subscript of the Element), and the return of zero indicates failure.

The algorithm is simple: set a lookout a[0] to specify the element to locate, and if so, return the record, or return 0 to indicate failure.

Here is the code implementation:

#include <iostream>
using namespace std;
typedef int ELEMENTTYPE;
typedef int KEYTYPE;
int main () {
cout<< "Please enter the array length:";
int length;
cin>>length;
int *a1=new Int[length];
cout<<, enter the values of the sequential table in turn: "<<endl;
for (int i=0;i<length;i++) {
Cin>>a1[i];
}
int x;//defines the element value to look for
cout<< "Enter the element value to find:";
cin>>x;
int Sequential_search (ElementType a[],int n,keytype x);//Declaration simple Lookup Function
int y=sequential_search (A1,LENGTH,X);// Save the return value
if (y==-1) cout<< "Sorry, there is no" <<x<< "in this order table!" <<endl; The
Else cout<< "Simple Lookup method finds this number" <<x<< "the subscript value in the order table is:" <<y<<endl;
return 1;
}
//Simple order lookup is simple, just iterate through the array to determine
int sequential_search (ElementType a[],int n,keytype x) {
/*
   // This notation is traversed from the back forward, with I as subscript
int i=n;
A[0].key=x;
while (A[i].key!=x) i--;
Return i;*/
//Usually we use the following method to find a simple order of
for (int i=0;i<n;i++) {
if (a[i]==x) return i;
}
Return-1;
}

2. Binary lookup of ordered tables:

Requirements: The elements are arranged in an orderly manner, either in ascending order or in descending order

Method: Two-point search, so the name of meaning, there is binary meaning. In fact, this is the meaning, the process is very simple, as long as the element in the range of the sequence, you can start the binary search, in the sequence range of binary, compare with the median value, the median value again as a range boundary, and do two sub-search, much like recursion is right. In fact, you can use the recursive return to do, the following two ways I write the code of the search algorithm:

1. First this arrangement is ordered, if not ordered, then the sorting algorithm will be given to you in the next article, where the first assumption is already in order, and is ascending order (in order to make the reader easier to understand) I put the code runtime input process prompts in ascending order input:

#include <iostream>
using namespace Std;
typedef int ElementType;
typedef int KEYTYPE;
int main () {
cout<< "Please enter array length:";
int length;
cin>>length;
int *a1=new Int[length];
cout<< "Enter each value of the sequential table in ascending order:" <<endl;
for (int i=0;i<length;i++) {
cin>>a1[i];
}
int x;//defines the value of the element to be checked
cout<< "Please enter the value of the element to find:";
cin>>x;
int Binary_search (ElementType a[],int n,keytype x);//Declaration of Binary lookup function
int Y=binary_search (a1,length,x);//Save the return value
if (y==-1) cout<< "Sorry, this order table does not have the" <<x<< "this number!" <<endl;
else cout<< "Binary lookup method finds this number" <<x<< "the subscript value in the order table is:" <<y<<endl;
return 1;
}
Binary order lookup is also very simple, array subscript binary logarithmic group value comparison can be
int Binary_search (ElementType a[],int n,keytype x) {


int low=0;//Set the first subscript
int mid;//defines the intermediate subscript
int high=n-1;//define tail subscript
Because it is to search by range, to ensure that the first subscript is less than or equal to the tail subscript
while (Low<=high) {
Mid= (Low+high)/2;//get intermediate subscript
if (X==a[mid]) return mid;
else if (X<a[mid]) high=mid-1;
else low=mid+1;
}
return-1;
}

2. Binary lookup algorithm using recursion

#include <iostream>
using namespace Std;
typedef int ElementType;
typedef int KEYTYPE;
int main () {
cout<< "Please enter array length:";
int length;
cin>>length;
int *a1=new Int[length];
cout<< "Enter each value of the sequential table in ascending order:" <<endl;
for (int i=0;i<length;i++) {
cin>>a1[i];
}
int x;//defines the value of the element to be checked
cout<< "Please enter the value of the element to find:";
cin>>x;
int Binary_search (ElementType a[],int low,int high,keytype x);//Declaration of Binary lookup function
int Y=binary_search (a1,0,length-1,x);//Save the return value
if (y==-1) cout<< "Sorry, this order table does not have the" <<x<< "this number!" <<endl;
else cout<< "Binary recursive lookup method to find this number" <<x<< "the subscript value in the order table is:" <<y<<endl;
return 1;
}
Binary order lookup is also very simple, array subscript binary logarithmic group value comparison can be
int Binary_search (ElementType a[],int low,int high,keytype x) {
int mid;//defines the intermediate subscript
if (Low>high) return-1;//the first subscript is greater than the tail subscript, indicating that the lookup must fail
else{
Mid= (Low+high)/2;
if (X=a[mid]) return mid;
Recursively invokes itself, passing to itself, when the range is in [0,mid], the parameter high=mid-1
When the range is [Mid,high], the parameter low=mid+1 is transmitted;
else if (X<a[mid]) return Binary_search (a,low,mid-1,x);
else return Binary_search (a,low+1,high,x);
}
}

3. Search Algorithm for index order table

Requirements: Block order, what is the meaning of it. In a large table, the entire table is not an ordered table, but divided into blocks of time, each piece is ordered, this time for each piece to add an index number, this index number is usually used subscript to do, so index number of index table must be ordered. Can do is to find the maximum value of the index table, then we can do a two-point lookup, because of the time of the problem, here I give ideas, there is a need for code readers can be about the time, I will be updated in the late.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.