fork download
  1. /* this program makes an array of pointer to int
  2. and sorts the pointers so that they "point to sorted ints"*/
  3. #include <stdio.h>
  4. #define NB_POS 4
  5.  
  6. /*based on the shellsort algorithm*/
  7. void sort_positions_pointers(int **pt_to_ints, int n){
  8. int gap, i, j;
  9. int *temp = NULL;
  10. /*sorting the pointers*/
  11. for(gap = n/2; gap > 0; gap /= 2){
  12. for(i = gap; i < n; i++){
  13. for(j = i-gap; j >= 0 && *(pt_to_ints[j]) > *(pt_to_ints[j + gap]); j -= gap){
  14. temp = pt_to_ints[j];
  15. pt_to_ints[j] = pt_to_ints[j + gap];
  16. pt_to_ints[j + gap] = temp;
  17. }
  18. }
  19. }
  20. }
  21.  
  22. int main(void){
  23. /*array of int*/
  24. int positions[NB_POS] = {1200, 600, 1300, 2300};
  25. /*array of pointers to int*/
  26. int *positions_pointers[NB_POS] = {NULL, NULL, NULL, NULL};
  27. int i;
  28.  
  29. i = 0;
  30. /*initialise the array of pointers*/
  31. while(i < NB_POS){
  32. positions_pointers[i] = &(positions[i]);
  33. i++;
  34. }
  35.  
  36. /*display the unsorted array:*/
  37. puts("non sorted array is: ");
  38. i = 0;
  39. while(i < NB_POS){
  40. printf("%d ", *positions_pointers[i]); i++;
  41. } puts("");
  42.  
  43. sort_positions_pointers(positions_pointers, NB_POS);
  44.  
  45. /*display the "sorted pointers to int" array:*/
  46. puts("sorted array is: ");
  47. i = 0;
  48. while(i < NB_POS){
  49. printf("%d ", *positions_pointers[i]); i++;
  50. } puts("");
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0s 2168KB
stdin
Standard input is empty
stdout
non sorted array is: 
1200 600 1300 2300 
sorted array is: 
600 1200 1300 2300