-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
sort.c
92 lines (86 loc) · 2.63 KB
/
sort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* Routines for randomized recursive quick-sort */
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
# include "nsga2.h"
# include "rand.h"
/* Randomized quick sort routine to sort a population based on a particular objective chosen */
void quicksort_front_obj(population *pop, int objcount, int obj_array[], int obj_array_size)
{
q_sort_front_obj (pop, objcount, obj_array, 0, obj_array_size-1);
return;
}
/* Actual implementation of the randomized quick sort used to sort a population based on a particular objective chosen */
void q_sort_front_obj(population *pop, int objcount, int obj_array[], int left, int right)
{
int index;
int temp;
int i, j;
double pivot;
if (left<right)
{
index = rnd (left, right);
temp = obj_array[right];
obj_array[right] = obj_array[index];
obj_array[index] = temp;
pivot = pop->ind[obj_array[right]].obj[objcount];
i = left-1;
for (j=left; j<right; j++)
{
if (pop->ind[obj_array[j]].obj[objcount] <= pivot)
{
i+=1;
temp = obj_array[j];
obj_array[j] = obj_array[i];
obj_array[i] = temp;
}
}
index=i+1;
temp = obj_array[index];
obj_array[index] = obj_array[right];
obj_array[right] = temp;
q_sort_front_obj (pop, objcount, obj_array, left, index-1);
q_sort_front_obj (pop, objcount, obj_array, index+1, right);
}
return;
}
/* Randomized quick sort routine to sort a population based on crowding distance */
void quicksort_dist(population *pop, int *dist, int front_size)
{
q_sort_dist (pop, dist, 0, front_size-1);
return;
}
/* Actual implementation of the randomized quick sort used to sort a population based on crowding distance */
void q_sort_dist(population *pop, int *dist, int left, int right)
{
int index;
int temp;
int i, j;
double pivot;
if (left<right)
{
index = rnd (left, right);
temp = dist[right];
dist[right] = dist[index];
dist[index] = temp;
pivot = pop->ind[dist[right]].crowd_dist;
i = left-1;
for (j=left; j<right; j++)
{
if (pop->ind[dist[j]].crowd_dist <= pivot)
{
i+=1;
temp = dist[j];
dist[j] = dist[i];
dist[i] = temp;
}
}
index=i+1;
temp = dist[index];
dist[index] = dist[right];
dist[right] = temp;
q_sort_dist (pop, dist, left, index-1);
q_sort_dist (pop, dist, index+1, right);
}
return;
}