C#에서 활용가능한 5가지 SOTRING 소스코드
C#으로 5가지 정렬을 구현한 코드입니다.
선택정렬(SelectionSorting)
버블정렬(BubbleSorting)
삽입정렬(InsertionSorting)
쉘정렬(ShellSorting)
퀵정렬(QuickSorting)
//SelectionSort static void SelectionSort(int[] a, int N) { int min; for(int i = 1; i < N; i++) { min = i; for (int j = i + 1; j <= N; j++) { //내가 생각한 최소값보다 더 작은값이 발견되면 그값이 최소값이다. //최소값의 인덱스를 저장 //i+1부터 N까지 값들중 가장 최소값을 선택하여 i로 교환한다고 하여 //선택 정렬이라고 함 if (a[j] < a[min]) { min = j; } } //포인터 i와 최소값의 인덱스 min을 바꾼다 swap(a, min, i); } //결과출력 Console.Write("SelectionSort OUTPUT : "); for (int i = 1; i < a.Length; i++) { Console.Write(a[i] + " "); } Console.WriteLine(); } |
//BubbleSort static void BubbleSort(int[] a, int N) { for (int i = N; i >= 1; i--) { for (int j = 1; j < i; j++) { //포인터가 순서대로 지니간다. //인접한값(j와 j+1)을 비교해서 j가 j+1보다 크다면 바꾼다 //값이 큰값(무거운값)을 오른쪽으로 밀고 왼쪽값은 점점 가벼운값이 배치되어 //마치 거품이 떠오르는것 같다고 하여 버블정렬이라고 함 if (a[j] > a[j + 1]) swap(a, j, j + 1); } } //결과출력 Console.Write("BubbleSort OUTPUT : "); for (int i = 1; i < a.Length; i++) { Console.Write(a[i] + " "); } Console.WriteLine(); } |
다음으로 삽입정렬입니다.
//InsertionSort static void InsertionSort(int[] a, int N) { int temp, j; //삽입정렬은 2부터 시작 for (int i = 2; i <= N; i++) { //i번째 값을 기준으로 temp = a[i]; j = i; //j가 왼쪽으로 이동하면서 //그 값보다 큰값을 한칸씩 오른쪽으로 쉬프트 시킨다 while (a[j - 1] > temp) { a[j] = a[j - 1]; j--; } //이동이 없을경우 i == j //이동이 있을경우 j가 이동하여 빈자리에 temp값을 저장 a[j] = temp; } //결과출력 Console.Write("InsertionSort OUTPUT : "); for (int i = 1; i < a.Length; i++) { Console.Write(a[i] + " "); } Console.WriteLine(); } |
다음으로 쉘정렬입니다.
//ShellSort static void ShellSort(int[] a, int N) { //h=인덱스 간격 //h가 증가할때 3*h+1 //h가 감소할때 h /= 3 int h, v; for (h = 1; h < N; h = 3 * h + 1) ;//h를 N보다 적을때까지 3*h+1씩 증가시켜 최대의 h를 찾아냄 //인덱스 간격은 h /= 3만큼씩 변화 for (; h > 0; h /= 3) { //분리해낸 값들에 대해서 삽입정렬을 한다. for (int i = h + 1; i <= N; i++) { v = a[i]; int j = i; //i 포인터가 오른쪽으로 이동하면서 h보다(간격)크면 작동(기본조건) while (j > h && a[j - h] > v) { a[j] = a[j - h]; j -= h; } a[j] = v; } } //결과출력 Console.Write("ShellSort OUTPUT : "); for (int i = 1; i < a.Length; i++) { Console.Write(a[i] + " "); } Console.WriteLine(); } |
마지막으로 퀵소트입니다.
//QuickSort static void QuickSort(int[] a, int l, int r) { int i, j, v; if (r > l) { v = a[r]; i = l - 1; j = r; for (; ; ) { while (a[++i] < v) ; while (a[--j] > v) ; if (i >= j) break; swap(a, i, j); } swap(a, i, r); //피봇을 기준으로 좌 우 리커전 QuickSort(a, l, i - 1); QuickSort(a, i + 1, r); }
} |
배열값을 바꾸는데 필요한 함수입니다.
//Index로 배열의 값을 바꾸는 SWAP static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } |
마치기전에 테스트 코드 입니다.
static int[] b = new Int32[11]; static void Main(string[] args) { int[] array = { -1, 6, 2, 8, 1, 3, 9, 4, 5, 10, 7 }; //TEST Console.Write("INPUT : "); for (int i = 1; i < array.Length; i++) { Console.Write(array[i] + " "); } Console.WriteLine(); SelectionSort(array, array.Length - 1); //BubbleSort(array, array.Length - 1); //InsertionSort(array, array.Length - 1); //ShellSort(array, array.Length - 1); /* QuickSort(array, 1, array.Length - 1); //결과출력 Console.Write("QuickSort OUTPUT : "); for (int k = 1; k < array.Length; k++) { Console.Write(array[k] + " "); } Console.WriteLine(); */ } |
원하는 정렬방법에 주석을 풀었다가 해제했다가 하면
결과를 볼수 있습니다.
(하나마나 정렬된 결과는 똑같겠지만요)
마치겠습니다.