RAM 모델에 따라 계산할 때 선택 정렬을 사용하여보다 효율적으로 정렬 할 수 있고 mergesort를 사용하여 덜 효율적으로 정렬되는 요소의 수는 매우 낮습니다 (n < < 500000). 그러나 이것을 테스트하려고 시도했을 때, 선택 정렬은 3 분 11 초 만에 500000 개의 요소 배열을 정렬하고 mergesort는 6 분 44 초에 정렬을 찾았습니다. 두 알고리즘의 구현이 정확하다고 확신합니다. 무슨 일이야? 설명 해주십시오.어떤 상황에서 mergesort가 선택 정렬보다 빠릅니다.
죄송합니다. 정말 죄송합니다. 네트워크 연결이 끊어졌습니다.
public class MergeSort {
// temp array
public static int temp[];
public static void merge(int a[], int low, int mid, int high)
{
temp = new int [a.length];
for(int i = low; i <= high; i++)
temp[i] = a[i];
int i = low;
int j = mid + 1;
int k = low;
while(i <= mid && j <= high)
{
if(temp[i] <= temp[j])
{
a[k] = temp[i];
i++;
}
else
{
a[k] = temp[j];
j++;
}
k++;
}
while(i <= mid)
{
a[k] = temp[i];
k++;
i++;
}
}
public static void sort(int a[], int low, int high)
{
if(low < high)
{
int middle = (low + high)/2;
// sort the left half
sort(a, low, middle);
// sort the right half
sort(a, middle + 1, high);
// merge parts
merge(a, low, middle, high);
}
}
}
내 선택 정렬 : 선택 정렬에서
public static int[] selSort(int a[])
{
int length = a.length;
int temp[] = new int [length];
System.arraycopy(a, 0, temp, 0, length);
for(int i = 0; i < length - 1; i++)
{
int smIndex = i;
for(int cIndex = i + 1; cIndex < length; ++cIndex)
if(temp[cIndex] < temp[smIndex])
smIndex = cIndex;
int t = temp[smIndex];
temp[smIndex] = temp[i];
temp[i] = t;
}
return temp;
}
, 나는 이유에 대한 배열을 복제하고
그래서 여기 내 머지 소트입니다.
http://www.titrias.com/ultimate-sorting-algorithms-comparison/ –
안녕하세요 라훌. Mergesort는 배열 크기가 10000 정도로 작더라도 선택 링크보다 빠릅니다.하지만 내 컴퓨터에서는 그렇게되지 않습니다. –
'두 알고리즘의 구현이 맞을 것이라고 확신합니다. '그러면 구현을 보여줄 수 있습니다. –