在遺傳算法中,只需要對適應性函數評分進行排序,沒必要對所有的個體也參與排序,因為在適應性函數評分排序是可以紀律下最初的索引,排序后的索引隨着元素排序而變動,這樣就知道那個評分對應那個個體了:

1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace ConsoleApplication1arraysort 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 int[] arr2 = new int[] {72,6,57,88,60,42,83,73,48,85 }; 15 List<int> mylist=new List<int> (); 16 mylist =arr2.ToList(); 17 int [] indexArr=new int[arr2.Length]; 18 for (int i = 0; i < indexArr.Length ; i++) 19 { 20 indexArr[i] = i; 21 } 22 23 QuickSortClass qs = new QuickSortClass(); 24 qs.QuickSort(mylist, 0, mylist.Count-1,indexArr ); 25 26 27 } 28 29 //http://www.jb51.net/article/86442.htm 30 public class QuickSortClass 31 { 32 public int Division(List<int> list, int left, int right,int[] mindexArr) 33 { 34 //首先挑選一個基准元素 35 int baseNum = list[left]; 36 int baseIndex = mindexArr[left]; 37 while (left < right) 38 { 39 //從數組的右端開始向前找,一直找到比base小的數字為止(包括base同等數) 40 while (left < right && list[right] >= baseNum) 41 right = right - 1; 42 //最終找到了比baseNum小的元素,要做的事情就是此元素放到base的位置 43 list[left] = list[right]; 44 mindexArr[left] = mindexArr[right]; 45 //從數組的左端開始向后找,一直找到比base大的數字為止(包括base同等數) 46 while (left < right && list[left] <= baseNum) 47 left = left + 1; 48 //最終找到了比baseNum大的元素,要做的事情就是將此元素放到最后的位置 49 list[right] = list[left]; 50 mindexArr[right] = mindexArr[left]; 51 } 52 //最后就是把baseNum放到該left的位置 53 list[left] = baseNum; 54 mindexArr[left] = baseIndex; 55 //最終,我們發現left位置的左側數值部分比left小,left位置右側數值比left大 56 //至此,我們完成了第一篇排序 57 return left; 58 } 59 60 public void QuickSort(List<int> list, int left, int right, int[] mindexArr) 61 { 62 //左下標一定小於右下標,否則就超越了 63 if (left < right) 64 { 65 //對數組進行分割,取出下次分割的基准標號 66 int i = Division(list, left, right,mindexArr); 67 68 //對“基准標號“左側的一組數值進行遞歸的切割,以至於將這些數值完整的排序 69 QuickSort(list, left, i - 1, mindexArr); 70 71 //對“基准標號“右側的一組數值進行遞歸的切割,以至於將這些數值完整的排序 72 QuickSort(list, i + 1, right, mindexArr); 73 } 74 } 75 } 76 77 78 79 80 81 82 } 83 }
修改,使之輸入為數組而不是list:

1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace ConsoleApplication1arraysort 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 double[] arr2 = new double[] { 72, 6, 57, 88, 60, 42, 83, 73, 48, 85 }; 15 int [] indexArr=new int[arr2.Length]; 16 for (int i = 0; i < indexArr.Length ; i++) 17 { 18 indexArr[i] = i; 19 } 20 21 QuickSortClass qs = new QuickSortClass(); 22 qs.QuickSort(arr2, 0, arr2.Length - 1, indexArr); 23 } 24 25 //http://www.jb51.net/article/86442.htm 26 public class QuickSortClass 27 { 28 public int Division(double[] list, int left, int right,int[] mindexArr) 29 { 30 //首先挑選一個基准元素 31 double baseNum = list[left]; 32 int baseIndex = mindexArr[left]; 33 while (left < right) 34 { 35 //從數組的右端開始向前找,一直找到比base小的數字為止(包括base同等數) 36 while (left < right && list[right] >= baseNum) 37 right = right - 1; 38 //最終找到了比baseNum小的元素,要做的事情就是此元素放到base的位置 39 list[left] = list[right]; 40 mindexArr[left] = mindexArr[right]; 41 //從數組的左端開始向后找,一直找到比base大的數字為止(包括base同等數) 42 while (left < right && list[left] <= baseNum) 43 left = left + 1; 44 //最終找到了比baseNum大的元素,要做的事情就是將此元素放到最后的位置 45 list[right] = list[left]; 46 mindexArr[right] = mindexArr[left]; 47 } 48 //最后就是把baseNum放到該left的位置 49 list[left] = baseNum; 50 mindexArr[left] = baseIndex; 51 //最終,我們發現left位置的左側數值部分比left小,left位置右側數值比left大 52 //至此,我們完成了第一篇排序 53 return left; 54 } 55 56 public void QuickSort(double[] list, int left, int right, int[] mindexArr) 57 { 58 //左下標一定小於右下標,否則就超越了 59 if (left < right) 60 { 61 //對數組進行分割,取出下次分割的基准標號 62 int i = Division(list, left, right,mindexArr); 63 64 //對“基准標號“左側的一組數值進行遞歸的切割,以至於將這些數值完整的排序 65 QuickSort(list, left, i - 1, mindexArr); 66 67 //對“基准標號“右側的一組數值進行遞歸的切割,以至於將這些數值完整的排序 68 QuickSort(list, i + 1, right, mindexArr); 69 } 70 } 71 } 72 73 74 75 76 77 78 } 79 }
list比較是還是得用單個元素,不能一條一條的比較啊:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication1Listadd 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 List<List<double>> twoDimeList = new List<List<double>>(); 14 for (int j = 0; j < 4; j++) 15 { 16 List<double> oneDimeList = new List<double>(); 17 for (int i = 0; i < 3; i++) 18 { 19 oneDimeList.Add(i+j); 20 } 21 twoDimeList.Add(oneDimeList); 22 } 23 twoDimeList.Add(new List<double> { 1.0,2.0,3.0}); 24 Console.WriteLine(twoDimeList.Count);//輸出行數 25 Console.WriteLine(twoDimeList[1][1]); 26 Console.WriteLine(twoDimeList[1]==twoDimeList[twoDimeList .Count-1]); 27 28 } 29 } 30 }