Law One: Pointers
#include <stdio.h> #include <stdlib.h>void bubble_sort (int &NBSP;ARR[],&NBSP;INT&NBSP;SZ) {int i = 0;int j = 0;for (j = 0; j < sz - 1; j++) {for (i = 0; i < sz - 1 - j; i++) {if (* (arr + i) < * (arr + i + 1) {int tmp = * (arr + i); * (Arr + i) = * (arr + i + &NBSP;1); * (arr + i + 1) = tmp;}}} Int main () {int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };int i = 0;bubble_sort (arr, sizeof (arr) / sizeof (arr[0])); (i = 0; i < sizeof (arr) / sizeof (arr[0]), i++) {printf ("%d ", arr[i]);} printf ("\ n"); system ("pause"); return 0;}
Results:
9 8 7 6 5 4 3 2 1
Please press any key to continue ...
Law II: arrays
#include <stdio.h> #include <stdlib.h>void bubble_sort (int &NBSP;ARR[],&NBSP;INT&NBSP;SZ) {int i = 0;int j = 0;for (j = 0; j < sz - 1; j++) {for (i = 0; i < sz - 1 - j; i++) {if (arr[i] < arr[i + 1]) {int tmp = arr[i];arr[i] = arr[i + 1];arr[i + 1] = tmp;}}}} Int main () {int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };int i = 0;bubble_sort (arr, sizeof (arr) / sizeof (arr[0])); (i = 0; i < sizeof (arr) / sizeof (arr[0]), i++) {printf ("%d ", arr[i]);} printf ("\ n"); system ("pause"); return 0;}
Results:
9 8 7 6 5 4 3 2 1
Please press any key to continue ...
Implement bubble sort. (sort shaping array). Use both array and pointer methods.