■ 쉘 정렬하는 방법을 보여준다.
▶ 쉘 정렬하기 예제 (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 }; ShellSort<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 |
using System; #region 쉘 정렬하기 - ShellSort<T>(itemArray) /// <summary> /// 쉘 정렬하기 /// </summary> /// <typeparam name="T">항목 타입</typeparam> /// <param name="itemArray">항목 배열</param> public void ShellSort<T>(T[] itemArray) where T : IComparable { int half = (int)Math.Round(itemArray.Length / 2d); while(half > 0) { for(int i = half; i < itemArray.Length; i++) { T temporaryItem = itemArray[i]; int j = i; while(j >= half && itemArray[j - half].CompareTo(temporaryItem) > 0) { itemArray[j] = itemArray[j - half]; j -= half; } itemArray[j] = temporaryItem; } half = (int)Math.Round(half / 2.2d); } } #endregion |