0

My sorting algorithm keeps turning out 10-15 each of the same number its suppose to sort my array which has a size of 2000 with numbers from 1-100. It's a homework assignment I've only been coding in C++ for 4 months and I've been working on it bout all day and it's due 12:00. Please help. Just can't figure out the algorithm

//Function to sort the numbers
void sortNumbers(int nums[], int ARRAY_SIZE) {
    int startScan, minIndex, minValue;
    for (startScan = 0; startScan < (ARRAY_SIZE-1); startScan++) {
        minIndex = startScan;
        minValue = nums[startScan];

        for(int index = startScan+1; index < ARRAY_SIZE; index++) {
            if(nums[index]<  minValue) {
                minValue = nums[index];
                minIndex = index;
            }

            nums[minIndex] = nums[startScan];
            nums[startScan] = minValue;
        }
    }
}
6
  • 4
    Lack of planning on your part does not constitute an emergency on ours. Next time, start your homework sooner. And please don't think I'm being unnecessarily harsh, I have to tell my own son the same darn thing. Sometimes it's good to fail, it makes you less likely to fail next time and prepares you for the real world :-) Commented Dec 11, 2014 at 3:14
  • The bubble sort is the easiest to implement. If you want to hand in something, rewrite it so that it does a bubble sort. Commented Dec 11, 2014 at 3:17
  • However, you should be able to figure out what's going on if you learn some basic debugging skills (either in an actual debugger or by the judicious placement of printf statements). Institutions tend to concentrate on coding when the most important skill is debugging. IMNSHO. Commented Dec 11, 2014 at 3:24
  • And one more thing re: keeps turning out 10-15 each of the same number, you are aware that 2000 numbers all in the range 1-100 will average about 20 copies of each number, right? And, if you sort them, that's what you'll see: 1 1 1 1 1 2 2 2 2 3 3 3 3... (but with about 20 of each). Are you sure you're not just tilting at windmills? Commented Dec 11, 2014 at 3:27
  • To check that, start with your original 2000-element array and count how many of each number (1-100) there is. Then do the same for your sorted array. If the counts are identical pre- and post-sort, you have no problem. Commented Dec 11, 2014 at 3:30

4 Answers 4

2

You just need to do the swapping outside the inner loop.

Get this part

nums[minIndex] = nums[startScan];
nums[startScan] = minValue;

out of the inner loop.

Your code should look like this:

void sortNumbers(int nums[], int ARRAY_SIZE) {
    int startScan, minIndex, minValue;
    for (startScan = 0; startScan < (ARRAY_SIZE-1); startScan++) {
        minIndex = startScan;
        minValue = nums[startScan];

        for(int index = startScan+1; index < ARRAY_SIZE; index++) {
            if(nums[index]<  minValue) {
                minValue = nums[index];
                minIndex = index;
            }
        }

        nums[minIndex] = nums[startScan];
        nums[startScan] = minValue;
    }
}

Here's a very clear explanation of Selection Sort with images and steps: http://www.algolist.net/Algorithms/Sorting/Selection_sort

Sign up to request clarification or add additional context in comments.

Comments

1

There is nothing wrong with the concept of what you've done, all I'd be looking at is the placement of that third last closing brace }.

Do you really want to be swapping inside the inner loop when the whole intent of the inner loop is to find the element you want to swap with? By definition, you can't have found it until the loop is finished.


Try figure that out for yourself but, if you can't for some reason, see below:

void sortNumbers(int nums[], int ARRAY_SIZE) {
    int startScan, minIndex, minValue;

    // For every array index bar the last.

    for (startScan = 0; startScan < (ARRAY_SIZE-1); startScan++) {
        // Start with minimum at that point.

        minIndex = startScan;
        minValue = nums[startScan];

        // Check against every other array element not yet placed.

        for(int index = startScan+1; index < ARRAY_SIZE; index++) {
            if(nums[index]<  minValue) {
                minValue = nums[index];
                minIndex = index;
            }
        }

        // Swap when you've found the minimum.

        nums[minIndex] = nums[startScan];
        nums[startScan] = minValue;
    }
}

Comments

0

ALL sort routines need a temporary copy of the values being swapped. SWAPPED. Think about the need for this. The first thing that stands out is that I do not see swapping anywhere. You are smashing your values resulting in duplication of them. Where did you get this design from?

2 Comments

Guru, you may find that minValue is the temporary copy in this case. Hence swapping is happening, it's just very obtuse.
counting sort doesn't use swapping, although i agree that OP should probably attend class.
0

2000 > 100, you might find bucketsort -> counting sort a suitable method to solve your current problem.

edit: in english: you have an array of 100 integers that keep a count of what numbers are in the input

As others have mentioned, your sort isn't even swapping values properly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.