简体   繁体   中英

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

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);

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM