- exe1-1
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 // 调用函数binarySearch()在数组a中查找指定数据项item,并返回查找结果给index 19 // 补足代码① 20 // ××× 21 index=binarySearch(a,N,key); 22 if(index>=0) 23 printf("%d在数组中,下标为%d\n", key, index); 24 else 25 printf("%d不在数组中\n", key); 26 27 return 0; 28 } 29 30 31 //函数功能描述: 32 //使用二分查找算法在数组x中查找特定值item,数组x大小为n 33 // 如果找到,返回其下标 34 // 如果没找到,返回-1 35 int binarySearch(int x[], int n, int item) { 36 int low, high, mid; 37 38 low = 0; 39 high = n-1; 40 41 while(low <= high) { 42 mid = (low+high)/2; 43 44 if (item == x[mid]) 45 return mid; 46 else if(item<x[mid])
47 high = mid - 1; 48 else 49 low = mid + 1; 50 } 51 52 return -1; 53 }
截图

- 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 // 调用函数binarySearch()在数组a中查找指定数据项item,并返回查找结果 19 // 补足代码① 20 // ××× 21 index=binarySearch(a,N,key); 22 if(index>=0) 23 printf("%d在数组中,下标为%d\n", key, index); 24 else 25 printf("%d不在数组中\n", key); 26 27 return 0; 28 } 29 30 //函数功能描述: 31 //使用二分查找算法在x指向的数据项开始的n个数据中,查找item 32 // 如果找到,返回其位置 33 // 如果没找到,返回-1 34 int binarySearch(int *x, int n, int item) { 35 int low, high, mid; 36 37 low = 0; 38 high = n-1; 39 40 while(low <= high) { 41 mid = (low+high)/2; 42 43 if (item == *(x+mid)) 44 return mid; 45 else if(item<*(x+mid)) 46 high = mid - 1; 47 else 48 low = mid + 1; 49 } 50 51 return -1; 52 }
运行截图

- 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 int i,j; 26 char t[20]; 27 for(i=0;i<n-1;i++) 28 for(j=0;j<n-i-1;j++) 29 { 30 if(strcmp(str[j],str[j+1])>0) 31 { 32 strcpy(t,str[j]); 33 strcpy(str[j],str[j+1]); 34 strcpy(str[j+1],t); 35 } 36 } 37 38 }
- 运行截图

- 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 { 26 int i,j,k; 27 char t[20]; 28 for(i=0;i<n-1;i++) 29 { 30 k=i; 31 for(j=i+1;j<n;j++) 32 { 33 if(strcmp(str[j],str[k])<0) 34 k=j; 35 } 36 if(k!=i) 37 { 38 strcpy(t,str[i]); 39 strcpy(str[i],str[k]); 40 strcpy(str[k],t); 41 } 42 } 43 }
结果同上
- 孰能生巧熟能生巧熟能生巧
https://www.cnblogs.com/plutocharon/p/10934443.html
https://www.cnblogs.com/yuan82/p/10908930.html
https://www.cnblogs.com/mgl1999/p/10933803.html
浙公网安备 33010602011771号