Random random = new Random();
//중복되는 숫자가 없도록 만들어주기
// int NumericRange는 랜덤 수의 범위를 정해준다
public ArrayList<Integer> NoDuplicateList(int ListLength , int NumericRange){
ArrayList<Integer> randomIntList = new ArrayList<Integer>();
boolean tf , checkboolean = false, same =true;
// 중복되는 숫자가 없도록 짜주자
for(int i=0; i< ListLength; i++){
tf = true;
if(i==0){
randomIntList.add(random.nextInt(NumericRange));
}else {
// 중복성 검사 중복 값이 있으면 다시 주입
int randnum;
while (tf) {
randnum = random.nextInt(NumericRange);
// System.out.println("try Input Number : "+randnum);
//중복성 검사 부분
for (Integer integer : randomIntList) {
//System.out.println("already numberList: "+integer.intValue());
if (randnum == integer) {
// 같은 값이 있다면 당장 break; 시키고 다시 값 넣어주기
same = false;
break;
}
same =true;
}
if(same){
randomIntList.add(randnum);
tf = false;
break;
}
}
}
}
return randomIntList;
}
Random random = new Random();
//중복이 있는 리스트
public ArrayList<Integer> duplicatesList(int length, int NumericRange){
ArrayList<Integer> arrayList = new ArrayList<Integer>();
for ( int i =0; i < length; i++) {
arrayList.add(randomNum(NumericRange));
}
정렬이란
핵심 항목(나이, 길이 등의 핵심 항목)의 대소관계에 따라 데이터 집합을
일정한 순서로 줄지어 늘어서도록 바꾸는 작업을 말합니다.
키값이 작은 데이터 순으로, 올라가는 그림
내려가는 그림, 가장높은 값 순으로 나오는 경우
(찾아보니 시작점이 head부분이나 끝부분 이렇게 두가지로 나뉘는 데, 큰 차이는 없다.)
public ArrayList<Integer> bubbleSort(ArrayList<Integer> arrayList){
int repetitionNum = 0;
for (int t: arrayList){
for(int i=0; i < arrayList.size()-repetitionNum;i++){
if(i+1 == arrayList.size()-repetitionNum){
}else {
if(arrayList.get(i) > arrayList.get(i+1)){
int tmp = arrayList.get(i);
arrayList.set(i, arrayList.get(i+1));
arrayList.set(i+1, tmp);
}
}
}
repetitionNum++;
}
return arrayList;
}
(오름차순)
이름 그대로 단순하게 가장 작은 값을 선택해서 정렬을 시작합니다.
(n = 0 : 배열의 길이)
public ArrayList<Integer> straightselectionSort(ArrayList<Integer> arrayList ){
// 타겟 인덱스는 처음부터
int targetIndex =0;
int minimum = arrayList.get(0); //인덱스 첫번째부터
int minumumIndex = 0;
for (int i:arrayList) {
minimum = arrayList.get(targetIndex);
// 먼저 가장 작은 값을 구함
for(int j=targetIndex; j<arrayList.size(); j++){
if(minimum > arrayList.get(j)){
minimum = arrayList.get(j);
minumumIndex = j;
}
}
if(targetIndex > arrayList.size()){
// 교환작업,
int tmp = arrayList.get(targetIndex);
arrayList.set(targetIndex, minimum);
arrayList.set(minumumIndex, tmp);
}
targetIndex++;
}
return arrayList;
}
public ArrayList<Integer> straightInsertionSort(ArrayList<Integer> arrayList){
int targetIndexNum = 0;
for(int i=0; i < arrayList.size();i++){
if(targetIndexNum==0){
targetIndexNum++;
}else {
int temp = arrayList.get(targetIndexNum);
for(int j =1; j < i; j++){
// target index
if(targetIndexNum-j < 0){
break;
}
// 타겟 인덱스의 값이 더 작으면 왼쪽으로 교환해줘야함
if(arrayList.get(targetIndexNum-j) > temp){
int chtemp = arrayList.get(targetIndexNum-j);
arrayList.set(targetIndexNum-j, temp);
arrayList.set(targetIndexNum, chtemp);
}else {
break;
}
targetIndexNum++;
}
}
}
return arrayList;
}