1 /* vio.c: Sort an array using an 'indirect sorting array'.
3 * I don't know whether the right translation is really
4 * 'indirect sorting array', but the portuguese name for
5 * this is 'vetor indireto de ordenacao'.
7 * The basic idea is: instead of moving array's elements
8 * from a position to another, we use another array only
9 * to store the indexes and move only the indexes.
11 * This can save time if you're sorting an array of big
12 * structures, because you'll only move integers instead
13 * of the structs itself.
15 * Luiz Fernando N. Capitulino
16 * <lcapitulino@gmail.com>
22 /* sort_vio(): sort an array of length 'len' pointed by 'p'
23 * using an 'indirect sorting array' (vio, in portuguese).
24 * Uses insertion sort. */
25 void sort_vio(int *p
, int *vio
, int len
)
29 for (i
= 0; i
< len
; i
++)
32 for (i
= 1; i
< len
; i
++) {
35 for (j
= i
- 1; j
>= 0 && p
[vio
[j
]] > p
[idx
]; j
--)
42 /* show_array_vio(): print an array of length 'len'
43 * pointed by 'p' using an 'indirect sorting array'
44 * (vio, in portuguese). */
45 void show_array_vio(int *p
, int *vio
, int len
)
49 for (i
= 0; i
< len
; i
++)
50 printf("%d ", p
[vio
[i
]]);
54 /* show_array(): print an array of length 'len'
56 void show_array(int *p
, int len
)
60 for (i
= 0; i
< len
; i
++)
69 int array
[SIZE
], vio
[SIZE
], i
;
73 for (i
= 0; i
< SIZE
; i
++)
74 array
[i
] = rand() % SIZE
;
76 show_array(array
, SIZE
);
77 sort_vio(array
, vio
, SIZE
);
78 show_array_vio(array
, vio
, SIZE
);