📄 sort.c
字号:
/* Routines for randomized recursive quick-sort */# include <stdio.h># include <stdlib.h># include <math.h># include "global.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)
/*pop为待排序的种群,objcount为第多少个目标,obj_array用于传递并返回个体序号,left=0,right=front_size-1*///这种排序方法事实是采用的一种快速排序方法,即先将序列分成两边,小的在左边,大的在右边,采用递归的方法以实现排序
{ int index; int temp; int i, j; long double pivot; if (left<right) { index = rnd (left, right);
//obj_array[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;//i记录了目标值比pivot小的个体数目 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;
//找到比pivot小的个体有i个,因此可以将 上面随机产生序号的个体 放在i+1的位置,
//而该个存储在obj_array[right]中,因此可以将obj_array[i+1]与obj_array[right]互换而达到目的 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; long 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;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -