Results 1 to 4 of 4

Math Help - Explanation of selection sort, which uses arithmetic progression

  1. #1
    Newbie
    Joined
    Feb 2013
    From
    Northern Ireland
    Posts
    3

    Explanation of selection sort, which uses arithmetic progression

    I'm training to be a software developer but I'm afraid some basic Maths has gotten in my way.
    We're learning about sorting right now, the first of which is a basic selection sort algorithm. Take the following numbers:

    [ 9, 5, 17, 11, 12 ]

    It takes the first number and sets it as the minimum number and compares it to everything after it. As 5 is smaller than it, it swaps the two numbers around and moves on. This leaves us with:

    [ 5, 9, 17, 11, 12 ]

    Next, it takes 5 as the minimum number. If searches the numbers after it and as none are smaller than 5, it stays in position and so on. This leaves us with the same terms:

    [ 5, 9, 17, 11, 12 ]

    The sort effectively works its way from left to right, leaving the numbers sorted as it goes.

    Our lecturer has told us that it is sorted in the following way.

    To find the smallest, visit n elements and make 2 visits to perform the swap.
    To find the next smallest, visit (n-1) elements and make 2 visits to perform the swap.
    To find the last term, visit 2 elements and make 2 visits to perform the swap.

    This translates to:

    n + 2 + (n-1) + 2 + (n-2) + 2 + ... ... + 2 + 2

    I know this equation then simplifies to this:

    n2/2 + 5n/2 - 3

    However, for the life of me, I cannot see how the top equation translates to this. I know that the formula to find the sum of terms in an arithmetic progression is this:

    n/2 [2a + (n-1)d] OR n/2 [a + l] where a = first term d = common difference and l = last term.

    Despite using this, I don't get the right answer. Could somebody please walk me through, STEP BY STEP!!, as to how I get to the answer?

    Kind regards and many thanks in advance.

    Edit: Wanted to add that I posted this in the pre-university forum as I remember studying this at A Level - just can't remember the outcome!!

    Last edited by amartin903; February 19th 2013 at 12:51 PM.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor

    Joined
    Apr 2005
    Posts
    15,707
    Thanks
    1470

    Re: Explanation of selection sort, which uses arithmetic progression

    So you have [n + 2] + [(n-1) + 2] + [(n-2) + 2] + ... ... + [1+ 2] + [0+ 2] and want to know how that is equal to n^2/2+ 5n/2- 3? Well, it's NOT! If n= 2, that would be [2+ 2]+ [1+ 2]+ [0+ 2]= 4+ 3+ 2= 9. That is NOT 2^2/2+ 5(2)/2- 3= 2+ 5- 3= 4.

    To sum [n + 2] + [(n-1) + 2] + [(n-2) + 2] + ... ... + [1+ 2] + [0+ 2], note that you have n+ 1 terms (from n down to 0), so you are adding 2 n+ 1 times and that sums to 2(n+1)= 2n+ 1. The sum 1+ 2+ ...+ (n-1)+ n is, as you say, an arithmetic progression and sums to n(n+1)/2. So your sum is 2n+1+ n(n+1)/2= 2n+ 1+ n^2/2+ n/2= n^2/2+ 3n/2+ 1.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Feb 2013
    From
    Northern Ireland
    Posts
    3

    Re: Explanation of selection sort, which uses arithmetic progression

    Could I ask you then a slightly strange follow up question. This is the website I originally posted this question on (and didn't understand the answer!)
    algorithms - Basic Maths question regarding the selection sort formula. - Mathematics

    Could I ask you what the person did wrong in the answer here? The reason why I'm asking is because both he and my lecturer have come up with the n^2/2 + 5n/2 - 3. I've also googled the formula, and the -3 one is the one that is generally listed. Please note - I'm not for a moment suggesting that your answer isn't correct. Rather I'm just trying to emphasize how confused I've gotten over this formula and how, as I can't find consensus on the outcome, I can't figure out how to get there! Thank you for the very clear answer you have given so far.

    Edit: To actually explain my post further, I'm not sure your formula would work. You asked how it would work if 2 were substituted in to the formula. But as the collection of numbers is random and unsorted, it would be impossible to find any particular nth term - all that I can find is the sum of all terms? I think? ... Possibly lol. I really don't know.
    Last edited by amartin903; February 19th 2013 at 01:13 PM.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Newbie
    Joined
    Feb 2013
    From
    Northern Ireland
    Posts
    3

    Re: Explanation of selection sort, which uses arithmetic progression

    Okay, my lecturer has emailed it to me and explained it like this. It's a weird sequence. Because it takes a collection of random, unsorted numbers the formula only tells the number of processes that are needed to sort a collection of numbers.

    The formula is effectively broken into two separate sequences:

    1. n + (n-1) + (n-2) + (n-3) + (n-4) + ... + 2
    2. 2 + 2 + 2 + 2 etc.

    The first part can be rearranged to look like this:

    1. 2 + 3 + 4 + 5 + 6 ... + n. This is effectively the same as 1 + 2 + 3 + 4 ... + n.

    However, in our sequence, we are not starting at 1, but rather 2, this 1 must be taken off the output of this one row of sums.

    The formula to calculate this line is: n/2 [2a + (n-1)d]. This gives n/2 [2*1 + (n-1)*1]. This gives n/2 [2 + n - 1]. Which is: n/2[n + 1]. Which is: n2/2 + n/2.
    When we remember to take our 1 off, we get the final formula for the first part of the sum:

    (n2/2 + n/2) - 1

    The second line contains all 2s, and (n-1) of them are needed. This gives the formula 2(n-1), which is: 2n - 2

    Putting the two formulae together gives: n2/2 + n/2 - 1 + 2n - 2.

    Simplified, this is: n2/2 + n/2 - 3 + 2n

    Multiply the 2n by 2 so it is over the number 2 as well gives the final output:

    n2/2 + n/2 - 3 + 4n/2, which is: n2/2 + 5n/2 - 3

    Any thoughts folks? I'm still fairly confused!
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 9
    Last Post: August 9th 2012, 09:33 AM
  2. Replies: 3
    Last Post: September 22nd 2011, 12:01 AM
  3. Arithmetic Progression or Arithmetic Series Problem
    Posted in the Math Topics Forum
    Replies: 1
    Last Post: October 8th 2009, 12:36 AM
  4. Replies: 8
    Last Post: March 23rd 2009, 07:26 AM
  5. Replies: 5
    Last Post: February 12th 2008, 04:02 PM

Search Tags


/mathhelpforum @mathhelpforum