Java 如何使合并排序按降序而不是升序进行?
这可能是一个相当简单的问题。我就是搞不懂如何按降序而不是升序进行排序。有人能帮我吗Java 如何使合并排序按降序而不是升序进行?,java,mergesort,Java,Mergesort,这可能是一个相当简单的问题。我就是搞不懂如何按降序而不是升序进行排序。有人能帮我吗 public static void sortYear(Movie4[] movies, int low, int high) { if ( low == high ) return; int mid = ( low + high ) / 2; sortYear( movies, low, mid ); sortYear( movies, mid + 1,
public static void sortYear(Movie4[] movies, int low, int high)
{
if ( low == high )
return;
int mid = ( low + high ) / 2;
sortYear( movies, low, mid );
sortYear( movies, mid + 1, high );
mergeYears(movies, low, mid, high );
}
public static void mergeYears(Movie4[] movies, int low, int mid, int high)
{
Movie4[] temp = new Movie4[ high - low + 1 ];
int i = low, j = mid + 1, n = 0;
while ( i <= mid || j <= high )
{
if ( i > mid )
{
temp[ n ] = movies[ j ];
j++;
}
else if ( j > high )
{
temp[ n ] = movies[ i ];
i++;
}
else if ( movies[ i ].getYear() < movies[ j ].getYear())
{
temp[n] = movies[i];
i++;
}
else
{
temp[n] = movies[j];
j++;
}
n++;
}
for ( int k = low ; k <= high ; k++ )
{
movies[ k ] = temp[ k - low ];
}
}
要更改排序顺序,您必须担心它们比较对象值的确切位置。在你的情况下,这是在下面的一行 只要改变这个:
else if ( movies[ i ].getYear() < movies[ j ].getYear())
请注意,唯一更改的是>运算符。要更改排序顺序,您必须担心它们比较对象值的确切位置。在你的情况下,这是在下面的一行 只要改变这个:
else if ( movies[ i ].getYear() < movies[ j ].getYear())
请注意,唯一更改的是>运算符。为了帮助您自己回答问题,我将在代码中添加一些注释 所有的实际工作都需要几年时间:
public static void mergeYears(Movie4[] movies, int low, int mid, int high)
{
Movie4[] temp = new Movie4[ high - low + 1 ];
// 'i' tracks the index for the head of low half of the range.
// 'j' tracks the index for the head of upper half of the range.
int i = low, j = mid + 1, n = 0;
// While we still have a entry in one of the halves.
while ( i <= mid || j <= high )
{
// Lower half is exhausted. Just copy from the upper half.
if ( i > mid )
{
temp[ n ] = movies[ j ];
j++;
}
// Upper half is exhausted. Just copy from the lower half.
else if ( j > high )
{
temp[ n ] = movies[ i ];
i++;
}
// Compare the two Movie4 objects at the head of the lower and upper halves.
// If lower is less than upper copy from lower.
else if ( movies[ i ].getYear() < movies[ j ].getYear())
{
temp[n] = movies[i];
i++;
}
// Lower is is greater than upper. Copy from upper.
else
{
temp[n] = movies[j];
j++;
}
n++;
}
// Copy from the temp buffer back into the 'movie' array.
for ( int k = low ; k <= high ; k++ )
{
movies[ k ] = temp[ k - low ];
}
}
为了帮助您自己回答这个问题,我将在代码中添加一些注释 所有的实际工作都需要几年时间:
public static void mergeYears(Movie4[] movies, int low, int mid, int high)
{
Movie4[] temp = new Movie4[ high - low + 1 ];
// 'i' tracks the index for the head of low half of the range.
// 'j' tracks the index for the head of upper half of the range.
int i = low, j = mid + 1, n = 0;
// While we still have a entry in one of the halves.
while ( i <= mid || j <= high )
{
// Lower half is exhausted. Just copy from the upper half.
if ( i > mid )
{
temp[ n ] = movies[ j ];
j++;
}
// Upper half is exhausted. Just copy from the lower half.
else if ( j > high )
{
temp[ n ] = movies[ i ];
i++;
}
// Compare the two Movie4 objects at the head of the lower and upper halves.
// If lower is less than upper copy from lower.
else if ( movies[ i ].getYear() < movies[ j ].getYear())
{
temp[n] = movies[i];
i++;
}
// Lower is is greater than upper. Copy from upper.
else
{
temp[n] = movies[j];
j++;
}
n++;
}
// Copy from the temp buffer back into the 'movie' array.
for ( int k = low ; k <= high ; k++ )
{
movies[ k ] = temp[ k - low ];
}
}
是的,他是这么说的。。。就在问题的末尾…这是家庭作业,但不是全部问题。我必须用降序合并排序法写一个程序,但我搞不懂。查一下没什么错。所以我只想继续打字一会儿-是的,你不应该这么做。是的,他这么说的。。。就在问题的末尾…这是家庭作业,但不是全部问题。我必须用降序合并排序法写一个程序,但我搞不懂。查一下没什么不对的。所以我只想继续打字一会儿——是的,你不应该这么做。