-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
530 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package day05; | ||
import java.util.*; | ||
public class BubbleSort { | ||
//앞에서부터 버블정렬하기 => 1회 반복돌고 나면 가장 큰수가 오른쪽에 가있음 | ||
public static void bubble1(int []arr) { | ||
//중첩 for루프 이용해서 버블 정렬을 해봅시다. | ||
for(int i=0; i<arr.length-1; i++) { | ||
for(int j=0; j<arr.length-1-i; j++) { | ||
if(arr[j]>arr[j+1]) { | ||
swap(arr, j, j+1); | ||
} | ||
}//for j------------ | ||
System.out.println(i+": "+Arrays.toString(arr)); | ||
}//for i------------------ | ||
}//------------------------------------------------------------- | ||
|
||
//뒤에서 부터 버블정렬하기 => 1회 반복돌고 나면 가장 작은수가 왼쪽에 가있다. | ||
public static void bubble2(int[] arr) { | ||
int cnt=0; | ||
for(int i=0; i<arr.length-1; i++) { | ||
for(int j=arr.length-1; j>i; j--) { | ||
if(arr[j-1]>arr[j]) { | ||
swap(arr, j-1, j); | ||
} | ||
cnt++; | ||
} | ||
System.out.println(i+": "+Arrays.toString(arr)); | ||
} | ||
System.out.println("2. cnt: "+cnt); | ||
}//---------------------------------------- | ||
|
||
public static void swap(int[] arr, int m, int n){ | ||
int tmp=arr[m]; | ||
arr[m]=arr[n]; | ||
arr[n]=tmp; | ||
}//------------------------------------------- | ||
|
||
//int [] arr2= {1,3,6,4,7,8,9}; 이 경우는 반복문 1회만에 정렬을 완료함에도 쓸데 없이 반복문을 계속 수행 | ||
public static void bubble3(int[] arr) { | ||
int cnt=0; | ||
for(int i=0; i<arr.length-1; i++) { | ||
int k=0; | ||
for(int j=arr.length-1; j>i; j--) { | ||
if(arr[j-1]>arr[j]) { | ||
swap(arr, j-1, j); | ||
k++; // 교환 횟수를 세보자. swap이 일어나면 k를 1씩 증가 | ||
} | ||
cnt++; | ||
if(k==0) break; | ||
//k가 0이라면 교환이 없단느 의미 => 정렬을 이미 마쳤다는 의미 | ||
}//for j------------ | ||
}//for i----------------- | ||
System.out.println("3. cnt: "+ cnt); | ||
}//----------------------------------------------- | ||
|
||
|
||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
System.out.println("배열 크기 입력: "); | ||
int n = sc.nextInt(); | ||
int [] arr = new int[n]; | ||
int [] arr2= {1,3,6,4,7,8,9}; | ||
Random ran = new Random(); | ||
for(int i=0; i<arr.length; i++) { | ||
arr[i]=ran.nextInt(10)+1;// 1<= r <=10 | ||
//(int)(Math.random()*범위 + 시작수) | ||
//ran.nextInt(범위) + 시작수 | ||
} | ||
System.out.println(Arrays.toString(arr)); | ||
// bubble1(arr); | ||
// bubble2(arr); | ||
bubble2(arr2); | ||
System.out.println("----------------------------"); | ||
bubble3(arr2); | ||
System.out.println(Arrays.toString(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package day05; | ||
|
||
import java.util.*; | ||
//선택한 요소와 배열에 저장된 값의 비교를 통해 삽입할 위치를 찾는 방식 | ||
//삽입정렬은 어느정도 정렬이 되어 있는 배열일 경우 O(n) | ||
// 정렬이 잘 안되어 있는 배열일 경우 O(n^2) | ||
public class InsertSort { | ||
|
||
public static void insertSort(int[] arr) { | ||
for(int i=1; i<arr.length; i++) { | ||
int tmp=arr[i]; | ||
int j=i-1; | ||
for(; j>=0; j--) { | ||
if(arr[j]>tmp) { | ||
arr[j+1]=arr[j]; | ||
}else { | ||
break; | ||
} | ||
}//for j-------------- | ||
arr[j+1]=tmp; | ||
//삽입할 위치를 찾았다면 tmp를 해당 위치에 삽입한다 | ||
}//for i---------------- | ||
}////////////////////////// | ||
|
||
public static void insertSort2(int[] arr) { | ||
for(int i=1; i<arr.length; i++) { | ||
int tmp=arr[i]; | ||
int j=i-1; | ||
for(; j>=0 && arr[j]>tmp ; j--) { | ||
arr[j+1]=arr[j]; | ||
} | ||
arr[j+1]=tmp; | ||
//삽입할 위치를 찾았다면 tmp를 해당 위치에 삽입한다 | ||
}//for i---------------- | ||
}////////////////////////// | ||
|
||
public static void main(String[] args) { | ||
int arr[] = {7,8,4,1,9,2}; | ||
System.out.println(Arrays.toString(arr)); | ||
insertSort(arr); | ||
System.out.println("----삽입 정렬 후-------------------------"); | ||
System.out.println(Arrays.toString(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package day05; | ||
|
||
import java.util.Arrays; | ||
|
||
//정렬을 완료한 2개의 배열을 병합해보자. | ||
public class MergeArray { | ||
|
||
public static void main(String[] args) { | ||
int[] a = { 2, 4, 6, 8, 11, 13 }; | ||
int b[] = { 1, 2, 3, 5, 9, 12, 21 }; | ||
int c[] = new int[a.length + b.length]; | ||
System.out.println("a: " + Arrays.toString(a)); | ||
System.out.println("b: " + Arrays.toString(b)); | ||
System.out.println("c: " + Arrays.toString(c)); | ||
|
||
merge(a, b, c); | ||
|
||
System.out.println("-----배열 a와 b를 병합하여 c에 저장했습니다.------------"); | ||
System.out.println("c: " + Arrays.toString(c)); | ||
|
||
} | ||
|
||
// 정렬이 완료된 배열 a,b를 병합하여 c에 저장하는 메서드 | ||
private static void merge(int[] a, int[] b, int[] c) { | ||
int pa=0; | ||
int pb=0; | ||
int pc=0; | ||
//a에 저장된 값과 b에 저장된 값을 비교해서 더 작은 쪽을 c에 저장한다. 그러면서 작은쪽의 포인터를 증가 | ||
/*while(pa<a.length && pb<b.length) { | ||
if(a[pa]<b[pb]) { | ||
c[pc++]=a[pa++]; | ||
}else { | ||
c[pc++]=b[pb++]; | ||
} | ||
}//while-----------------------*/ | ||
|
||
while(pa<a.length)//a에 남아있는 요소를 모두 c에 저장 | ||
c[pc++]=(a[pa]<b[pb])? a[pa++]: b[pb++]; | ||
} | ||
|
||
static public void printProcess(int[]a, int[] b, int[] c) { | ||
System.out.println("----------------------------"); | ||
System.out.println("a: "+Arrays.toString(a)); | ||
System.out.println("b: "+Arrays.toString(b)); | ||
System.out.println("c: "+Arrays.toString(c)); | ||
System.out.println("------------------------------"); | ||
|
||
} | ||
|
||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package day05; | ||
|
||
import java.lang.reflect.Array; | ||
import java.util.Arrays; | ||
|
||
public class MergeSort { | ||
|
||
public static void main(String[] args) { | ||
int arr[]= {6,5,13,7,1,9,8,15,3}; | ||
System.out.println(Arrays.toString(arr)); | ||
|
||
partition(arr, 0, arr.length-1); | ||
System.out.println("--병합 정렬 이후------------------"); | ||
System.out.println(Arrays.toString(arr)); | ||
}//------------------------------------------- | ||
|
||
static void partition(int[] arr, int left, int right) { | ||
if(left==right) return; | ||
//분할하다 보면(2로 나누기) left==right순간이 생긴다 | ||
int mid=(left+right)/2; | ||
partition(arr, left, mid); // 전반부 분할 | ||
partition(arr, mid+1, right); //후반부 분할 | ||
merge2(arr, left, right);//2개로 나뉜 배열을 병합해주는 메서드 호출 | ||
}//---------------------------------------------------- | ||
static void merge(int[] a, int left, int right) { | ||
int tmp[]=new int[a.length];//임시 저장소 | ||
int index; | ||
int pc=(left+right)/2; | ||
int pl=left;//첫번째 배열방의 인덱스를 저장 | ||
int pr=pc+1;//두번째 배열방의 인덱스를 저장 | ||
index=left; | ||
while(pl<=pc && pr<=right) { | ||
tmp[index++]=a[pl]<a[pr] ? a[pl++]:a[pr++]; | ||
} | ||
int i; | ||
if(pl>pc) { | ||
for(i=pr; i<=right;i++) | ||
tmp[index++]=a[i]; | ||
}else { | ||
//반대인 경우는 왼쪽에 있는 부분배열의 남은 값을 전부 복사 | ||
for(i=pl; i<=pc;i++) | ||
tmp[index++]=a[i]; | ||
} | ||
for(i=left;i<=right;i++) | ||
a[i]=tmp[i];//원본배열에 임시배열값을 카피 | ||
}//------------------------------------------ | ||
|
||
static void merge2(int[] arr, int left, int right) { | ||
int tmp[]=new int[arr.length]; | ||
int index=0; | ||
//비교해서 병합해준 뒤 | ||
int pc=(left+right)/2; | ||
int pl=left; | ||
int pr=pc+1; | ||
index=left; | ||
while(pl<=pc&& pr<=right) { | ||
tmp[index++]=(arr[pl]<arr[pr])? arr[pl++]:arr[pr++]; | ||
//두 배열 중 더 작은 값을 tmp배열에 넣어준다 | ||
|
||
} | ||
//남은 것들이 있으면 옮기자 | ||
int i; | ||
if(pl>pc) { | ||
for(i=pr; i<=right; i++) | ||
tmp[index++]=arr[i]; | ||
}else { | ||
for(i=pl; i<=pc; i++) | ||
tmp[index++]=arr[i]; | ||
} | ||
|
||
//tmp에 있는 값을 다시 arr에 옮겨준다 | ||
System.out.println(left+"/"+right); | ||
for(i=left; i<=right; i++) { | ||
arr[i]=tmp[i]; | ||
} | ||
System.out.println(Arrays.toString(tmp)); | ||
}//---------------------------- | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package day05; | ||
|
||
import java.util.Arrays; | ||
|
||
/*퀵 정렬 알고리즘을 살펴보기 위해 피벗을 하나 정해 2그룹으로 나누어 데이터를 교환하는 작업을 수행해보자. | ||
* */ | ||
public class Partition { | ||
public static void main(String[] args) { | ||
int arr[] = {1,8,7,4,5,2,6,3,9}; | ||
partition(arr); | ||
}//--------------------------------- | ||
|
||
static void partition(int arr[]) { | ||
int pl=0;//증가 | ||
int pr=arr.length-1;//감소 | ||
int pivot=arr[arr.length/2]; | ||
|
||
do { | ||
while(arr[pl]<pivot) pl++; | ||
while(arr[pr]>pivot) pr--; | ||
if(pl<=pr) { | ||
swap(arr, pl, pr); | ||
pl++; | ||
pr--; | ||
} | ||
System.out.println("pivot값은: "+pivot+"입니다"); | ||
}while(pl<=pr); | ||
System.out.println("----파티션 한 후--------------"); | ||
System.out.println(Arrays.toString(arr)); | ||
}//---------------------------------- | ||
|
||
static void swap(int[] arr, int i, int k) { | ||
int tmp=arr[i]; | ||
arr[i]=arr[k]; | ||
arr[k]=tmp; | ||
}//------------------------------------ | ||
|
||
}/////////////////////////////////////////// |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package day05; | ||
|
||
import java.util.Arrays; | ||
|
||
public class QuickSort { | ||
|
||
public static void main(String[] args) { | ||
int arr[]= {1,8,7,4,5,2,6,3,9}; | ||
System.out.println(Arrays.toString(arr)); | ||
quickSort(arr, 0, arr.length-1);//quickSort(arr, 0, 8) | ||
System.out.println("--퀵 정렬 이후-------------------------"); | ||
System.out.println(Arrays.toString(arr)); | ||
}//------------------------------------------------- | ||
//퀵 정렬의 분할과정을 출력하는 메서드 | ||
public static void printProcess(int[] arr, int left, int right, int pivot) { | ||
System.out.println("pivot: "+pivot); | ||
System.out.printf("arr[%d] ~ arr[%d]: {", left,right); | ||
for(int i=left; i<right; i++) { | ||
System.out.printf("%d", arr[i]); | ||
} | ||
System.out.printf("%d}%n", arr[right]); | ||
}//------------------------------------------------- | ||
|
||
static void quickSort(int[] arr, int left, int right) {//(0,8) | ||
int pl=left;//0,0 | ||
int pr=right;//8,4 | ||
int pivot=arr[(pl+pr)/2]; //arr[4]=5, arr[2]=? | ||
|
||
printProcess(arr, left, right, pivot); | ||
|
||
do { | ||
while(arr[pl]<pivot) pl++; | ||
while(arr[pr]>pivot) pr--; | ||
if(pl<=pr) { | ||
swap(arr, pl++, pr--); | ||
} | ||
}while(pl<=pr); | ||
|
||
if(left<pr) quickSort(arr, left, pr);//재귀 호출(0,4) (0,2) (0,1) (0,0) | ||
if(pl<right) quickSort(arr, pl, right);//재귀 호출 (5,8) | ||
}////////////////////////////// | ||
|
||
|
||
static void swap(int[] arr, int i, int k) { | ||
int tmp=arr[i]; | ||
arr[i]=arr[k]; | ||
arr[k]=tmp; | ||
}//------------------------------ | ||
|
||
|
||
} |
Oops, something went wrong.