8.21
#include <stdio.h> void bubbleSort(int **p, int n); int main(void){ int a[100]; int *b[100]; int n, i, **p; printf("Input your n(n < 100) "); scanf("%d", &n); printf("Input your a[n] "); for(i = 0; i < n; i++) scanf("%d", a + i); for(i = 0; i < n; i++) b[i] = a + i; p = b; bubbleSort(p, n); for(i = 0; i < n; i++) printf("%d ", *b[i]); printf(" "); return 0; } void bubbleSort(int **p, int n){ int i, j, flag = 1; int *temp; for(i = 0; flag; i++){ flag = 0; for(j = n - 1; j > i; j--){ flag = 1; if(**(p + i) > **(p + j)){ temp = *(p + i); *(p + i) = *(p + j); *(p + j) = temp; } } } }
8.20
#include <stdio.h> #include <string.h> void selectionSort(char **p); int main(void) { char a[5][30], *b[5], **p; int i; for(i = 0; i < 5; i++) b[i] = a[i]; for(i = 0; i < 5; i++) scanf("%s", b[i]); p = b; selectionSort(p); printf(" "); for(i = 0; i < 5; i++) printf("%s ", b[i]); return 0; } void selectionSort(char **p) { int i, j, minj, n = 5; char *temp; for(i = 0; i < n - 1; i++) { minj = i; for(j = i; j < n; j++) { if(strcmp(*(p + j), *(p + minj)) < 0) minj = j; } temp = *(p + i); *(p + i) = *(p + minj); *(p + minj) = temp; } }
8.16
#include <stdio.h> int getnum(char str[], int a[]); int main(void) { char str[100] = {}, *pstr; int a[100] = {}, total, *p, i; p = a; pstr = str; printf("Input your string "); gets(pstr); total = getnum(str, a); for(i = 0; a[i] != '