Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8568625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:09:13+00:00 2026-06-11T18:09:13+00:00

I learnt about quicksort and how it can be implemented in both Recursive and

  • 0

I learnt about quicksort and how it can be implemented in both Recursive and Iterative method.

In Iterative method:

  1. Push the range (0...n) into the stack
  2. Partition the given array with a pivot
  3. Pop the top element.
  4. Push the partitions (index range) onto a stack if the range has more than one element
  5. Do the above 3 steps, till the stack is empty

And the recursive version is the normal one defined in wiki.

I learnt that recursive algorithms are always slower than their iterative counterpart.

So, Which method is preferred in terms of time complexity (memory is not a concern)?
Which one is fast enough to use in Programming contest?
Is C++ STL sort() using a recursive approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:09:14+00:00Added an answer on June 11, 2026 at 6:09 pm

    In terms of (asymptotic) time complexity – they are both the same.

    "Recursive is slower then iterative" – the rational behind this statement is because of the overhead of the recursive stack (saving and restoring the environment between calls).

    However -these are constant number of ops, while not changing the number of "iterations".

    Both recursive and iterative quicksort are O(nlogn) average case and O(n^2) worst case.


    EDIT:

    just for the fun of it I ran a benchmark with the (java) code attached to the post , and then I ran wilcoxon statistic test, to check what is the probability that the running times are indeed distinct

    The results may be conclusive (P_VALUE=2.6e-34, https://en.wikipedia.org/wiki/P-value. Remember that the P_VALUE is P(T >= t | H) where T is the test statistic and H is the null hypothesis). But the answer is not what you expected.

    The average of the iterative solution was 408.86 ms while of recursive was 236.81 ms

    (Note – I used Integer and not int as argument to recursiveQsort() – otherwise the recursive would have achieved much better, because it doesn’t have to box a lot of integers, which is also time consuming – I did it because the iterative solution has no choice but doing so.

    Thus – your assumption is not true, the recursive solution is faster (for my machine and java for the very least) than the iterative one with P_VALUE=2.6e-34.

    public static void recursiveQsort(int[] arr,Integer start, Integer end) { 
        if (end - start < 2) return; //stop clause
        int p = start + ((end-start)/2);
        p = partition(arr,p,start,end);
        recursiveQsort(arr, start, p);
        recursiveQsort(arr, p+1, end);
    
    }
    
    public static void iterativeQsort(int[] arr) { 
        Stack<Integer> stack = new Stack<Integer>();
        stack.push(0);
        stack.push(arr.length);
        while (!stack.isEmpty()) {
            int end = stack.pop();
            int start = stack.pop();
            if (end - start < 2) continue;
            int p = start + ((end-start)/2);
            p = partition(arr,p,start,end);
    
            stack.push(p+1);
            stack.push(end);
    
            stack.push(start);
            stack.push(p);
    
        }
    }
    
    private static int partition(int[] arr, int p, int start, int end) {
        int l = start;
        int h = end - 2;
        int piv = arr[p];
        swap(arr,p,end-1);
    
        while (l < h) {
            if (arr[l] < piv) {
                l++;
            } else if (arr[h] >= piv) { 
                h--;
            } else { 
                swap(arr,l,h);
            }
        }
        int idx = h;
        if (arr[h] < piv) idx++;
        swap(arr,end-1,idx);
        return idx;
    }
    private static void swap(int[] arr, int i, int j) { 
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
    
    public static void main(String... args) throws Exception {
        Random r = new Random(1);
        int SIZE = 1000000;
        int N = 100;
        int[] arr = new int[SIZE];
        int[] millisRecursive = new int[N];
        int[] millisIterative = new int[N];
        for (int t = 0; t < N; t++) { 
            for (int i = 0; i < SIZE; i++) { 
                arr[i] = r.nextInt(SIZE);
            }
            int[] tempArr = Arrays.copyOf(arr, arr.length);
            
            long start = System.currentTimeMillis();
            iterativeQsort(tempArr);
            millisIterative[t] = (int)(System.currentTimeMillis()-start);
            
            tempArr = Arrays.copyOf(arr, arr.length);
            
            start = System.currentTimeMillis();
            recursvieQsort(tempArr,0,arr.length);
            millisRecursive[t] = (int)(System.currentTimeMillis()-start);
        }
        int sum = 0;
        for (int x : millisRecursive) {
            System.out.println(x);
            sum += x;
        }
        System.out.println("end of recursive. AVG = " + ((double)sum)/millisRecursive.length);
        sum = 0;
        for (int x : millisIterative) {
            System.out.println(x);
            sum += x;
        }
        System.out.println("end of iterative. AVG = " + ((double)sum)/millisIterative.length);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just learnt about temporary tables and using them has given me some
One question about protected constructor. I learnt that the protected constructor can be used
Having learnt a bit about the subject, can anyone tell, what is the real
I recently learnt about the benefits of EnumMap in Java and would like to
I just learnt about generators in Python a week back. From what I understood,
I've recently learnt about CNS and FNS , and since they look so elegant
I just learnt about how to parse data in Xcode using NSXMLPARSER. In order
My app is ready for submission now but I have recently learnt about Licensing.
I learnt a lot about weak_ptr working with share_ptr to break cyclic reference. How
I have learnt Python for about a month as a one year's PHPer.And I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.