Sort 선택정렬

JAVA/Sort 2015. 10. 17. 15:22

import java.rmi.server.RemoteObjectInvocationHandler;

import java.util.ArrayList;

import java.util.Iterator;


public class Selection {


private static ArrayList arr;


public static void main(String[] args) {

// TODO Auto-generated method stub

Random(10000);

System.out.println("최소값은 " + reMin(arr, "min"));

System.out.println("최대값은 " + reMin(arr, "max"));


}


public static ArrayList<Integer> Random(int num) {

arr = new ArrayList<Integer>();

for (int i = 0; i < num; i++) {

int dec = (int) (Math.random() * num);

if (!arr.contains(dec)) {

arr.add(dec);

} else {

i--;

}

}


return arr;

}


public static int reMin(ArrayList<Integer> arr, String choice) {


selectSort(arr);


switch (choice) {

case "min":

return arr.get(0);

case "max":

return arr.get(arr.size() - 1);

default:

break;

}


return -1;

}


//선택정렬

public static ArrayList<Integer> selectSort(ArrayList<Integer> arr) {

int tmp;

for (int i = 0; i < arr.size(); i++) {

for (int j = i + 1; j < arr.size(); j++) {

if (arr.get(j) < arr.get(i)) {

tmp = arr.get(j);

arr.remove(j);

arr.add(j, arr.get(i));

arr.remove(i);

arr.add(i, tmp);

}

}

}


return arr;

}


}


블로그 이미지

왕왕왕왕

,