[Translation] C # data structures and algorithms-Chapter 4 Basic Search Algorithms

Source: Internet
Author: User

Chapter 2 Basic Search Algorithms

Data search is a basic computer programming task and has been studied for many years. This chapter only studies one aspect of finding a problem-finding a given value in the list (array.

There are two basic methods to search data in the list: sequential search and binary search. The ordered search applies when the items in the list are randomly arranged. The binary search applies to the sorted list.

 

Sequential search

This most obvious search type starts from the starting part of a record set and traverses each record until you find the record you are looking for or you are at the end of the record. This is called sequential search.

Sequential search (also known as linear search) is easy to use. Start from the array and compare each element of the accessed array with the value you want to search. If you find the matched element, the search ends. If a match cannot be obtained at the end of the array, it indicates that this value does not exist in the array.

The sequential search function is as follows:

BoolSeqSearch (Int[] Arr,IntSValue)

{

For(IntIndex = 0; index <arr. Length-1; index ++)

If(Arr [index] = sValue)

Return True;

Return False;

}

If a match is found, the program returns True immediately and exits. If the function at the end of the array does not return True, the value to be searched is not in the array and the function returns False.

The following is a program for testing our sequential search implementation:

UsingSystem;

UsingSystem. IO;

 

Public Class Chapter4

{

Static VoidMain ()

{

Int[] Numbers =New Int[100];

StreamReaderNumFile =File. OpenText ("C: \ numbers.txt");

For(IntI = 0; I <numbers. Length-1; I ++)

Numbers [I] =Convert. ToInt32 (numFile. ReadLine (), 10 );

IntSearchNumber;

Console. Write ("Enter a number to search :");

SearchNumber =Convert. ToInt32 (Console. ReadLine (), 10 );

BoolFound;

Found = SeqSearch (numbers, searchNumber );

If(Found)

Console. WriteLine (searchNumber +"Is in the array .");

Else

Console. WriteLine (searchNumber +"Is not in the array .");

}

 

Static BoolSeqSearch (Int[] Arr,IntSValue)

{

For(IntIndex = 0; index <arr. Length-1; index ++)

If(Arr [index] = sValue)

Return True;

Return False;

}

}

This program first reads a dataset from a text file. This number set contains 100 integers, which are stored in a file in partially random order. Then the program prompts the user to enter a number to be searched and calls the SeqSearch function to perform the search.

You can also write a search function in this order-the index is returned when the value to be searched is found, and-1 is returned if no value is found. First, let's take a look at the new function:

Static IntSeqSearch (Int[] Arr,IntSValue)

{

For(IntIndex = 0; index <arr. Length-1; index ++)

If(Arr [index] = sValue)

ReturnIndex;

Return-1;

}

The following program uses this function:

UsingSystem;

UsingSystem. IO;

 

Public Class Chapter4

{

Static VoidMain ()

{

Int[] Numbers =New Int[100];

StreamReaderNumFile =File. OpenText ("C: \ numbers.txt");

For(IntI = 0; I <numbers. Length-1; I ++)

Related Article

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.