需要帮助确定给定的算法是迭代的还是递归的。

huangapple go评论64阅读模式
英文:

Need help to determine if a given algorithm is iterative or recursive

问题

以下是翻译好的部分:

public static void quickSort(ArrayList<school> x){

    if(x.isEmpty()){        
        return ;
    }
    
    ArrayList<school> smaller = new ArrayList<>();     
    ArrayList<school> greater = new ArrayList<>();      

    school pivot = x.get(0);        // 枢纽值
    int i;      // 增量计数器
    school j;       // 循环值

    for( i=1; i < x.size();i++){
        j = x.get(i);                                       
        if( j.getName().compareTo(pivot.getName()) < 0 ){       
            smaller.add(j);
        }else{
            greater.add(j);
        }
    }

    quickSort(smaller);
    quickSort(greater);

    x.clear();

    x.addAll(smaller);
    x.add(pivot);
    x.addAll(greater);

}
英文:

so I got a project for a computer science module, and they ask us to perform ITERATIVE quick and merge sorts, so I wrote an algorithm, but I am unsure if it is iterative or recursive.

Any feedback would be greatly appreciated, thanks in advance!

Here is the algorithm (it works, just need to know if it is iterative or not)

public static void quickSort(ArrayList&lt;school&gt; x){

    if(x.isEmpty()){        
        return ;
    }
    
    ArrayList&lt;school&gt; smaller = new ArrayList&lt;&gt;();     
    ArrayList&lt;school&gt; greater = new ArrayList&lt;&gt;();      

    school pivot = x.get(0);        // pivot value
    int i;      // incremental counter
    school j;       // looping value

    for( i=1; i &lt; x.size();i++){
        j = x.get(i);                                       
        if( j.getName().compareTo(pivot.getName()) &lt; 0 ){       
            smaller.add(j);
        }else{
            greater.add(j);
        }
    }

    quickSort(smaller);
    quickSort(greater);

    x.clear();

    x.addAll(smaller);
    x.add(pivot);
    x.addAll(greater);

}

答案1

得分: 0

quickSort函数在其内部调用自身,因此这是递归的。

英文:

The quickSort function is called from within itself, therefore this is recursive.

huangapple
  • 本文由 发表于 2020年9月18日 16:41:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63952243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定