게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
4일 1코딩 연습(뻘글)(어제 술먹어서 오늘 쓰는 정렬연습)
게시물ID : programmer_22584짧은주소 복사하기
작성자 : 궭뷁뛣뉅
추천 : 0
조회수 : 584회
댓글수 : 0개
등록시간 : 2018/08/31 10:55:27
public class SortAlgorism {
public static void main(String[] args) {
Sort sort = new Sort();
getResult(sort.bubbleSort(getRandom(100)));
getResult(sort.selectionSort(getRandom(100)));
getResult(sort.insertionSort(getRandom(100)));
}
// 단순 출력
public static void getResult(int[] array) {
System.out.print("result => ");
for(int i : array) {
System.out.print(i + " ");
}
System.out.println("");
System.out.println("============================================");
}
// 중복 없는 랜덤 생성기
public static int[] getRandom(int length) {
int[] array = new int[length];
for(int a = 0; a < array.length; a++) {
array[a] = new Random().nextInt(length);
for(int b = 0; b < a; b++) {
if(array[a] == array[b]) {
a--;
}
}
}
System.out.print("array => ");
for(int i : array) {
System.out.print(i + " ");
}
System.out.println("");
return array;
}
}

public class Sort {
// 버블 정렬
public int[] bubbleSort(int[] array) {
long startTime = System.nanoTime();
int temp;

for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array.length - 1; j++) {
if(array[j] > array[j + 1]) {
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
}
long endTime = System.nanoTime();
System.out.println("Bubble Sort TIME : " + (endTime - startTime) / 1000.0 + "(ms)");

return array;
}
// 선택 정렬
public int[] selectionSort(int[] array) {
long startTime = System.nanoTime();
int temp;
for(int a = 0; a < array.length; a++) {
for(int b = a + 1; b < array.length; b++) {
if(array[a] > array[b]) {
temp = array[b];
array[b] = array[a];
array[a] = temp;
}
}
}
long endTime = System.nanoTime();
System.out.println("SelectionSort TIME : " + (endTime - startTime) / 1000.0 + "(ms)");
return array;
}
// 삽입 정렬
public int[] insertionSort(int[] array) {
long startTime = System.nanoTime();
int temp;
for(int i = 1; i<array.length; i++) {
temp = array[i];
int j = i;
for(; j > 0 && array[j - 1] > temp; j--) {
array[j] = array[j - 1];
}
array[j] = temp;
}
long endTime = System.nanoTime();
System.out.println("InsertionSort TIME : " + (endTime - startTime) / 1000.0 + "(ms)");
return array;
}
}

오늘은 기본 3개만.....
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호