[C#/COMMON] 스무스 정렬하기
■ 스무스 정렬하는 방법을 보여준다. ▶ 스무스 정렬하기 예제 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; int[] array = new int[] { 10, 50, 30, 20, 90, 80, 15, 20 }; SmoothSort<int>(array); for(int i = 0; i < array.Length; i++) { Console.Write(array[i]); Console.Write(" "); } |
▶ 스무스 정렬하기 (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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
using System; #region Field /// <summary> /// 논리적 포인트 배열 /// </summary> [NonSerialized] private static int[] _logicalPointArray = new int[] { 1 , 1 , 3 , 5 , 9 , 15 , 25 , 41 , 67 , 109 , 177 , 287 , 465 , 753 , 1219 , 1973 , 3193 , 5167 , 8361 , 13529 , 21891 , 35421 , 57313 , 92735 , 150049 , 242785 , 392835 , 635621 , 1028457 , 1664079 , 2692537 , 4356617 , 7049155 , 11405773, 18454929, 29860703, 48315633, 78176337, 126491971, 204668309, 331160281, 535828591, 866988873 }; #endregion #region 스무스 정렬하기 - SmoothSort<T>(itemArray, lowIndex, highIndex) /// <summary>/// 스무스 정렬하기 /// </summary> /// <typeparam name="T">항목 타입</typeparam> /// <param name="itemArray">항목 배열</param> /// <param name="lowIndex">하위 인덱스</param> /// <param name="highIndex">상위 인덱스</param> public void SmoothSort<T>(T[] itemArray, int lowIndex, int highIndex) where T : IComparable { int headIndex = lowIndex; int index = 1; int shiftIndex = 1; while(headIndex < highIndex) { if((index & 3) == 3) { Sift(itemArray, shiftIndex, headIndex); index >>= 2; shiftIndex += 2; } else { if(_logicalPointArray[shiftIndex - 1] >= highIndex - headIndex) { Trinkle(itemArray, index, shiftIndex, headIndex, false); } else { Sift(itemArray, shiftIndex, headIndex); } if(shiftIndex == 1) { index <<= 1; shiftIndex--; } else { index <<= (shiftIndex - 1); shiftIndex = 1; } } index |= 1; headIndex++; } Trinkle(itemArray, index, shiftIndex, headIndex, false); while(shiftIndex != 1 || index != 1) { if(shiftIndex <= 1) { int trail = GetNumberOfTrailingZeros((uint)(index & ~1)); index >>= trail; shiftIndex += trail; } else { index <<= 2; index ^= 7; shiftIndex -= 2; Trinkle(itemArray, index >> 1, shiftIndex + 1, headIndex - _logicalPointArray[shiftIndex] - 1, true); Trinkle(itemArray, index, shiftIndex, headIndex - 1, true); } headIndex--; } } #endregion #region 스무스 정렬하기 - SmoothSort<T>(itemArray) /// <summary> /// 스무스 정렬하기 /// </summary> /// <typeparam name="T">항목 타입</typeparam> /// <param name="itemArray">항목 배열</param> public void SmoothSort<T>(T[] itemArray) where T : IComparable { SmoothSort(itemArray, 0, itemArray.Length - 1); } #endregion #region 분류하기 - Sift<T>(itemArray, shiftIndex, headIndex) /// <summary> /// 분류하기 /// </summary> /// <typeparam name="T">항목 타입</typeparam> /// <param name="itemArray">항목 배열</param> /// <param name="shiftIndex">이동 인덱스</param> /// <param name="headIndex">헤드 인덱스</param> private void Sift<T>(T[] itemArray, int shiftIndex, int headIndex) where T : IComparable { T item = itemArray[headIndex]; while(shiftIndex > 1) { int rootIndex = headIndex - 1; int leafIndex = headIndex - 1 - _logicalPointArray[shiftIndex - 2]; if(item.CompareTo(itemArray[leafIndex]) >= 0 && item.CompareTo(itemArray[rootIndex]) >= 0) { break; } if(itemArray[leafIndex].CompareTo(itemArray[rootIndex]) >= 0) { itemArray[headIndex] = itemArray[leafIndex]; headIndex = leafIndex; shiftIndex -= 1; } else { itemArray[headIndex] = itemArray[rootIndex]; headIndex = rootIndex; shiftIndex -= 2; } } itemArray[headIndex] = item; } #endregion #region 후행 0 수 구하기 - GetNumberOfTrailingZeros(value) /// <summary> /// 후행 0 수 구하기 /// </summary> /// <param name="value">값</param> /// <returns>후행 0 수</returns> private int GetNumberOfTrailingZeros(uint value) { int i = 32; while(value != 0u) { i--; value += value; } return i; } #endregion #region 트링클 - Trinkle<TItem>(itemArray, index, shiftIndex, headIndex, isTrusty) /// <summary> /// 트링클 /// </summary> /// <typeparam name="T">항목 타입</typeparam> /// <param name="itemArray">항목 배열</param> /// <param name="index">인덱스</param> /// <param name="shiftIndex">이동 인덱스</param> /// <param name="headIndex">헤드 인덱스</param> /// <param name="isTrusty">신뢰 여부</param> private void Trinkle<T>(T[] itemArray, int index, int shiftIndex, int headIndex, bool isTrusty) where T : IComparable { T item = itemArray[headIndex]; while(index != 1) { int stepIndex = headIndex - _logicalPointArray[shiftIndex]; if(itemArray[stepIndex].CompareTo(item) <= 0) { break; } if(!isTrusty && shiftIndex > 1) { int rootIndex = headIndex - 1; int leafIndex = headIndex - 1 - _logicalPointArray[shiftIndex - 2]; if(itemArray[rootIndex].CompareTo(itemArray[stepIndex]) >= 0 || itemArray[leafIndex].CompareTo(itemArray[stepIndex]) >= 0) { break; } } itemArray[headIndex] = itemArray[stepIndex]; headIndex = stepIndex; int trail = GetNumberOfTrailingZeros((uint)(index & ~1)); index >>= trail; shiftIndex += trail; isTrusty = false; } if(!isTrusty) { itemArray[headIndex] = item; Sift(itemArray, shiftIndex, headIndex); } } #endregion |