第三次作业
以下是代码部分:public class ee{
public static void main(String []args) { int[] a = {15,1,2,5,17,15,1,2,5,33}; int start = 0; int end = a.length-1; sort(a,start,end); for(int i = 0; i<a.length; i++) { System.out.print (a[i]+" "); } } public static void sort(int[] a,int low,int high) { int start = low; int end = high; int key = a[low]; while(end>start) { while(end>start&&a[end]>=key) end--; if(a[end]<=key) { int temp = a[end]; a[end] = a[start]; a[start] = temp; } while(end>start&&a[start]<=key) //从后往前进行比较,如果没有比基准值小的,就继续向后比较。 start++; if(a[start]>=key) { int temp = a[start]; a[start] = a[end]; a[end] = temp; } }
//此时第一次循环比较结束,关键值的位置已经确定了。
//左边的值都比关键值小,右边的值都比关键值大,但是两边的顺序还有可能是不一样的,进行下面的递归调用
if(start>low) sort(a,low,start-1); //左边排序第一个索引位置到基准值索引-1
if(end<high) sort(a,end+1,high); //右边排序右边关键值+1到最后一个
}
}
运行结果如下图
小结感受:结对编程真的能够带来1+1>2的效果吗?
能。遇到问题每个人的想法会不一样,有时候会进入误区,两个人会相互弥补对方的不足,起到事半功倍的效果。
运用“汉堡包”的方式,评价你的合作伙伴,指出优点、缺点,希望ta提高的地方
他很聪明,遇到问题也基本是他帮我改正。至于缺点,嫌弃我。希望他改正态度,不要嫌弃我。