/* this program makes an array of pointer to int
and sorts the pointers so that they "point to sorted ints"*/
#include <stdio.h>
#define NB_POS 4
/*based on the shellsort algorithm*/
void sort_positions_pointers(int **pt_to_ints, int n){
int gap, i, j;
int *temp = NULL;
/*sorting the pointers*/
for(gap = n/2; gap > 0; gap /= 2){
for(i = gap; i < n; i++){
for(j = i-gap; j >= 0 && *(pt_to_ints[j]) > *(pt_to_ints[j + gap]); j -= gap){
temp = pt_to_ints[j];
pt_to_ints[j] = pt_to_ints[j + gap];
pt_to_ints[j + gap] = temp;
}
}
}
}
int main(void){
/*array of int*/
int positions[NB_POS] = {1200, 600, 1300, 2300};
/*array of pointers to int*/
int *positions_pointers[NB_POS] = {NULL, NULL, NULL, NULL};
int i;
i = 0;
/*initialise the array of pointers*/
while(i < NB_POS){
positions_pointers[i] = &(positions[i]);
i++;
}
/*display the unsorted array:*/
puts("non sorted array is: "); i = 0;
while(i < NB_POS){
printf("%d ", *positions_pointers
[i
]); i
++;
sort_positions_pointers(positions_pointers, NB_POS);
/*display the "sorted pointers to int" array:*/
puts("sorted array is: "); i = 0;
while(i < NB_POS){
printf("%d ", *positions_pointers
[i
]); i
++;
return 0;
}
LyogdGhpcyBwcm9ncmFtIG1ha2VzIGFuIGFycmF5IG9mIHBvaW50ZXIgdG8gaW50CmFuZCBzb3J0cyB0aGUgcG9pbnRlcnMgc28gdGhhdCB0aGV5ICJwb2ludCB0byBzb3J0ZWQgaW50cyIqLwojaW5jbHVkZSA8c3RkaW8uaD4KI2RlZmluZSBOQl9QT1MgNAoKLypiYXNlZCBvbiB0aGUgc2hlbGxzb3J0IGFsZ29yaXRobSovCnZvaWQgc29ydF9wb3NpdGlvbnNfcG9pbnRlcnMoaW50ICoqcHRfdG9faW50cywgaW50IG4pewoJaW50IGdhcCwgaSwgajsKCWludCAqdGVtcCA9IE5VTEw7CgkvKnNvcnRpbmcgdGhlIHBvaW50ZXJzKi8KCWZvcihnYXAgPSBuLzI7IGdhcCA+IDA7IGdhcCAvPSAy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