英文:
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<school> x){
if(x.isEmpty()){
return ;
}
ArrayList<school> smaller = new ArrayList<>();
ArrayList<school> greater = new ArrayList<>();
school pivot = x.get(0); // pivot value
int i; // incremental counter
school j; // looping value
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);
}
答案1
得分: 0
quickSort
函数在其内部调用自身,因此这是递归的。
英文:
The quickSort
function is called from within itself, therefore this is recursive.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论