Profil de Fengthis.Pray();PhotosBlogListesPlus ![]() | Aide |
|
17 mars 我还是一名IT人员啊1 class QuickSorter 2 { 3 public void DoSort(List<int> list) 4 { 5 QSort(list, 0, list.Count - 1); 6 } 7 8 private void QSort(List<int> list, int l, int u) 9 { 10 if (l >= u) 11 { 12 //lower >= upper, return 13 return; 14 } 15 16 int m = l; //at first, set the middle at left 1st slot 17 18 for (int i = l + 1; i <= u; i++) //start from left 2nd slot 19 { 20 if (list[i] < list[l]) //compare with pivotValue(left 1st item: list[l]) 21 { 22 //if current list[i] < the left 1st item value 23 //"push" the smaller item before middle index by 1) middle index ++, 2) swap the smaller number and middle index item 24 m++; 25 Swap(list, i, m); 26 } 27 } 28 29 //now "i" reaches upper, the final step is set pivotValue(list[l]) to its correct middle position 30 Swap(list, l, m); 31 //Okay, we have set the pivotValue(list[l]) at its correct middle position of list[u, l] 32 //list[l, m-1] <= list[m] <= list[m+1, u] 33 34 QSort(list, l, m - 1); 35 QSort(list, m + 1, u); 36 } 37 38 private void Swap(List<int> list, int x, int y) 39 { 40 int temp = list[x]; 41 list[x] = list[y]; 42 list[y] = temp; 43 } 44 } |
|
|