Skip to content

Commit 0420673

Browse files
Lee jeonggeunredrebel
Lee jeonggeun
authored andcommitted
Add comment
1 parent 769f5d1 commit 0420673

File tree

9 files changed

+262
-0
lines changed

9 files changed

+262
-0
lines changed

.classpath

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin

.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Algorithm</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,12 @@ Algorithm
22
=========
33

44
Algorithm study
5+
6+
p140910
7+
Bubble sort
8+
9+
p140913
10+
Calculate Factorial, Permutation, Combination
11+
12+
p140916
13+
print weekday at afterDay

src/p140910/Bubble.java

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package p140910;
2+
3+
/**
4+
* bubble sort
5+
*
6+
* @author red
7+
*
8+
*/
9+
public class Bubble {
10+
11+
/**
12+
* bubble sort
13+
* @param arr
14+
*/
15+
public static int bubbleSort(int[] arr) {
16+
int cntSwap = 0;
17+
boolean swapped;
18+
do {
19+
swapped = false;
20+
for (int i = 1; i < arr.length; i++) {
21+
if (arr[i - 1] > arr[i]) {
22+
int tmp;
23+
tmp = arr[i - 1];
24+
arr[i - 1] = arr[i];
25+
arr[i] = tmp;
26+
swapped = true;
27+
cntSwap++;
28+
}
29+
}
30+
}while(swapped);
31+
return cntSwap;
32+
}
33+
34+
/**
35+
* enhanced bubble sort
36+
* @param arr
37+
* @return
38+
*/
39+
public static int enhancedBubbleSort(int[] arr){
40+
int cntSwap = 0;
41+
boolean swapped;
42+
int lastIndex = arr.length;
43+
do{
44+
swapped = false;
45+
for(int i = 1; i<lastIndex; i++){
46+
if(arr[i-1] > arr[i]){
47+
int tmp = arr[i-1];
48+
arr[i-1] = arr[i];
49+
arr[i] = tmp;
50+
swapped = true;
51+
cntSwap++;
52+
}
53+
}
54+
lastIndex--;
55+
}while(swapped);
56+
57+
return cntSwap;
58+
}
59+
60+
public static void main(String[] args) {
61+
int[] arr = { 3, 4, 16, 2 ,8, 32, 88, 1};
62+
for(int a:arr)
63+
System.out.print(a+",");
64+
System.out.println();
65+
int cntSwap;
66+
//cntSwap = bubbleSort(arr);
67+
cntSwap = enhancedBubbleSort(arr);
68+
for(int a:arr)
69+
System.out.print(a+",");
70+
System.out.println();
71+
System.out.println("cntSwap : " + cntSwap);
72+
}
73+
74+
}

src/p140913/Combination.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package p140913;
2+
3+
public class Combination {
4+
5+
/**
6+
* Calculate Combination
7+
* @param n
8+
* @param k
9+
* @return
10+
*/
11+
public static int comb(int n, int k){
12+
// n장에서 k장을 선택하는 모든 순열의 수 / k장을 치환하는 모든 가지의 수.
13+
int c = Permutation.perm(n, k) / Permutation.perm(k, k);
14+
return c;
15+
}
16+
public static void main(String[] args) {
17+
// what is 5C3
18+
// 3개의 카드중에 2개를 선택할 경우의 갯수는? (순서 구별안. {A,B,C} = {C,B,A})
19+
int c = comb(5,3);
20+
System.out.println(c);
21+
}
22+
23+
}

src/p140913/Factorial.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package p140913;
2+
3+
public class Factorial {
4+
/**
5+
* Calculate Factorial
6+
* @param n
7+
* @return
8+
*/
9+
public static int facto(int n){
10+
int m = 1;
11+
for(int i = n; i >= 1 ; i--){
12+
m = m*i;
13+
}
14+
return m;
15+
}
16+
public static void main(String[] args) {
17+
// what is 7! (7*6*5*4*3*2*1)
18+
System.out.println(facto(7));
19+
}
20+
21+
}

src/p140913/Permutation.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package p140913;
2+
3+
public class Permutation {
4+
/**
5+
* Calculate Permutation
6+
* @param n
7+
* @param k
8+
* @return
9+
*/
10+
public static int perm(int n, int k){
11+
12+
int p = Factorial.facto(n) / Factorial.facto(n - k);
13+
return p;
14+
}
15+
public static void main(String args[]){
16+
// what is 3P2
17+
// 3개의 카드중에 2개를 선택할 경우의 갯수는? (순서도 구별됨. {A,B,C} != {C,B,A})
18+
int p = perm(3,2);
19+
System.out.println(p);
20+
}
21+
}

src/p140916/Weekday.java

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package p140916;
2+
3+
public class Weekday {
4+
5+
/**
6+
* print weekday at d'after
7+
* @param d
8+
*/
9+
public void run(int d){
10+
int remainDay = d%7;
11+
System.out.println(remainDay);
12+
13+
switch(remainDay){
14+
case 0:
15+
System.out.println("sunday");
16+
break;
17+
case 1:
18+
System.out.println("monday");
19+
break;
20+
case 2:
21+
System.out.println("tuesday");
22+
break;
23+
case 3:
24+
System.out.println("wednesday");
25+
break;
26+
case 4:
27+
System.out.println("thursday");
28+
break;
29+
case 5:
30+
System.out.println("friday");
31+
break;
32+
case 6:
33+
System.out.println("saturday");
34+
break;
35+
}
36+
37+
}
38+
39+
/**
40+
* print weekday at n-after
41+
* @param n
42+
*/
43+
public void run2(int n){
44+
/*
45+
* 10^0 / 7 = 1
46+
* 10^1 / 7 = 3
47+
* 10^2 / 7 = 2
48+
* 10^3 / 7 = 6
49+
* 10^4 / 7 = 4
50+
* 10^5 / 7 = 5
51+
* 10^6 / 7 = 1
52+
* 10^7 / 7 = 3
53+
* ...
54+
* ...
55+
*/
56+
int remainDay = n%6;
57+
58+
switch(remainDay){
59+
case 0:
60+
System.out.println("monday");
61+
break;
62+
case 1:
63+
System.out.println("wednesday");
64+
break;
65+
case 2:
66+
System.out.println("tuesday");
67+
break;
68+
case 3:
69+
System.out.println("saturday");
70+
break;
71+
case 4:
72+
System.out.println("thursday");
73+
break;
74+
case 5:
75+
System.out.println("friday");
76+
break;
77+
}
78+
79+
}
80+
public static void main(String[] args) {
81+
Weekday wd = new Weekday();
82+
83+
// what is weekday at 100day after?
84+
wd.run(100);
85+
86+
// what is weekday at 10^100 day after?
87+
wd.run(100);
88+
}
89+
90+
}

0 commit comments

Comments
 (0)