Skip to content

Commit fa8717f

Browse files
committed
퀵소트 구현
1 parent 354762d commit fa8717f

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed

QuickSort/HelpSorting.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
public class HelpSorting {
2+
static int cnt;
3+
4+
// Sort 배열 결과 출력
5+
static void printArrVer1(int[] arr, String sortName) {
6+
7+
System.out.println(sortName + "[" + arr.length + "]인 배열 정렬 결과.");
8+
System.out.println("Count the number of Key comparisons : " + cnt);
9+
if (arr.length < 40) {
10+
System.out.print("정렬된 값: ");
11+
for (int n = 0; n < arr.length; n++)
12+
System.out.print(arr[n] + " ");
13+
System.out.print("\n");
14+
}
15+
}
16+
17+
// Sort 클래스 안에서 count를 독립적으로 계산했을때 사용하는 배열 출력 메소드
18+
static void printArrVer2(int[] arr, String sortName,int cnt) {
19+
20+
System.out.println(sortName + "[" + arr.length + "]인 배열 정렬 결과.");
21+
System.out.println("Count the number of Key comparisons : " + cnt);
22+
if (arr.length < 40) {
23+
System.out.print("정렬된 값: ");
24+
for (int n = 0; n < arr.length; n++) {
25+
System.out.print(arr[n] + " ");
26+
}
27+
System.out.print("\n");
28+
}
29+
}
30+
31+
// a,b의 값을 비교하여 결과 반환
32+
static int Compare(int a, int b) {
33+
cnt++;
34+
if (a < b)
35+
return 1;
36+
else if (a == b)
37+
return 0;
38+
else
39+
return -1;
40+
}
41+
42+
//단순 스왑 함수
43+
static void swap(int arr[], int a, int b){
44+
int temp = arr[a];
45+
arr[a] = arr[b];
46+
arr[b] = temp;
47+
}
48+
}

QuickSort/QuickSortVer1.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// 마지막 값을 pivot으로 하는 퀵소트
2+
public class QuickSortVer1 extends HelpSorting {
3+
4+
static void sort(int[]arr, String sortName){
5+
cnt = 0;
6+
quickSortLastPivot(arr,0,arr.length-1);
7+
//정렬된 배열 출력
8+
printArrVer1(arr,sortName);
9+
}
10+
11+
private static void quickSortLastPivot(int[] arr, int left, int right){
12+
if(left >= right)
13+
return;
14+
15+
int mid = partitionLast(arr,left,right);
16+
quickSortLastPivot(arr,left,mid-1);
17+
quickSortLastPivot(arr,mid +1,right);
18+
}
19+
20+
private static int partitionLast(int[] arr, int left, int right){
21+
return partition(arr,left,right);
22+
}
23+
24+
25+
public static int partition(int[] arr, int left, int right) {
26+
int pivot = arr[right];
27+
int i = left -1;
28+
for(int j=left;j<right;j++) {
29+
if(Compare(arr[j],pivot)!=-1) {
30+
i++;
31+
swap(arr,i,j);
32+
}
33+
}
34+
swap(arr,i+1,right);
35+
return i+1;
36+
}
37+
}

QuickSort/QuickSortVer2.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// 랜덤pivot 퀵소트
2+
public class QuickSortVer2 extends HelpSorting {
3+
static void sort(int[]arr, String sortName){
4+
cnt = 0;
5+
6+
quickSortRandomPivot(arr,0,arr.length-1);
7+
//정렬된 배열 출력
8+
printArrVer1(arr,sortName);
9+
}
10+
11+
private static void quickSortRandomPivot(int[] arr, int left, int right){
12+
if(left >= right) {
13+
return;
14+
}
15+
16+
int mid = partitionRandom(arr,left,right);
17+
quickSortRandomPivot(arr,left,mid-1);
18+
quickSortRandomPivot(arr,mid +1,right);
19+
}
20+
21+
//피벗을 랜덤으로 고른 다음 맨마지막 위치로 이동시켜 기존의 파티션 코드 실행
22+
public static int partitionRandom(int[] arr, int left, int right) {
23+
int index = (int)(Math.random()*(right+1-left)+left);
24+
int pivot = arr[index];
25+
int i = left -1;
26+
27+
swap(arr,right,index);
28+
return partition(arr,left,right);
29+
}
30+
31+
public static int partition(int[] arr, int left, int right) {
32+
int pivot = arr[right];
33+
int i = left -1;
34+
for(int j=left;j<right;j++) {
35+
if(Compare(arr[j],pivot)!=-1) {
36+
i++;
37+
swap(arr,i,j);
38+
}
39+
}
40+
swap(arr,i+1,right);
41+
return i+1;
42+
}
43+
}

QuickSort/QuickSortVer3.java

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// pivot 은 median of three로 설정
2+
public class QuickSortVer3 extends HelpSorting {
3+
static void sort(int[]arr, String sortName){
4+
cnt = 0;
5+
6+
quickSortMedianOfThreePivot(arr,0,arr.length-1);
7+
//정렬된 배열 출력
8+
printArrVer1(arr,sortName);
9+
}
10+
11+
private static void quickSortMedianOfThreePivot(int[] arr, int left, int right){
12+
if(left >= right)
13+
return;
14+
15+
int mid = partitionMOT(arr,left,right);
16+
quickSortMedianOfThreePivot(arr,left,mid-1);
17+
quickSortMedianOfThreePivot(arr,mid +1,right);
18+
}
19+
20+
21+
22+
private static int partitionMOT(int[] arr, int left, int right) {
23+
int center = (left+right)/2;
24+
if(Compare(arr[left],arr[right])!=-1) {
25+
if(Compare(arr[center],arr[right])!=-1)
26+
swap(arr,center,right);
27+
}
28+
else {
29+
if(Compare(arr[center],arr[left])!=-1)
30+
swap(arr,center,right);
31+
else
32+
swap(arr,left,right);
33+
34+
}
35+
return partition(arr,left,right);
36+
}
37+
38+
public static int partition(int[] arr, int left, int right) {
39+
int pivot = arr[right];
40+
int i = left -1;
41+
for(int j=left;j<right;j++) {
42+
if(Compare(arr[j],pivot)!=-1) {
43+
i++;
44+
swap(arr,i,j);
45+
}
46+
}
47+
swap(arr,i+1,right);
48+
return i+1;
49+
}
50+
}

0 commit comments

Comments
 (0)