The fifth experiment

part1: use dichotomy to find the array location of data

Parameter is an array, parameter is an array name

// Exercise: use binary search to find data items in a set of ordered elements
//  Parameter is an array, parameter is an array name 
#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};
	int i,index, key;
	
	printf("array a Data in:\n");
	for(i=0;i<N;i++)
	   printf("%d ",a[i]);
	printf("\n");
	
	printf("Enter the data item to be found: ");
	scanf("%d", &key);
	
	// Call the function binarySearch() to find the specified data item in array a, and return the search result to index 
	index=binarySearch(a , N , key);                     // Complement code ① 
	 
	
	if(index>=0) 
		printf("%d In an array, the subscript is%d\n", key, index);
	else
		printf("%d Not in array\n", key); 
   
   return 0;
}


//Function function description:
//Use binary search algorithm to find specific value item in array x, the size of array x is n 
// If found, returns its subscript 
// If not, return to - 1 
int binarySearch(int x[], int n, int item) {
	int low, high, mid;
	
	low = 0;
	high = n-1;
	
	while(low <= high) {
		mid = (low+high)/2;
		
		if (item == x[mid])
			return mid;
		else if(item < x[mid])
			high = mid - 1;
		else
			low = mid + 1;
	}
	
	return -1;
}

Operation results

 

 

1.1 the parameter is a pointer variable and the actual parameter is an array name

// Exercise: use binary search to find data items in a set of ordered elements
//  Parameter is a pointer variable, parameter is an array name
#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};
	int i,index, key;
	
	printf("array a Data in:\n");
	for(i=0;i<N;i++)
	   printf("%d ",a[i]);
	printf("\n");
	
	printf("Enter the data item to be found: ");
	scanf("%d", &key);
	
	// Call the function binarySearch() to find the specified data item in array a and return the search result
	index=binarySearch(a ,N ,key);                               // Complement code ①
	 
	
	if(index>=0) 
		printf("%d In an array, the subscript is%d\n", key, index);
	else
		printf("%d Not in array\n", key); 
   
   return 0;
}

//Function function description:
//Use the binary search algorithm to find the item in the n data at the beginning of the data item pointed to by x
// If found, return to its location
// If not, return to - 1 
int binarySearch(int *x, int n, int item) {
	int low, high, mid;
	
	low = 0;
	high = n-1;
	
	while(low <= high) {
		mid = (low+high)/2;
		
		if (item == *(x+mid))
			return mid;
		else if(item < *(x+mid))
			high = mid - 1;
		else
			low = mid + 1;
	}
	
	return -1;
}

  

part2: Select Sorting Method

Sort by dictionary

#include <stdio.h>
#include <string.h>
void selectSort(char str[][20], int n ); // Function declaration, parameter str is a two-dimensional array name 
int main() {
    char name[][20] = {"John", "Alex", "Joseph", "Candy", "Geoge"};
    int i;
    
    printf("Output initial list:\n");
    for(i=0; i<5; i++)
        printf("%s\n", name[i]);
        
    selectSort(name, 5);  // Call the selection method to sort the strings in the name array
    
    printf("Output list in dictionary order:\n");
    for(i=0; i<5; i++)
        printf("%s\n", name[i]);
     
    return 0;
} 

// Function definition
// Function function description: use selection method to sort n strings in 2D array str in dictionary order 
void selectSort(char str[][20], int n) {
     int i,j,k; char temp[20];
     for(i=0;i<n-1;i++){
        k=i;
    for(j=i+1;j<n;j++)
        if(strcmp(str[j],str[k])<0)
         k=j;
         if(k!=i){
         
        strcpy(temp,str[i]);    
        strcpy(str[i],str[k]);
        strcpy(str[k],temp);}
    
     
}
                                   // Complement code
                                   // ××× 
}

  

part3: summary and experience

1. Array name as parameter VS pointer variable as parameter

Parameter parameter parameter parameter

How to write a n array

How to write a pointer int swap(int*p, int*q), P, q

2. When sorting strings using the selection method, it should be noted that the string processing function is needed to compare the relationship between strings,

Consider some boundary values clearly.

3. I don't know how to choose the sorting method.

Mutual comments:

https://www.cnblogs.com/xu9729/p/10918429.html

https://www.cnblogs.com/weiyuyang/p/10896239.html

https://www.cnblogs.com/silentisland/p/10913271.html

Keywords: PHP

Added by isiah on Tue, 05 Nov 2019 18:53:11 +0200