Experiment 5 Experiment report

Experimental results

1. binary search

ex1_1

@ Exercise: using a binary search to find the data items in an ordered set of elements 
// parameter is an array, the array name argument is 
#include <stdio.h> 
const int N =. 7; 
int binarySearch (int X [] , n-int, int Item); 
int main () { 
	int a [N] = {1,83,92,161,215,584,984}; 
	int I, index, Key; 
	
	the printf ( "in a data array: \ n-"); 
	for ( I = 0; I <N; I ++) 
	   the printf ( "% D", a [I]); 
	the printf ( "\ n-"); 
	
	the printf ( "enter be searched data items:"); 
	Scanf ( "% D" , & Key); 
	
	index = binarySearch (a, N, Key); // call the function binarySearch () to find the specified data item item in the array a, and returns the search results to the index 
	
	IF (index> = 0) 
		the printf ( "% D in an array, subscript D% \ n-", Key, index); 
	the else 
		the printf ("% D from the array \ n-", Key); 
   
   return 0;
} 


// Functions Function Description: 
// binary search algorithm used to find a specific item in the value of x in the array, the array size n x
// If found, it returns the subscript  
// If not found, returns -1
int binarySearch (int X [], n-int, int Item) { 
	int Low, High, MID; 
	
	Low = 0; 
	High-n-=. 1; 
	
	the while (Low <= High) { 
		MID = (High + Low) / 2; 
		
		IF (Item X == [MID]) 
			return MID; 
		the else IF (Item <X [MID]) / * * complement codes ② / 
			High MID = -. 1; 
		the else 
			Low = MID + . 1; 
	} 
	
	return -1; 
}

  

ex1_2

@ Exercise: using a binary search to find the data items in an ordered set of elements
 //   parameter is a pointer variable, the argument is the name of the array 
#include <stdio.h> const int N = . 5 ;
 int binarySearch ( int * X , int n-, int Item);
 int main () {
     int a [N] = { . 1 , . 3 , . 9 , 16 , 21 is };
     int I, index, Key; 
    the printf ( " array a data: \ n- " );
     for (I = 0 ; I <N; I ++ ) 
       the printf ( "
 
    % D " , A [I]); 
    the printf ( " \ n- " ); 
    
    the printf ( " Enter be searched data items: " ); 
    Scanf ( " % D " , & Key); 
    
    index = binarySearch (A, N, Key);     // call the function binarySearch () to find the specified data item entries in the array a, and returns the search results 
    
    IF (index> = 0 ) 
        the printf ( " % D in an array, subscript D% \ n- " , Key , index);
     the else 
        the printf ( " % D from the array \ n- " , Key); 
   
   return  0 ;
} 

// Functions Function Description:
// use the binary search algorithm in n data points to the start of a data item x, find Item
 // if found, return to its position
 // If not found, returns -1 
int binarySearch ( int * x, int n, int Item ) {
     int Low, High, MID; 
    
    Low = 0 ; 
    High = N- . 1 ; 
    
    the while (Low <= High) { 
        MID = (High + Low) / 2 ; 
        
        IF (Item == * (X + MID))
             return MID ;
         the else  IF (Item <* (X + MID) / * make up the code ② * / )
            high = mid - 1;
        else
            low = mid + 1;
    }
    
    return -1;
}

These two experiments are similar, a write another no problem, relatively simple.

2. Select Sorting

ex2-2

@ Exercise: using the selection method strings are sorted lexicographically 
#include <stdio.h> 
#include < String .h>
 void SelectSort ( char STR [] [ 20 is ], int n-); // function declaration parameter str is the name of a two-dimensional array 
int main () {
     char name [] [ 20 is ] = { " John " , " Alex " , " Joseph " , " Candy " , " Geoge " };
     int I; 
    
    the printf ( "Output initial list: \ n" );
     For (I = 0 ; <I . 5 I ++; ) 
        the printf ( " % S \ n- " , name [I]); 
        
    SelectSort (name, . 5 );   // call to the selection method to sort the array of string name 
    
    the printf ( " lexicographical ordering output list: \ n- " );
     for (I = 0 ; I < . 5 ; I ++ ) 
        the printf ( " % S \ n- " , name [I]); 
     
    return  0 ; 
} 

// function definition
 / / functions function description: selection method using a sort of two-dimensional array of n strings str lexicographical ordering 
void selectSort(char str[][20], int n) {
    char ch1,min,ch3,*temp;
    for(ch1=0;ch1<n-1;ch1++){
        min=ch1;
        for(ch3=ch1+1;ch3<n;ch3++)
            if(strcmp(str[ch3],str[min])<0){
                min=ch3;
            }
        if(min!=ch1){
            strcpy(temp,str[ch1]);
            strcpy(str[ch1],str[min]);
            strcpy(str[min],temp);
        }    
        
        
    }
    
    
}

This experiment is consistent with the integer sort method, so only reference ex2-1 algorithm is very easy to write, but to pay attention to the string comparison and the assignment must be completed by means of the function!

3. String Processing

1) understand the idea of ​​the algorithm, the program can be read, but to write their own, certainly not write to.

 

Guess you like

Origin www.cnblogs.com/angel-x/p/10902521.html