范腾

博客园 首页 联系 订阅 管理

实验结论

1.二分查找

ex1_1

 1 // 练习:使用二分查找,在一组有序元素中查找数据项
 2 //  形参是数组,实参是数组名 
 3 #include  <stdio.h>
 4 const int N=7;
 5 int binarySearch(int x[], int n, int item);
 6 int main() {
 7     int a[N]={1,83,92,161,215,584,984};
 8     int i,index, key;
 9      
10     printf("数组a中的数据:\n");
11     for(i=0;i<N;i++)
12        printf("%d ",a[i]);
13     printf("\n");
14      
15     printf("输入待查找的数据项: ");
16     scanf("%d", &key);
17      
18     index=binarySearch(a,N,key);// 调用函数binarySearch()在数组a中查找指定数据项item,并返回查找结果给index 
19      
20     if(index>=0) 
21         printf("%d在数组中,下标为%d\n", key, index);
22     else
23         printf("%d不在数组中\n", key); 
24     
25    return 0;
26 }
27  
28  
29 //函数功能描述:
30 //使用二分查找算法在数组x中查找特定值item,数组x大小为n 
31 // 如果找到,返回其下标 
32 // 如果没找到,返回-1 
33 int binarySearch(int x[], int n, int item) {
34     int low, high, mid;
35      
36     low = 0;
37     high = n-1;
38      
39     while(low <= high) {
40         mid = (low+high)/2;
41          
42         if (item == x[mid])
43             return mid;
44         else if(item<x[mid])/*补足代码②*/
45             high = mid - 1;
46         else
47             low = mid + 1;
48     }
49      
50     return -1;
51 }

 

ex1_2

 1 // 练习:使用二分查找,在一组有序元素中查找数据项
 2 //  形参是指针变量,实参是数组名
 3 #include  <stdio.h>
 4 const int N=5;
 5 int binarySearch(int *x, int n, int item);
 6 int main() {
 7     int a[N]={1,3,9,16,21};
 8     int i,index, key;
 9     
10     printf("数组a中的数据:\n");
11     for(i=0;i<N;i++)
12        printf("%d ",a[i]);
13     printf("\n");
14     
15     printf("输入待查找的数据项: ");
16     scanf("%d", &key);
17     
18     index=binarySearch(a,N,key);    // 调用函数binarySearch()在数组a中查找指定数据项item,并返回查找结果
19     
20     if(index>=0) 
21         printf("%d在数组中,下标为%d\n", key, index);
22     else
23         printf("%d不在数组中\n", key); 
24    
25    return 0;
26 }
27 
28 //函数功能描述:
29 //使用二分查找算法在x指向的数据项开始的n个数据中,查找item
30 // 如果找到,返回其位置
31 // 如果没找到,返回-1 
32 int binarySearch(int *x, int n, int item) {
33     int low, high, mid;
34     
35     low = 0;
36     high = n-1;
37     
38     while(low <= high) {
39         mid = (low+high)/2;
40         
41         if (item == *(x+mid))
42             return mid;
43         else if(item<*(x+mid)/*补足代码②*/)
44             high = mid - 1;
45         else
46             low = mid + 1;
47     }
48     
49     return -1;
50 }

2.选择法排序

ex2-2

 1 // 练习:使用选择法对字符串按字典序排序
 2 #include <stdio.h>
 3 #include <string.h>
 4 void selectSort(char str[][20], int n ); // 函数声明,形参str是二维数组名 
 5 int main() {
 6     char name[][20] = {"John", "Alex", "Joseph", "Candy", "Geoge"};
 7     int i;
 8     
 9     printf("输出初始名单:\n");
10     for(i=0; i<5; i++)
11         printf("%s\n", name[i]);
12         
13     selectSort(name, 5);  // 调用选择法对name数组中的字符串排序
14     
15     printf("按字典序输出名单:\n");
16     for(i=0; i<5; i++)
17         printf("%s\n", name[i]);
18      
19     return 0;
20 } 
21 
22 // 函数定义
23 // 函数功能描述:使用选择法对二维数组str中的n个字符串按字典序排序 
24 void selectSort(char str[][20], int n) {
25     char ch1,min,ch3,*temp;
26     for(ch1=0;ch1<n-1;ch1++){
27         min=ch1;
28         for(ch3=ch1+1;ch3<n;ch3++)
29             if(strcmp(str[ch3],str[min])<0){
30                 min=ch3;
31             }
32         if(min!=ch1){
33             strcpy(temp,str[ch1]);
34             strcpy(str[ch1],str[min]);
35             strcpy(str[min],temp);
36         }    
37         
38         
39     }
40     
41     
42 }

 

 

这个实验与整数排序方法一致,因此只需参考ex2-1的算法很容易就能写出来,但要注意字符串的比较和赋值必须借助函数完成!

3.用指针处理字符串

 是直接使用的老师写好的代码

总结

1.二分查找中,数据需满足

     (1) 顺序储存

   (2)元素有序

          如由大到小排序,或由小到大排序

2. 形参:数组名,需带[ ],如  void delMiddleStar(char s[ ])

       函数声明中,变量名可省略,如 void delPrefixStar(char [ ])

    实参:数组 无[ ],如  index=binarySearch(a,N,key)中的 a 为数组

3.字符串的比较,赋值等操作,不能直接使用关系运算符和赋值运算符,要借助字符串处理函数

       如 字符串的比较使用 strcmp(  , )

           赋值使用 strcpy(   ,  )

      需予以注意,很容易因为习惯出错

posted on 2019-05-27 20:29  范腾  阅读(106)  评论(0编辑  收藏  举报