开发者

Cocktail Sort code segfaults - not sure why

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;
    bo开发者_如何转开发ol 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.


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


This may help you

import java.util.Scanner;

public class CocktailSort {
static Scanner in;

public static void main(String args[]) {
in = new Scanner(System.in);
int[] data = new int[10];
int i, n = 10, c;
System.out.print("\nEnter the data");
for (i = 0; i < 10; i++) {
data[i] = in.nextInt();
}

do {
 // Rightward pass will shift the largest element to its correct
 // place at the end

for (i = 0; i < n - 1; i++) {
if (data[i] > data[i + 1]) {
data[i] = data[i] + data[i + 1];
data[i + 1] = data[i] - data[i + 1];
data[i] = data[i] - data[i + 1];
}
}
n = n - 1;
// Leftward pass will shift the smallest element to its correct
// place at the beginning

for (i = 10 - 1, c = 0; i >= c; i--) {
if (data[i] < data[i - 1]) {
data[i] = data[i] + data[i - 1];
data[i - 1] = data[i] - data[i - 1];
data[i] = data[i] - data[i - 1];
}
}
c = c + 1;
} while (n != 0 && c != 0);
System.out.print("The sorted elements are:");
for (i = 0; i < 10; i++) {
System.out.print(data[i] + "\t");
}
}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜