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

  • SEARCH
  • Home
  • 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 8640859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:22:52+00:00 2026-06-12T11:22:52+00:00

The code works good for small num_items , but overflows the stack for num_items

  • 0

The code works good for small num_items, but overflows the stack for num_items = 1000.

I think 1000 is a pretty small number, so there should be a way to make this work upto much larger numbers. What can I do to make it survive more recursive calls?

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <ctime>

void printv(std::vector<int>& v) {
    for (int i=0; i<v.size(); i++) std::cout << v[i] << " ";
    std::cout << std::endl;
}

void quicksort(std::vector<int>& v, int begin, int end) {

    if (((end - begin) <= 1) ||
        (begin < 0) ||
        (end > (v.size() - 1))) return;

    int pivot = v[std::rand() % v.size()];

    int x = 0;
    int y = v.size() -1;

    while(x < y) {
        int changed = 0;
        if(v[x] <= pivot) {
            x++;
            changed = 1;
        }
        if (v[y] > pivot) {
            y--;
            changed = 1;
        }
        if (!changed){
            std::swap(v[x], v[y]);
            x++;
            y--;
        }
    }

    if (x == y) y++;
    if (x > y) std::swap(x, y);

    quicksort(v, begin, x);
    quicksort(v, y, end);
}

int ran() {
    return std::rand() % 100;
}

int main() {
    std::srand(std::time(0));
    int num_items = 1000;
    std::vector<int> v (num_items);
    std::generate_n(v.begin(), num_items, ran);

    printv(v);
    quicksort(v, 0, v.size()-1);
    printv(v);
}

General comments about the code are also welcome.


Stuck at [0,4]:

#1  0x0000000000400b4c in quicksort (
    v=std::vector of length 1000, capacity 1000 = {...}, begin=0, end=4)
    at quicksort.cpp:14
#2  0x0000000000400cc1 in quicksort (
    v=std::vector of length 1000, capacity 1000 = {...}, begin=0, end=4)
    at quicksort.cpp:43
#3  0x0000000000400cc1 in quicksort (
    v=std::vector of length 1000, capacity 1000 = {...}, begin=0, end=4)
    at quicksort.cpp:43
#4  0x0000000000400cc1 in quicksort (
    v=std::vector of length 1000, capacity 1000 = {...}, begin=0, end=4)
    at quicksort.cpp:43
#5  0x0000000000400cc1 in quicksort (
    v=std::vector of length 1000, capacity 1000 = {...}, begin=0, end=4)
    at quicksort.cpp:43
#6  0x0000000000400cc1 in quicksort (
    v=std::vector of length 1000, capacity 1000 = {...}, begin=0, end=4)
    at quicksort.cpp:43
#7  0x0000000000400cc1 in quicksort (
    v=std::vector of length 1000, capacity 1000 = {...}, begin=0, end=4)
    at quicksort.cpp:43
  • 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-12T11:22:53+00:00Added an answer on June 12, 2026 at 11:22 am
    int pivot = v[std::rand() % v.size()];
    
    int x = 0;
    int y = v.size() -1;
    

    You explicitly pass begin and end positions, and yet you choose the pivot element from the complete list. That doesn’t sound right. You should choose a pivot element between begin and end. Also, x should start at begin, and y should start at end. Otherwise, you are always processing the complete list in each recursive step, which would explain the infinite recursion you are experiencing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code works well for displaying separate xml items in my main.xml. But is
This code works fine, but I'll want to handle exception if any thing goes
I have written a small code, but found something amazing. I have a class
We have quite a bit of reusable code at work (a good thing). Whenever
This code works well: UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:pickerFrame]; [pickerView addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
This code works fine in Chrome. However, in Firefox, when it hits the GMxhr
This code works for me except it only checks for a duplicate once, and
This code works: monkey.h @interface monkey : NSObject { NSNumber *monkeyRanch; } @property (nonatomic,
This code works (when ControlType=dropDown then the background yellow ): <Window x:Class=TestCollapsed.Views.MainView xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
this code works fine until I start scrolling: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

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.