The c Programming language Chapter 7 Arrays Exp3: sort 8 numbers with bubble-sort STEPS 38383838131313 n=8 49494913272727 65651327303030 76132730 383838 1327 304949 4949 273065 65 56565 3076 7676767676 97 97 9797979797 234567
The C Programming Language Chapter 7 Arrays Exp3: sort 8 numbers with bubble-sort 38 49 65 76 13 27 30 97 2 38 49 65 13 27 30 76 97 3 38 49 13 27 30 65 76 97 4 38 13 27 30 49 65 76 97 5 13 27 30 38 49 65 76 97 6 13 27 30 38 49 65 76 97 7 49 38 65 97 76 13 27 30 1 n=8 38 49 76 9713 972 9730 97 13 76 76 7627 30 13 6527 6530 65 13 13 49 4930 4927 3827 380 38 STEPS: 30 27
The c Programming language Chapter 7 Arrays #include <stdio. h> main Input n numbers to a[1]. a[nl( int a[g], ij, t, for j=l to n-1 printf("Input 8 numbers: n") for(i=1;i<9;i++) for i=1 to n nf("9d",&a[]); lipai+l printf("n") F for(=1j<=7j++) alai+1 for(i=1;i<=8-j;i++) ifal]ai+ID Output al. an it=al]; ai=ai+ll; ai+1] printf("The sorted numbers: n") for(i=1;i<9;i++) printf(" %d",aiD)
The C Programming Language Chapter 7 Arrays Input n numbers to a[1]… a[n] for j=1 to n-1 for i=1 to n-j a[i]>a[i+1] T F a[i]a[i+1] Output a[1] … a[n] #include <stdio.h> main() { int a[9],i,j,t; printf("Input 8 numbers:\n"); for(i=1;i<9;i++) scanf("%d",&a[i]); printf("\n"); for(j=1;j<=7;j++) for(i=1;i<=8-j;i++) if(a[i]>a[i+1]) {t=a[i]; a[i]=a[i+1]; a[i+1]=t;} printf("The sorted numbers:\n"); for(i=1;i<9;i++) printf("%d ",a[i]); }
The c Programming language Chapter 7 Arrays Exp4: sort 7 numbers with selected-sort kk STEPS: 1: 13386597764927 k 2 13276597764938 13276597764938 3456 132738197764965 132738491769765 132738496519776 13273849657697
The C Programming Language Chapter 7 Arrays 1: [ 49 38 65 97 76 13 27 ] j 13 49 2: 13 [38 65 97 76 49 27 ] 27 38 3: 13 27 [65 97 76 49 38 ] 4: 13 27 38 [97 76 49 65 ] 5: 13 27 38 49 [76 97 65 ] 6: 13 27 38 49 65 [97 76 ] 13 27 38 49 65 76 97 k k k k j j j j j j j j j j k Exp4: sort 7 numbers with selected-sort STEPS:
The c Programming language #include <stdio. h maino Input n numbers a[1]. a[n] i int a[8,i,j,k,x printf("Input 7 numbers: In") for j=1 to n-1 for(i=1;i<8;i++) scanf("%d", &ai]) printf("n") for j=i+l to n for(i=1;i<7;i++) au]sakk ik-=i for(=i+1j<=7j++) if(al]a[k]= T i=k if(il=k alisa k (]; al=ak]; a[k=x; Output a[l]. a[n] printf("The sorted numbers: In") for(i=1;i<8;i++) printf(" %d"aiD)
The C Programming Language Chapter 7 Arrays Input n numbers a[1]…a[n] for i=1 to n-1 for j=i+1 to n a[j]<a[k] T F k=j Output a[1] … a[n] k=i a[i]a[k] T i != k F #include <stdio.h> main() { int a[8],i,j,k,x; printf("Input 7 numbers:\n"); for(i=1;i<8;i++) scanf("%d",&a[i]); printf("\n"); for(i=1;i<7;i++) { k=i; for(j=i+1;j<=7;j++) if(a[j]<a[k]) k=j; if(i!=k) { x=a[i]; a[i]=a[k]; a[k]=x;} } printf("The sorted numbers:\n"); for(i=1;i<8;i++) printf("%d ",a[i]); }
The c Programming language Chapter 7 Arrays 87.2 Two-dimensional Arrays ☆ Declaration number=row*col type array name [expr]expr 今 Storms Elem int a[3 14] float b[2][5] subscript or co ssed in Int a3, 4 storage oruc int a[3 ][2 a0J[0 aol a[O][0]a0][1] 234 a a[2]01 a[2][0]a[2][1 a[2][
The C Programming Language Chapter 7 Arrays §7.2 Two-dimensional Arrays ❖ Declaration: type array name[expr][expr] ❖ Storage order of elements: ⚫Elements are stored by rows,so the rightmost subscript, or column, varies fastest as elements are accessed in storage order. as int a[3][4]; float b[2][5]; int a[3,4]; () row column number=row*col int a[3][2] a[0][1] a[1][0] a[1][1] a[2][0] a[2][1] 0 1 4 5 2 3 a[0][0] a[0][0] a[0][1] a[1][0] a[1][1] a[2][0] a[2][1]