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 991053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:02:42+00:00 2026-05-16T06:02:42+00:00

I wrote a Cocktail Sort algorithm, and am testing it by generating random vectors

  • 0

I wrote a Cocktail Sort algorithm, and am testing it by generating random vectors of size 500 up to 10,000 – running it 10 times per vector. After about the 2000-3000 length vector mark, the code segfaults. I expect it isn’t the test code as the same test is used for multiple sorting algorithms and it runs fine for every other algorithm. I am assuming somewhere I miss an end-condition and it tries to access an element of the input array that doesn’t exist… but I’m not entirely sure if that would cause a segfault to be run.

This is the code, I hope someone can spot my error. (I also would enjoy any comments on how it could be better – but please note I do value readability over speed for this code.)

void Sorting::cocktailSort(vector<int>& A) {

    int temp;

    // The first/last indexes to check. Anything before/after these indexes
    // is already sorted.
    int firstIndex = -1;
    int lastIndex = A.size()-1;
    bool swapped;

    do {
        firstIndex += 1;
        swapped = false;
        for(int i = firstIndex-1; i < lastIndex; i++) {
            if(A[i] > A[i+1]) {
                temp = A[i];
                A[i] = A[i+1];
                A[i+1] = temp;
                swapped = true;
            }
        }
        if(!swapped) break;
        swapped = false;
        lastIndex -= 1;
        for(int i = lastIndex; i >= firstIndex; i--) {
            if(A[i] < A[i-1]) {
                temp = A[i];
                A[i] = A[i-1];
                A[i-1] = temp;
                swapped = true;
            }
        }
    }while (swapped);

}

This is not homework.

  • 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-05-16T06:02:43+00:00Added an answer on May 16, 2026 at 6:02 am

    If you use A.at(i) instead of A[i], bounds checking will be done, and out-of-range exceptions thrown. That may be helpful for debugging.

    It appears to me that the access here…

        for(int i = firstIndex-1; i < lastIndex; i++) {
            if(A[i] > A[i+1]) {
    

    will be out-of-bounds when firstIndex is zero (the first iteration of the main loop).

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

Sidebar

Related Questions

I wrote some code to generate a boost::mpl::vector to use as a lookup table
I wrote a small algorithm using LINQ to read in a bunch of files
I wrote a java program to test the capabilities of processors by running an
Wrote a quick Java proggy to spawn 10 threads with each priority and calculate
I wrote a symfony task to fill a database of sample data. Here's a
I wrote a process explorer using C with GUI interface. I want to add
I wrote some script in a site. The script makes a new spreadsheet and
I wrote a simple XML file and a DTD file including an entity, but
I wrote a collada loader for my model viewer. Write now it loops through
I wrote a C++ DLL and it has been compiled. It requires Visual C++

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.