Sequential lookups and binary lookups

Source: Internet
Author: User

#include <iostream>
using namespace Std;
#define MAXSIZE 100
Class date{
Public
int date[maxsize + 1]; /* stored in 1 to n subscript storage, empty the location labeled 0 */
};
int Mindsearch (date&a, int k, int n); /* Search by two points */
int Sequentialsearch (Date &a, int k, int n); /* Find sequentially */
int main () {
Date A;
int i,n,k,j;
CIN >> N;
for (i = 1; i < n + 1; i++)
CIN >> A.date[i];
cout << "Enter the number of searches K:";
Cin >> K;
J=sequentialsearch (A, K, N);
if (j = = 0)
cout << "Search by order, not found" << K << Endl;
Else
cout << "Find in order" << K << "at the first" << J << "position" << Endl;
j = Mindsearch (A, K, N);
if (j = = 0)
cout << "Two-point search, not found" << K << Endl;
Else
cout << "binary search" << K << "at the first" << J << "position" << Endl;
return 0;
}
int Sequentialsearch (Date &a, int k,int n) {
A.date[0] = k;
int i;
for (i = n; A.date[i]!=k; i--);
return i; /* Not found return 0, find back subscript */
}
int Mindsearch (date&a,int k,int N) {
int right = N and left = 1;
int mind;
while (Left<=right) {
Mind = (left + right)/2;
if (a.date[mind] = = k)
return mind;
else if (A.date[mind] < K)
left = mind + 1;
Else
right = Mind-1;
}
return 0; /* Not found return 0*/
}

Sequential lookups and binary lookups

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.