import java.util.Random;
class MergeSort2 {
//Recursion을 이용한 Merge Sort
public static int[] mergesort(int a[ ], int first, int n) {
int n1;
int n2;
if (n > 1) {
n1 = n/2; n2=n-n1;
mergesort(a, first, n1);
mergesort(a, first+n1, n2);
merge(a, first, n1, n2);
}
return a;
}
private static void merge(int[ ] a, int first, int n1, int n2) {
int[ ] temp = new int[n1+n2];
int copied = 0;
int copied1 = 0;
int copied2 = 0;
int i;
while ((copied1 < n1) && (copied2 < n2))
{
if (a[first + copied1] < a[first + n1 + copied2])
temp[copied++] = a[first + (copied1++)];
else
temp[copied++] = a[first + n1 + (copied2++)];
}
while (copied1 < n1)
temp[copied++] = a[first + (copied1++)];
while (copied2 < n2)
temp[copied++] = a[first + n1 + (copied2++)];
for (i = 0; i < n1+n2; i++)
a[first + i] = temp[i];
}
public static void main(String[] args){
int nansu[] = new int[10000];
int[] sortedArray = new int[10000];
int[] halfSortedArray = new int[10000];
int[] reverseArray = new int[10000];
int[] temp = new int[10000];
long startTime, elapsedTime, counter;
MergeSort2 mySort = new MergeSort2();
//----------------- 난수 발생시킴
Random r = new Random();
for(int i=0; i<10000; i++) {
nansu[i] = r.nextInt(10000);
}
//-----------------------------------
//-----------------------------------------------
//난수를 정렬
//-----------------------------------------------
counter=0;
startTime = System.currentTimeMillis();
do {
counter++;
sortedArray = mySort.mergesort(nansu, 0, nansu.length);
} while (System.currentTimeMillis() - startTime < 1000);
elapsedTime = (System.currentTimeMillis()- startTime) / counter;
System.out.println("[난수일때의 수행시간] " + elapsedTime + "ms");
//------------------------------------------------------------------------------
// 정렬된 상태(최선의상황)을 다시 정렬시킴
//------------------------------------------------------------------------------
counter=0;
startTime = System.currentTimeMillis();
do {
counter++;
temp = mySort.mergesort(sortedArray, 0, sortedArray.length);
} while (System.currentTimeMillis() - startTime < 1000);
elapsedTime = (System.currentTimeMillis()- startTime) / counter;
System.out.println("[최선의 상황 수행시간] " + elapsedTime + "ms");
//-------------------------------------------------------------------------------
//반쯤 정열후 ...
//-------------------------------------------------------------------------------
counter=0;
halfSortedArray = sortedArray;
for(int i=0; i<5000; i++) {
halfSortedArray[i] = r.nextInt()*10000;
}
startTime = System.currentTimeMillis();
do {
counter++;
temp = mySort.mergesort(halfSortedArray, 0, halfSortedArray.length);
} while (System.currentTimeMillis() - startTime < 1000);
elapsedTime = (System.currentTimeMillis()- startTime) / counter;
System.out.println("[반쯤 정렬 수행시간] " + elapsedTime + "ms");
//-------------------------------------------------------------------------------
//역순상태
//-------------------------------------------------------------------------------
counter=0;
for(int i=0;i<sortedArray.length;i++) {
reverseArray[sortedArray.length-i-1] = sortedArray[i];
}
startTime = System.currentTimeMillis();
do {
counter++;
temp = mySort.mergesort(reverseArray, 0, reverseArray.length);
} while (System.currentTimeMillis() - startTime < 1000);
elapsedTime = (System.currentTimeMillis()- startTime) / counter;
System.out.println("[역순 상태 수행시간] " + elapsedTime + "ms");
}
}
class MergeSort2 {
//Recursion을 이용한 Merge Sort
public static int[] mergesort(int a[ ], int first, int n) {
int n1;
int n2;
if (n > 1) {
n1 = n/2; n2=n-n1;
mergesort(a, first, n1);
mergesort(a, first+n1, n2);
merge(a, first, n1, n2);
}
return a;
}
private static void merge(int[ ] a, int first, int n1, int n2) {
int[ ] temp = new int[n1+n2];
int copied = 0;
int copied1 = 0;
int copied2 = 0;
int i;
while ((copied1 < n1) && (copied2 < n2))
{
if (a[first + copied1] < a[first + n1 + copied2])
temp[copied++] = a[first + (copied1++)];
else
temp[copied++] = a[first + n1 + (copied2++)];
}
while (copied1 < n1)
temp[copied++] = a[first + (copied1++)];
while (copied2 < n2)
temp[copied++] = a[first + n1 + (copied2++)];
for (i = 0; i < n1+n2; i++)
a[first + i] = temp[i];
}
public static void main(String[] args){
int nansu[] = new int[10000];
int[] sortedArray = new int[10000];
int[] halfSortedArray = new int[10000];
int[] reverseArray = new int[10000];
int[] temp = new int[10000];
long startTime, elapsedTime, counter;
MergeSort2 mySort = new MergeSort2();
//----------------- 난수 발생시킴
Random r = new Random();
for(int i=0; i<10000; i++) {
nansu[i] = r.nextInt(10000);
}
//-----------------------------------
//-----------------------------------------------
//난수를 정렬
//-----------------------------------------------
counter=0;
startTime = System.currentTimeMillis();
do {
counter++;
sortedArray = mySort.mergesort(nansu, 0, nansu.length);
} while (System.currentTimeMillis() - startTime < 1000);
elapsedTime = (System.currentTimeMillis()- startTime) / counter;
System.out.println("[난수일때의 수행시간] " + elapsedTime + "ms");
//------------------------------------------------------------------------------
// 정렬된 상태(최선의상황)을 다시 정렬시킴
//------------------------------------------------------------------------------
counter=0;
startTime = System.currentTimeMillis();
do {
counter++;
temp = mySort.mergesort(sortedArray, 0, sortedArray.length);
} while (System.currentTimeMillis() - startTime < 1000);
elapsedTime = (System.currentTimeMillis()- startTime) / counter;
System.out.println("[최선의 상황 수행시간] " + elapsedTime + "ms");
//-------------------------------------------------------------------------------
//반쯤 정열후 ...
//-------------------------------------------------------------------------------
counter=0;
halfSortedArray = sortedArray;
for(int i=0; i<5000; i++) {
halfSortedArray[i] = r.nextInt()*10000;
}
startTime = System.currentTimeMillis();
do {
counter++;
temp = mySort.mergesort(halfSortedArray, 0, halfSortedArray.length);
} while (System.currentTimeMillis() - startTime < 1000);
elapsedTime = (System.currentTimeMillis()- startTime) / counter;
System.out.println("[반쯤 정렬 수행시간] " + elapsedTime + "ms");
//-------------------------------------------------------------------------------
//역순상태
//-------------------------------------------------------------------------------
counter=0;
for(int i=0;i<sortedArray.length;i++) {
reverseArray[sortedArray.length-i-1] = sortedArray[i];
}
startTime = System.currentTimeMillis();
do {
counter++;
temp = mySort.mergesort(reverseArray, 0, reverseArray.length);
} while (System.currentTimeMillis() - startTime < 1000);
elapsedTime = (System.currentTimeMillis()- startTime) / counter;
System.out.println("[역순 상태 수행시간] " + elapsedTime + "ms");
}
}
'Programming > JAVA' 카테고리의 다른 글
Sort 알고리즘(셸정렬) (0) | 2008.04.28 |
---|---|
Sort 알고리즘(퀵정렬-재귀호출이용) (0) | 2008.04.28 |
까오기 보드에서 사용하는 계층형 게시판 로직 (0) | 2008.04.28 |
JAVA DOC을 사용하자. (0) | 2008.04.28 |
Factory Method 패턴 (0) | 2008.04.28 |