Computer Science/Algorithm2011. 5. 26. 23:03
삽입 정렬을 자바로 작성해봤다.

@Test
public void insertionSort() 
{
int[] x = new int[10];
for (int i = 0; i < x.length; i++)
x[i] = (int) (Math.random() * x.length);
for(int i = 1; i < x.length; ++i){
for(int j = i; j > 0; --j){
if(x[j] < x[j-1]){
int temp = x[j-1];
x[j-1] = x[j];
x[j] = temp;
}
}
}
}


이번 삽입 정렬은 swap을 하지 않는 방식으로 해봤다.

@Test
public void insertionSort2() 
{
int[] x = new int[10];
for (int i = 0; i < x.length; i++)
x[i] = (int) (Math.random() * x.length);
int i, j, temp;
for(i = 1; i < x.length; ++i){
temp = x[i];
for(j = i; j > 0 && x[j-1] > temp; --j)
x[j] = x[j-1];
x[j] = temp;
}
}



'Computer Science > Algorithm' 카테고리의 다른 글

quick sort - c  (0) 2011.05.30
insertion sort - c  (0) 2011.05.28
binary search - Java  (0) 2011.05.17
binary search - c  (0) 2011.05.16
반전 알고리즘  (0) 2011.05.12
Posted by 준피