Skip to main content
added 45 characters in body
Source Link
kzidane
  • 17.7k
  • 3
  • 28
  • 100

The pseudocode for selection sort is as follows

for int i = 0 to i < n - 1, increment i by 1
{
    declare an int named min and set it to i
    
    for int j = i + 1 to j < n, increment j by 1
    {
        if values[min] > values[j]
        {
            set min to j
        }
    }

    swap values[i] and values[min]
}

You did most of it correctly though! However, you shouldn't be swapping the two values every time values[min] > values[j] because that would mess things up. Consider the following example

array: 5, 4, 3, 2, 1
pick the 5
walkthrough the list {4, 3, 2, 1} and pick the least element (i.e., 1)
swap it with the 5
array: 1, 4, 3, 2, 5
...

This way, you guarantee that after the first iteration of the outer loop, the least element in the array will be positioned at index 0.

What you're doing is something like

array: 5, 4, 3, 2, 1
set i to 0
set min to i
set j to 1
walkthrough the list {4, 3, 2, 1}
if values[min] > values[j]
{
    set min to j
    swap them
}

array: 4, 5, 3, 2, 1
i is still 0
min = 1
j is now 2
if (values[min] > values[j])
{
    set min to j
    swap them
}

array: 4, 3, 5, 2, 1
...

array: 4, 3, 2, 1, 5
then i = 1

and you'll never get back to values[0]values[0] (which is 44) again even though it's not in it's correct position yet.

The pseudocode for selection sort is as follows

for int i = 0 to i < n - 1, increment i by 1
{
    declare an int named min and set it to i
    
    for int j = i + 1 to j < n, increment j by 1
    {
        if values[min] > values[j]
        {
            set min to j
        }
    }

    swap values[i] and values[min]
}

You did most of it correctly though! However, you shouldn't be swapping the two values every time values[min] > values[j] because that would mess things up. Consider the following example

array: 5, 4, 3, 2, 1
pick the 5
walkthrough the list {4, 3, 2, 1} and pick the least element (i.e., 1)
swap it with the 5
array: 1, 4, 3, 2, 5
...

This way, you guarantee that after the first iteration of the outer loop, the least element in the array will be positioned at index 0.

What you're doing is something like

array: 5, 4, 3, 2, 1
set i to 0
set min to i
set j to 1
walkthrough the list {4, 3, 2, 1}
if values[min] > values[j]
{
    set min to j
    swap them
}

array: 4, 5, 3, 2, 1
i is still 0
min = 1
j is now 2
if (values[min] > values[j])
{
    set min to j
    swap them
}

array: 4, 3, 5, 2, 1
...

array: 4, 3, 2, 1, 5
then i = 1

and you'll never get back to values[0] (which is 4) again even though it's not in it's correct position.

The pseudocode for selection sort is as follows

for int i = 0 to i < n - 1, increment i by 1
{
    declare an int named min and set it to i
    
    for int j = i + 1 to j < n, increment j by 1
    {
        if values[min] > values[j]
        {
            set min to j
        }
    }

    swap values[i] and values[min]
}

You did most of it correctly though! However, you shouldn't be swapping the two values every time values[min] > values[j] because that would mess things up. Consider the following example

array: 5, 4, 3, 2, 1
pick the 5
walkthrough the list {4, 3, 2, 1} and pick the least element (i.e., 1)
swap it with the 5
array: 1, 4, 3, 2, 5
...

This way, you guarantee that after the first iteration of the outer loop, the least element in the array will be positioned at index 0.

What you're doing is something like

array: 5, 4, 3, 2, 1
set i to 0
set min to i
set j to 1
walkthrough the list {4, 3, 2, 1}
if values[min] > values[j]
{
    set min to j
    swap them
}

array: 4, 5, 3, 2, 1
i is still 0
min = 1
j is now 2
if (values[min] > values[j])
{
    set min to j
    swap them
}

array: 4, 3, 5, 2, 1
...

array: 4, 3, 2, 1, 5
then i = 1

and you'll never get back to values[0] (which is 4) again even though it's not in it's correct position yet.

added 45 characters in body
Source Link
kzidane
  • 17.7k
  • 3
  • 28
  • 100

The pseudocode for selection sort is as follows

for int i = 0 to i < n - 1, increment i by 1
{
    declare an int named min and set it to i
    
    for int j = i + 1 to j < n, increment j by 1
    {
        if values[min] > values[j]
        {
            set min to j
        }
    }

    swap values[i] and values[min]
}

You did most of it correctly though! However, you shouldn't be swapping the two values every time values[min] > values[j] because that would mess things up. Consider the following example

array: 5, 4, 3, 2, 1
pick the 5
walkthrough the list {4, 3, 2, 1} and pick the least element (i.e., 1)
swap it with the 5
array: 1, 4, 3, 2, 5
...

This way, you guarantee that after the first iteration of the outer loop, the least element in the array will be positioned at index 0.

What you're doing is something like

array: 5, 4, 3, 2, 1
set i to 0
set min to i
set j to 1
walkthrough the list {4, 3, 2, 1}
if values[min] > values[j]
{
    set min to j
    swap them
}

array: 4, 5, 3, 2, 1
i is still 0
min = 1
j is now 2
if (values[min] > values[j])
{
    set min to j
    swap them
}

array: 4, 3, 5, 2, 1
...

array: 4, 3, 2, 1, 5
then i = 1
and you'll never get back to values[0] (which is 4) again

and you'll never get back to values[0] (which is 4) again even though it's not in it's correct position.

The pseudocode for selection sort is as follows

for int i = 0 to i < n - 1, increment i by 1
{
    declare an int named min and set it to i
    
    for int j = i + 1 to j < n, increment j by 1
    {
        if values[min] > values[j]
        {
            set min to j
        }
    }

    swap values[i] and values[min]
}

You did most of it correctly though! However, you shouldn't be swapping the two values every time values[min] > values[j] because that would mess things up. Consider the following example

array: 5, 4, 3, 2, 1
pick the 5
walkthrough the list {4, 3, 2, 1} and pick the least element (i.e., 1)
swap it with the 5
array: 1, 4, 3, 2, 5
...

This way, you guarantee that after the first iteration of the outer loop, the least element in the array will be positioned at index 0.

What you're doing is something like

array: 5, 4, 3, 2, 1
set i to 0
set min to i
set j to 1
walkthrough the list {4, 3, 2, 1}
if values[min] > values[j]
{
    set min to j
    swap them
}

array: 4, 5, 3, 2, 1
i is still 0
min = 1
j is now 2
if (values[min] > values[j])
{
    set min to j
    swap them
}

array: 4, 3, 5, 2, 1
...

array: 4, 3, 2, 1, 5
then i = 1
and you'll never get back to values[0] (which is 4) again

The pseudocode for selection sort is as follows

for int i = 0 to i < n - 1, increment i by 1
{
    declare an int named min and set it to i
    
    for int j = i + 1 to j < n, increment j by 1
    {
        if values[min] > values[j]
        {
            set min to j
        }
    }

    swap values[i] and values[min]
}

You did most of it correctly though! However, you shouldn't be swapping the two values every time values[min] > values[j] because that would mess things up. Consider the following example

array: 5, 4, 3, 2, 1
pick the 5
walkthrough the list {4, 3, 2, 1} and pick the least element (i.e., 1)
swap it with the 5
array: 1, 4, 3, 2, 5
...

This way, you guarantee that after the first iteration of the outer loop, the least element in the array will be positioned at index 0.

What you're doing is something like

array: 5, 4, 3, 2, 1
set i to 0
set min to i
set j to 1
walkthrough the list {4, 3, 2, 1}
if values[min] > values[j]
{
    set min to j
    swap them
}

array: 4, 5, 3, 2, 1
i is still 0
min = 1
j is now 2
if (values[min] > values[j])
{
    set min to j
    swap them
}

array: 4, 3, 5, 2, 1
...

array: 4, 3, 2, 1, 5
then i = 1

and you'll never get back to values[0] (which is 4) again even though it's not in it's correct position.

added 365 characters in body
Source Link
kzidane
  • 17.7k
  • 3
  • 28
  • 100

The pseudocode for selection sort is as follows

for int i = 0 to i < n - 1, increment i by 1
{
    declare an int named min and set it to i
    
    for int j = i + 1 to j < n, increment j by 1
    {
        if values[min] > values[j]
        {
            set min to j
        }
    }

    swap values[i] and values[min]
}

You did most of it correctly though! However, you shouldn't be swapping the two values every time values[min] > values[j] because that would mess things up. Consider the following example

array: 5, 4, 3, 2, 1
pick the 5
walkthrough the list {4, 3, 2, 1} and pick the least element (i.e., 1)
swap it with the 5
array: 1, 4, 3, 2, 5
...

This way, you guarantee that after the first iteration of the outer loop, the least element in the array will be positioned at index 0.

What you're doing is something like

array: 5, 4, 3, 2, 1
set i to 0
set min to i
set j to 1
walkthrough the list {4, 3, 2, 1}
if 5values[min] > thevalues[j]
{
 current element  set min to j
    swap them
}

array: 4, 5, 3, 2, 1
i is still 0
min = 1
j is now 2
if (values[min] > values[j])
{
    set min to j
    swap them
}

array: 4, 3, 5, 2, 1
...

array: 4, 3, 2, 1, 5
then i = 1
and you'll never get back to values[0] (which is 4) again

The pseudocode for selection sort is as follows

for int i = 0 to i < n - 1, increment i by 1
{
    declare an int named min and set it to i
    
    for int j = i + 1 to j < n, increment j by 1
    {
        if values[min] > values[j]
        {
            set min to j
        }
    }

    swap values[i] and values[min]
}

You did most of it correctly though! However, you shouldn't be swapping the two values every time values[min] > values[j] because that would mess things up. Consider the following example

array: 5, 4, 3, 2, 1
pick the 5
walkthrough the list {4, 3, 2, 1} and pick the least element (i.e., 1)
swap it with the 5
array: 1, 4, 3, 2, 5
...

This way, you guarantee that after the first iteration of the outer loop, the least element in the array will be positioned at index 0.

What you're doing is something like

array: 5, 4, 3, 2, 1
walkthrough the list {4, 3, 2, 1}
if 5 > the current element
    swap them

array: 4, 5, 3, 2, 1
...

The pseudocode for selection sort is as follows

for int i = 0 to i < n - 1, increment i by 1
{
    declare an int named min and set it to i
    
    for int j = i + 1 to j < n, increment j by 1
    {
        if values[min] > values[j]
        {
            set min to j
        }
    }

    swap values[i] and values[min]
}

You did most of it correctly though! However, you shouldn't be swapping the two values every time values[min] > values[j] because that would mess things up. Consider the following example

array: 5, 4, 3, 2, 1
pick the 5
walkthrough the list {4, 3, 2, 1} and pick the least element (i.e., 1)
swap it with the 5
array: 1, 4, 3, 2, 5
...

This way, you guarantee that after the first iteration of the outer loop, the least element in the array will be positioned at index 0.

What you're doing is something like

array: 5, 4, 3, 2, 1
set i to 0
set min to i
set j to 1
walkthrough the list {4, 3, 2, 1}
if values[min] > values[j]
{
    set min to j
    swap them
}

array: 4, 5, 3, 2, 1
i is still 0
min = 1
j is now 2
if (values[min] > values[j])
{
    set min to j
    swap them
}

array: 4, 3, 5, 2, 1
...

array: 4, 3, 2, 1, 5
then i = 1
and you'll never get back to values[0] (which is 4) again
Source Link
kzidane
  • 17.7k
  • 3
  • 28
  • 100
Loading