sort 인터페이스
public interface Sort {
void Ascending(int[] arr);
void Decending(int[] arr);
default void description() {
System.out.println("이 일고리즘은 정렬입니다");
}
}
QuickSort class
public class QuickSort implements Sort{
@Override
public void description() {
// TODO Auto-generated method stub
Sort.super.description();
System.out.println("이 알고리즘은 퀵정렬입니다.");
}
@Override
public void Ascending(int[] arr) {
// TODO Auto-generated method stub
System.out.println("Quicksort Ascending");
}
@Override
public void Decending(int[] arr) {
// TODO Auto-generated method stub
System.out.println("quicksort Descending");
}
}
HeapSort class
public class HeapSort implements Sort{
@Override
public void description() {
// TODO Auto-generated method stub
Sort.super.description();
System.out.println("이 알고리즘은 힙정렬입니다.");
}
@Override
public void Ascending(int[] arr) {
// TODO Auto-generated method stub
System.out.println("Heapsort Ascending");
}
@Override
public void Decending(int[] arr) {
// TODO Auto-generated method stub
System.out.println("Heapsort Descending");
}
}
BubbleSort class
public class BubbleSort implements Sort{
@Override
public void description() {
// TODO Auto-generated method stub
Sort.super.description();
System.out.println("이 알고리즘은 버블정렬입니다.");
}
@Override
public void Ascending(int[] arr) {
System.out.println("BubbleSort Ascending");
}
@Override
public void Decending(int[] arr) {
// TODO Auto-generated method stub
System.out.println("BubbleSort Descending");
}
}
main
import java.io.IOException;
public class sortMain {
public static void main(String[] args) throws IOException {
System.out.println("정렬방식을 선택하세요.");
System.out.println("B : BubbleSort");
System.out.println("H : HeapSort");
System.out.println("Q : QuickSort");
int ch = System.in.read();
Sort sort = null;
if(ch == 'B' || ch == 'b') {
sort = new BubbleSort();
}else if( ch == 'H' || ch == 'h') {
sort = new HeapSort();
}else if(ch == 'Q' || ch =='q') {
sort = new QuickSort();
}else {
System.out.println("지원되지않는 기능");
return;
}
int[] arr = new int[10];
sort.Ascending(arr);
sort.Decending(arr);
sort.description();
}
}