• Data Sorting Issue
    1 replies, posted
I've been having some issues with a program I'm writing for an assignment. Everything seems to be working fine up until the sorting method because when it outputs nothing is sorted. I've taken a look through the code and I can't seem to find any logic errors. If a more experienced programmer could check this out I'd appreciate it. [CODE] private void PointerSort() { //The big ol' Exchange sort of doom string tempski; double tempavg; bool perfect=false; n = 6; while (perfect == false) { for (int j = 1; j <= n-1;j++ ) { for (int k = 1; k <= n;k++ ) { if (averaged[j] < averaged[k]) { tempski = resorts[j]; resorts[j] = resorts[k]; resorts[k] = tempski; tempavg = averaged[j]; averaged[j] = averaged[k]; averaged[k] = tempavg; } else { perfect = true; } } } } } [/CODE] No compiler errors and it's a near copy of the exchange sort used in the example assignment.
I don't know how your sorting algorithm is supposed to work, since I'm too lazy to google for exhange sort atm, but this seems just wrong: [code] if (averaged[j] < averaged[k]) { tempski = resorts[j]; resorts[j] = resorts[k]; resorts[k] = tempski; tempavg = averaged[j]; averaged[j] = averaged[k]; averaged[k] = tempavg; } else { perfect = true; }[/code] If one element is smaller than another then the whole loop will end and thus the function return. Also, arrays begin with 0. I [i]think[/i] you just have to check that you've switched an element at least once in the inner loop, and if that didn't happen then the algorithm is finished. There was some sorting-algorithm which checked that.
Sorry, you need to Log In to post a reply to this thread.