Need help in clearing a concept

here we are not considering the last element but when the array is like this (8,4,5,1,3,2) then the last element is 2 so after sorting the whole array without considering the last element becomes (1,3,4,5,8,2) but 2 will be in the middle right …I am not getting if we donot consider the last element then how 2 comes in between

I am unable to get u are trying to tell
basically in selection sort u find the smallest element in the array and swap it to the ith index

eg (8,4,5,1,3,2)
index range we analyse [0-4] smallest 1 swap it to the 0th index => 1 4 5 8 3 2
index range we analyse [1-4] smallest 2 swap it to the 1th index => 1 2 5 8 3 4
index range we analyse [2-4] smallest 3 swap it to the 2th index => 1 2 3 8 5 4
and so on

please try to explain u are not understanding

Can u explain me the full code once again?

eg (8,4,5,1,3,2)
index range we analyse [0-4] smallest 1 swap it to the 0th index => 1 4 5 8 3 2
index range we analyse [1-4] smallest 2 swap it to the 1th index => 1 2 5 8 3 4
index range we analyse [2-4] smallest 3 swap it to the 2th index => 1 2 3 8 5 4
index range we analyse [3-4] smallest 4 swap it to the 3th index => 1 2 3 4 5 8
index range we analyse [4-4] smallest 5 swap it to the 4th index => 1 2 3 4 5 8 ( 5 swapped with 5 )
then 8 is in the correct position
now the array has become sorted
ALGO

  1. for i from 0 to n-1
  2. Find the index of the smallest element in the array from i+1 to n-1
  3. swap it with the ith indexto make it increasingly sorted

for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;

    // Swap the found minimum element with the first element  
    swap(&arr[min_idx], &arr[i]);  
}

maam u take index range from [0-5] but it will go upto 4 i.e [0-4] because i=0 to less than n-1 i.e 4…isnt it?

oh sorry i am extremely sorry
it should be n-1
Ill update the 5s to 4
thanks for correcting

so maam while i will be moving then what about 2?will it remain in the same position?

That is Maam where u have explained the code there will be changes from line 2
“index range we analyse [1-4] smallest 2 swap it to the 1th index” can u correct from this line and explain me once again?

Maam in this video they are considering the whole array…but prateek bhaiya didnt considered the last element …

bro ekh bar dry run karo dono prateek bhiaya ke tarike se and gfg jise samjh aye and answer aye
code vahi sahi h
dry run to karo
2-3 khud example lo

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

when i open the link to rate experience it was showing this page couldnot be found

it`s ok
if u have a positive experience i am happy, in case u still have doubt regarding this u can comment below

yaa my doubt was cleared…In case of any difficulty I will contact you further

1 Like