java 数组中正数和负数的和_排序零,正数和负数的数组

有可用于从一个简单的冒泡排序的整数数组许多不同种类的排序到一个稍微复杂的堆排序。有些种类比另一种更快,例如堆排序比泡泡排序更快,但是这主要影响大型数组。这是由于数字之间的交换和比较的数量。

冒泡排序

public class BubbleSort

{

public void sortArray(int[] a)

{

int c, d, swap;

for(c = 1; c < a.length; c++)

{

for (d = 0; d < a.length - c; d++)

{

if (a[d] > a[d+1])

{

swap = a[d];

a[d] = a[d+1];

a[d+1] = swap;

}

}

}

for(int i=0;i

{

int correctNumber = i+1;

System.out.println("Value "+correctNumber+" of the sorted array which was sorted via the Bubble Sort is: "+a[i]);

}

}

}

堆排序

public class HeapSort

{

private static int[] a;

private static int n;

private static int left;

private static int right;

private static int largest;

public static void buildheap(int []a)

{

n=a.length-1;

for(int i=n/2;i>=0;i--)

{

maxheap(a,i);

}

}

public static void maxheap(int[] a, int i)

{

left=2*i;

right=2*i+1;

if(left <= n && a[left] > a[i])

{

largest=left;

}

else

{

largest=i;

}

if(right <= n && a[right] > a[largest])

{

largest=right;

}

if(largest!=i)

{

exchange(i,largest);

maxheap(a, largest);

}

}

public static void exchange(int i, int j)

{

int t=a[i];

a[i]=a[j];

a[j]=t;

}

public static void sort(int[] a0)

{

a=a0;

buildheap(a);

for(int i=n;i>0;i--)

{

exchange(0, i);

n=n-1;

maxheap(a, 0);

}

for(int i=0;i

{

int correctNumber = i+1;

System.out.println("Value "+correctNumber+" of the sorted array which was sorted via the Heap Sort is: "+a[i]);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值