Summary of various basic algorithms (6)-search algorithms

Source: Internet
Author: User

Summary of various basic algorithms (6)-search algorithms

(ALL tests passed)

========================================================== ======================================

 

1. Simple search

In a group of unordered columns, find a specific value and return its position POS

Test environment: VC 6.0 (c)

# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include <time. h> <br/> # define Max 101 <br/> void input (INT num []) <br/>{< br/> int I; </P> <p> srand (unsigned) Time (null); <br/> for (I = 1; I <Max; I ++) <br/> num [I] = rand () % 100; <br/>}< br/> void output (INT num []) <br/>{< br/> int I; </P> <p> for (I = 1; I <Max; I ++) <br/>{< br/> printf ("% 5d", num [I]); <br/> If (0 = I % 10) <br/> printf ("/N"); <br/>}< br/> int find (in T num [], int X) <br/>{< br/> int I; <br/> for (I = 1; I <Max; I ++) <br/> If (x = num [I]) <br/> return I; <br/> return 0; <br/>}< br/> void main () <br/>{< br/> int X, POs, num [Max]; <br/> input (Num); </P> <p> printf ("num... :/N "); <br/> output (Num); </P> <p> printf (" Enter find num :"); <br/> scanf ("% d", & X); <br/> Pos = find (Num, x); <br/> If (POS) <br/> printf ("OK! % D is found in POS: % d/N ", X, POS); <br/> else <br/> printf (" Sorry! % D is not found... in num/N ", x); <br/>}< br/>

Running result:

========================================================== ============================

2. Half-Lookup

In an ordered sequence, gradually narrow the search scope until records are found or not found.

This algorithm first generates 100 unordered series at random, then uses the Quick Sort Algorithm to sort them into ordered series, and then uses the half-lookup algorithm.

Note:The sorting algorithm in this algorithm can be implemented by any algorithm in the previous sorting algorithm, such as selecting sorting, Bubble sorting, and fast sorting.

 

Test environment: VC 6.0 (c)

# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include <time. h> <br/> # define Max 101 <br/> void input (INT num []) <br/>{< br/> int I; </P> <p> srand (unsigned) Time (null); <br/> for (I = 1; I <Max; I ++) <br/> num [I] = rand () % 100; <br/>}< br/> void output (INT num []) <br/>{< br/> int I; </P> <p> for (I = 1; I <Max; I ++) <br/>{< br/> printf ("% 5d", num [I]); <br/> If (0 = I % 10) <br/> printf ("/N"); <br/>}< br/> void sort (I NT num [], int low, int high)/* Quick Sort */<br/> {<br/> int L, h; </P> <p> If (low <pigh) <br/>{< br/> L = low; <br/> H = high; <br/> num [0] = num [l];/* save failed */</P> <p> while (L <p) <br/> {<br/> while (L <H & num [H]> = num [0]) h --; <br/> num [l] = num [H]; <br/> while (L <H & num [l] <= num [0]) l ++; <br/> num [H] = num [l]; <br/>}< br/> num [l] = num [0]; /* insert into */</P> <p> sort (Num, low L-1); <br/> sort (Num, L + 1, high); <br />}< Br/> int find (INT num [], int X, int low, int high) <br/>{< br/> int mid; </P> <p> while (low <= high) <br/>{< br/> mid = (low + high)/2; /* Find is OK */</P> <p> If (x = num [Mid]) <br/> return mid; <br/> else if (x <num [Mid]) <br/> high = mid-1; <br/> else <br/> low = Mid + 1; <br/>}< br/> return 0; <br/>}< br/> void main () <br/>{< br/> int X, POs, num [Max]; <br/> input (Num); </P> <p> printf ("sort before... /n "); <br/> ou Tput (Num); <br/> sort (Num, 1, MAX-1); <br/> printf ("sort after... /n "); <br/> output (Num); </P> <p> printf (" Enter find num :"); <br/> scanf ("% d", & X); <br/> Pos = find (Num, X, 1, MAX-1); <br/> If (POS) <br/> printf ("OK! % D is found in POS: % d/N ", X, POS); <br/> else <br/> printf (" Sorry! % D is not found... in num/N ", x); <br/>}< br/>

Running result:

========================================================== ============================

 

 

Reference recommendations:

The path to Learning Algorithms

Summary of various basic algorithms (1) -- chain table

Summary of various basic algorithms (II)-Stack

Summary of various basic algorithms (III)-tree and binary tree

Summary of various basic algorithms (4)-graph and traversal

Summary of various basic algorithms (V)-sorting algorithms

Summary of various basic algorithms (6)-search algorithms

Summary of various basic algorithms (7)-common algorithms

12 interesting questions in C Language

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.