Home > Enterprise >  Selection Sort Implementation with C incorrect
Selection Sort Implementation with C incorrect

Time:03-30

really new to C , trying to instantiate some basic algorithms with it. Having trouble returning the correct result for selection sort. Here is my code

#include <iostream>
#include <array>
#include <vector>

using namespace std;

// Selection Sort :

int findMin(vector<int> &arr, int a)
{
    int m = a;
    for (int i = a   1; i < arr.size(); i  )
    {
        if (arr[i] < arr[m])
        {
            m = i;
        }
        return m;
    }
}

void swap(int &a, int &b)
{
    int temp = a;
    a = b;
    b = temp;
}

void selectionSort(vector<int> &arr)
{
    if (!arr.empty())
    {
        for (int i = 0; i < arr.size();   i)
        {
            int min = findMin(arr, i);
            swap(arr[i], arr[min]); // Assume a correct swap function
        }
    }
}

void print(vector<int> &arr)
{
    if (!arr.empty())
    {
        for (int i = 0; i < arr.size(); i  )
        {
            cout << arr[i] << "";
            cout << endl;
        }
    }
}

int main()
{
    vector<int> sort;
    sort.push_back(2);
    sort.push_back(1);
    sort.push_back(7);
    sort.push_back(4);
    sort.push_back(5);
    sort.push_back(3);
    print(sort);
    cout << "this was unsorted array";
    cout << endl;
    cout << findMin(sort, 0);
    cout << "this was minimum";
    cout << endl;

    selectionSort(sort);
    print(sort);
}

I am getting the following results:

comparison_sort.cpp:20:1: warning: non-void function does not return a value in all control paths [-Wreturn-type]
}
^
1 warning generated.
2
1
7
4
5
3
this was unsorted array
1
this was minimum
1
2
4
5
3
0

My question is: What is causing this control path error? Why is the "7" here being replaced with a "0"?

Thanks in advance! Sorry for the noob question.

I have reviewed all my current functions and nothing seems to explain why the 7 is replaced with a 0. I have tried multiple integers and it looks like the maximum number is always replaced.

CodePudding user response:

The warning is very real, and it alludes to the problem that's breaking your sort as well.

You are currently returning m inside your loop body. What that means is that if the loop is entered, then the function will return m on the very first time around the loop. It only has a chance to check the first element.

And of course, if a is the last index of the array, then the loop will never execute, and you will never explicitly return a value. This is the "control path" which does not return a value.

It's quite clear that you've accidentally put return m; in the wrong place, and even though you have good code indentation, some inexplicable force is preventing you from seeing this. To fix both the warning and the sorting issue, move return m; outside the loop:

int findMin(vector<int> &arr, int a)
{
    int m = a;
    for (int i = a   1; i < arr.size(); i  )
    {
        if (arr[i] < arr[m])
        {
            m = i;
        }
    }
    return m;
}
  • Related