实验五
一: 二分查找
- 二分查找算法描述:
二分查找,也称折半查找,是一种高效的查找算法。使用二分查找,待查找的数据序列必须满足两 个条件:
① 顺序存储;
② 元素有序。
- 二分查找的方法:
把待查找数据项x与有序序列的中间项元素进行比较:(设有序数据序列是由小→大的)
- 如果item == 中间项元素,则表示找到;
- 如果item < 中间项元素,则在序列的前半部分以二分法继续查找;
- 如果item> 中间项元素,则在序列的后半部分以二分法继续查找。
实现方式1:形参是数组,实参是数组名,使用数组元素直接访问方式实现:
#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("数组a中的数据:\n"); for(i=0;i<N;i++) printf("%d ",a[i]); printf("\n"); printf("输入待查找的数据项: "); scanf("%d", &key); index=binarySearch(a,N,key); // 调用函数binarySearch()在数组a中查找指定数据项item,并返回查找结果给index if(index>=0) printf("%d在数组中,下标为%d\n", key, index); else printf("%d不在数组中\n", key); return 0; } //函数功能描述: //使用二分查找算法在数组x中查找特定值item,数组x大小为n // 如果找到,返回其下标 // 如果没找到,返回-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; }
实现方式2:形参是指针变量,实参是数组名,使用指针变量间接访问方式实现:
#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("数组a中的数据:\n"); for(i=0;i<N;i++) printf("%d ",a[i]); printf("\n"); printf("输入待查找的数据项: "); scanf("%d", &key); index=binarySearch(a,N,key); // 调用函数binarySearch()在数组a中查找指定数据项item,并返回查找结果 if(index>=0) printf("%d在数组中,下标为%d\n", key, index); else printf("%d不在数组中\n", key); return 0; } //函数功能描述: //使用二分查找算法在x指向的数据项开始的n个数据中,查找item // 如果找到,返回其位置 // 如果没找到,返回-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; }
二.:使用选择法对字符串按字典序排序:
#include <stdio.h> #include <string.h> void selectSort(char str[][20], int n ); // 函数声明,形参str是二维数组名 int main() { char name[][20] = {"John", "Alex", "Joseph", "Candy", "Geoge"}; int i; printf("输出初始名单:\n"); for(i=0; i<5; i++) printf("%s\n", name[i]); selectSort(name, 5); // 调用选择法对name数组中的字符串排序 printf("按字典序输出名单:\n"); for(i=0; i<5; i++) printf("%s\n", name[i]); return 0; } // 函数定义 // 函数功能描述:使用选择法对二维数组str中的n个字符串按字典序排序 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); } } }
三: 用指针处理字符串
- 假定输入的字符串中只包含字母和*,例如字符串****A*BC*DEF*G*******。编写子函数 delPrefixStar(),删除字符串中所有前导*删除,中间的和后面的*不删除。即删除后,字符串的内容应当是 A*BC*DEF*G*******
// 用指针变量处理字符串练习1 // 删除前导* #include <stdio.h> void delPrefixStar(char []); // 函数声明(函数声明中可以省略数组名不写) int main() { char string[80]; printf("输入一个字符串:\n"); gets(string); printf("\n删除<前导*>之前的字符串:\n"); puts(string); delPrefixStar(string); // 调用函数,删除前导*; 注意实参的写法 printf("\n删除<前导*>之后的字符串:\n"); puts(string); return 0; } // 函数定义 // 函数功能描述 // 删除字符数组s中前导* void delPrefixStar(char s[]) { char *target, *source; // 从字符串开始找到不是*的位置 source = s; while(*source == '*') source++; // 从这个位置开始将余下的字符前移 target = s; while( *target++ = *source++); }
- :假定输入的字符串中只包含字母和*,例如字符串****A*BC*DEF*G*******。编写子函数 delStarButPrefix(),除了前导*之外,删除其它*。即删除后,字符串的内容应当是****ABCDEFG
// 用指针变量处理字符串练习2 // 删除中间和末尾的* (即除了前导*,删除字符串中其它全部*) #include <stdio.h> void delStarButPrefix(char []); // 函数声明(函数声明中可以省略数组名不写) int main() { char string[80]; printf("输入一个字符串:\n"); gets(string); printf("\n删除<中间和末尾的*>之前的字符串:\n"); puts(string); delStarButPrefix(string); // 调用函数,删除中间和末尾的*; 注意实参的写法 printf("\n删除<中间和末尾的*>之后的字符串:\n"); puts(string); return 0; } // 函数定义 // 函数功能描述 // 删除字符数组s中除了前导*以外的所有*(即删除字符串中间和末尾出现的*) void delStarButPrefix(char s[]) { int i=0; // i用于记录字符在字符数组s中的下标 char *p = s; // 跳过前导*,i记录字符在字符数组s中的下标,p记录首个非*字符的位置 while(*p && *p == '*') { p++; i++; } // 从p指向的字符开始,把遇到的*删除 while(*p) { if(*p != '*') { s[i] = *p; i++; } p++; } s[i] = '\0'; // 在赋值过程中,结束标识符'\0'并没有被赋值进数组,所以要单独赋值 }
- 假定输入的字符串中只包含字母和*,例如字符串****A*BC*DEF*G*******。编写子函数 delMiddleStar(),除了前导*和尾部*之外,删除中间出现的所有*。即删除后,字符串内容应当是 ****ABCDEFG*******
// 用指针变量处理字符串练习3 // 删除字符串中间的* #include <stdio.h> void delMiddleStar(char []); // 函数声明(函数声明中可以省略数组名不写) int main() { char string[80]; printf("输入一个字符串:\n"); gets(string); printf("\n删除<中间的*>之前的字符串:\n"); puts(string); delMiddleStar(string); // 调用函数,删除字符串中间的*; 注意实参的写法 printf("\n删除<中间的*>之后的字符串:\n"); puts(string); return 0; } // 函数定义 // 函数功能描述 // 对字符数组s中存放的字符串,删除中间出现的* void delMiddleStar(char s[]) { int i=0; char *tail, *head, *p; // 找到末尾第一个非*字符的位置 tail = s; while(*tail) tail++; tail--; while(*tail == '*') tail--; // 找到开头第一个非*字符的位置 head = s; while(*head == '*') head++; // 把中间出现的*去掉 p = s; while(p<=head) { // 思考这里条件表达式为什么这样写,这个循环的功能? s[i] = *p; p++; i++; } while(p<tail) { if(*p != '*') { s[i] = *p; i++; } p++; } while(*p) { s[i] = *p; i++; p++; } s[i] = '\0'; // 在赋值过程中,结束标识符'\0'并没有被赋值进数组,所以要单独赋值 }
- 总结与体会:
1.还需要多理解数组元素地址与数组元素的区别,在使用时有时容易混淆:
2.字符串与字符数组的区别体现在:
- 字符串:必须以'\0'结尾; // ‘ \0 ’ 的意义是“字符串结束符”。
- 字符数组:可以包含多个'\0',但是如果当做字符串处理,则实际有效字符串为第一个'\0'的签名的字符串,如果当做字符数组处理,可以处理字符数组的任何一个字符,所有的字符都可以是'\0'。
互评地址:
https://www.cnblogs.com/dejizhuoma4637/p/10934978.html
https://www.cnblogs.com/txaalo/p/10934696.html
https://www.cnblogs.com/Bnuikl/p/10920697.html