写一个Java程序的方法

来源:百度知道 编辑:UC知道 时间:2024/06/16 20:40:06
要求如下~:There is a variation of the bubble sort algorithm called a gap sort that, rather than comparing neighboring elements each time through the list, compares elements that are some number(i) positions apart, where i is an integer less than n. For example, the first element would be compared to the(i+1)element, the second element would be compared to the(i+2) element, the nth element would be compared to the(n-i) element, etc. A single iteration is completed when all of the elements that can be compared, have been compared. On the next iteration, i is reduced by some number greater than 1 and the process continues until i is less than 1. Implement a gap sort.

请给出可使用正确的gapSort方法

public void gap_sort(int[] a, int size)
{
start = (int) (size/1.3);
while(start>0)
{
for(int i=start, j=0; i<size; i++, j++)
if(a[i]<a[j])
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
start = (int) (size/1.3);
}
}

一次gap_sort不能得到正确结果。但是给他穿上外衣结果就正确了。

public boolean nosort(int[] a, int size){
for(int i=0;i<size-1;i++)if(a[i]>a[i+1])return true; return false;
}

void GAP_SORT(int[] a, int size)
{
while(nosort(a,size))gap_sort(a,size);
}

//其实问题要求很简单,是冒泡排序的。不过貌似是对象元素的,所以没用用普通数据类型来写排序。
public class GapSort{
public static void gapSort(Object[] array){
Object temp;
int