0

I am trying to perform selection sort watching the pseudocode which says "we take the first element and then check for the minimum element excluding the first element, once found we swap the minimum with the first and the first points to the next element and the process goes on..." i tried to code but i am getting the same vector which i provided there is no sorting happening why is that?

#include <bits/stdc++.h>
using namespace std;


void selection(vector <int>& vp, int &n)
{
    for(int i=0;i<n;i++)
    {
        int sorted=vp[i];
        int iterator = i+1;
        int min=INT_MAX;
        while(iterator<n)
        {
            if(vp[iterator]<min)
            {
                min=vp[iterator];
            }
            iterator++;
        }
        swap(sorted,min);
    }
    
    for(int i=0;i<n;i++)
    {
        cout<<vp[i]<<" ";
    }
}

int main()
{
    vector <int> vp{12,45,23,51,19,8};
    int n = vp.size();
    selection(vp,n);
}
  • 4
    Ome problem is that you are confusing indices with values. `sorted` is an index, but `min` is a value. How should you swap those? Think carefully about what exactly you want to swap. – n. m. could be an AI Jul 12 '23 at 09:11
  • 3
    OT: Please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) Whatever resource you're using to learn those bad habits is not very good. – Some programmer dude Jul 12 '23 at 09:12
  • @n.m.willseey'allonReddit i have fixed that but still not working any ideas what should be the problem – Akshat kant Jul 12 '23 at 09:47
  • @Someprogrammerdude i know, i just have a bad habit of using those, still is there any logical issue. i mean it should work what could be the issue – Akshat kant Jul 12 '23 at 09:48
  • 1
    "ive fixed but still not working" Then you have not fixed. I have no idea what you have done. Please post a separate question with new code. – n. m. could be an AI Jul 12 '23 at 09:48
  • @n.m.willseey'allonReddit please check again. sorted i have made vp[i] while iterator as i+1 – Akshat kant Jul 12 '23 at 09:50
  • Exact duplicate of [I am having issue in selection sort](https://stackoverflow.com/q/76669340/207421)]. – user207421 Jul 12 '23 at 10:05
  • Again, *Think carefully about what exactly you want to swap*. Hint: suppose your array is `{2,1,3,2,1,3}`. What exactly will `swap(sorted,min);` do? – n. m. could be an AI Jul 12 '23 at 10:26
  • @n.m.willseey'allonReddit 2 is stored in sorted and iterator searches for the minimum which works from i+1 to n-1. i got 1 as the minmum store it in min. now i am swapping the sorted with min and then this process goes on. That is what i think i am doing – Akshat kant Jul 12 '23 at 10:48
  • Please read again. *suppose your array is `{2,1,3,2,1,3}`. What exactly will `swap(sorted,min);` do?* – n. m. could be an AI Jul 12 '23 at 10:52
  • @n.m.willseey'allon Reddit I cant find anything please tell me what am i doing wrong and what should i do else – Akshat kant Jul 12 '23 at 10:57
  • Habits, good and bad, tend to stick (as shown in your question). So why not bit the bullet and try to work in some good habits? Even for smaller programs or programs you just going to throw away once you finished them? If you work in some good habits, you don't have to remember and think about your bad habits when it's time for interviews or other similar situations, as the good habits will come out automatically. – Some programmer dude Jul 12 '23 at 11:09
  • Using name as `min` whereas `std::min` is in scope (with the `using namespace std;`) might be dangerous. – Jarod42 Jul 12 '23 at 13:27
  • For the "type issue" (index vs value), try another kind of vector, i.e `std::vector` or `std::vector`, so index and value won't have the same type. – Jarod42 Jul 12 '23 at 13:29

1 Answers1

1

Here in this line

swap(sorted,min);

You are interchanging the values of sorted and min, which are local variables holding the values of minimal element and the element to change. The interchange of these values has no effect on array because they only hold a copy of the required values.

You can hold the indexes of required elements

int sorted = i;
int iterator = i + 1;
int min_index = i;
while(iterator<n)
{
   if(vp[iterator]<min)
   {
       min_index=iterator;
   }
   iterator++;
} 
swap(vp[sorted], vp[min_index]);
Karen Baghdasaryan
  • 2,407
  • 6
  • 24