Created
May 26, 2021 11:08
-
-
Save bepyan/1860a9ccc969e57b3db80f74e5eeef18 to your computer and use it in GitHub Desktop.
Sort Testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class $ { | |
public static <T extends Comparable<T>> int compare(T a, T b) { | |
return a.compareTo(b); | |
} | |
public static <T extends Comparable<T>> void swap(T[] a, int i, int j) { | |
T tmp = a[i]; | |
a[i] = a[j]; | |
a[j] = tmp; | |
} | |
public static <T extends Comparable<T>> void print(T[] arr) { | |
for (int i = 0; i < arr.length; i++) | |
System.out.print(arr[i] + " "); | |
System.out.println(); | |
} | |
interface Func<T extends Comparable<T>> { | |
void sort(T[] arr); | |
} | |
public static <T extends Comparable<T>> void test(String intro, T[] arr, T[] result, Func<T> func) { | |
System.out.println("====== " + intro + " ======"); | |
T[] target = arr.clone(); | |
// 정렬 시작. | |
long start = System.currentTimeMillis(); | |
func.sort(target); | |
long passing = System.currentTimeMillis() - start; | |
// 매번 Arrays 정렬을 할 수 없기 때문에 정렬된 결과를 인수로 가져온다. | |
System.out.println("정렬 결과는 " + Arrays.equals(target, result)); | |
System.out.println("소요시간(ms): " + passing + "\n"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class App { | |
public static void main(String[] args) { | |
Integer[] origin = { 2, 3, 4, 123, 413, 4, 23, 41, 24, 0, 124, 12 }; | |
Integer[] result = origin.clone(); | |
Arrays.sort(result); | |
$.test("bubble sort", origin, result, (arr) -> Bubble.sort(arr)); | |
$.test("selection sort", origin, result, (arr) -> Selection.sort(arr)); | |
$.test("insertion sort", origin, result, (arr) -> Insertion.sort(arr)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Bubble { | |
// 자기보다 작은 값을 만나면 swap | |
public static <T extends Comparable<T>> void sort(T a[]) { | |
int len = a.length; | |
for (int i = 0; i < len - 1; i++) { | |
for (int j = 0; j < len - i - 1; j++) | |
if ($.compare(a[j], a[j + 1]) > 0) | |
$.swap(a, j, j + 1); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Insertion { | |
// 정렬된 부분에 자신 위치를 삽입 (앞으로 가면서 하나하나 swap) | |
public static <T extends Comparable<T>> void sort(T a[]) { | |
int len = a.length; | |
for (int i = 1; i < len; i++) { | |
for (int j = i; j > 0; j--) { | |
if ($.compare(a[j], a[j - 1]) < 0) | |
$.swap(a, j, j - 1); | |
else | |
break; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Selection { | |
// 이후에 있는 최솟값과 swap하여 정렬 | |
public static <T extends Comparable<T>> void sort(T a[]) { | |
int len = a.length; | |
for (int i = 0; i < len; i++) { | |
int minIdx = i; | |
for (int j = i + 1; j < len; j++) | |
if ($.compare(a[j], a[minIdx]) < 0) | |
minIdx = j; | |
$.swap(a, i, minIdx); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment