+ Reply to Thread
Results 1 to 11 of 11

finding lowest number of moves to sort a list

  1. #1
    Forum Contributor
    Join Date
    03-12-2018
    Location
    London, England
    MS-Off Ver
    2016
    Posts
    118

    Smile finding lowest number of moves to sort a list

    Hello everyone. This is my first post.
    In an application I use for work I have a list of 41 items that have to be sorted in a specific order.
    Unfortunately the only way to order it, it is to move each item up and down until it reaches the desired position (and of course doing this changes the position of other items)
    Now, as this is a tedious job I need to do over 500 times, I wanted to see if there is a quick way to find using VBA the lowest number of moves to have them all sorted correctly.

    Let me be more clear with an example with just 4 items.
    I have 4 colours that have to sorted as:
    1. Yellow
    2. Blue
    3. Green
    4. Red

    Given an initial order:
    1. Yellow
    2. Red
    3. Green
    4. Blue

    The quickest way is:
    Move Red down by 2 positions
    Move Green down by 1 position.

    I know that it can also be like
    move Blue up by 2 positions
    Move red down by 1 position.

    I actually do not care, as long as I get the lowest number of moves.

    Is there a way I can get a VBA showing me the shortest possible list of moves I have to do to sort the full list the way I want?


    Thank you in advance to anyone that can help me.

  2. #2
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,080

    Re: finding lowest number of moves to sort a list

    Hi & welcome to the board.
    Why not create a custom sort list & use that?
    For more info have a look here
    https://www.ablebits.com/office-addi...-custom-lists/

  3. #3
    Forum Contributor
    Join Date
    03-12-2018
    Location
    London, England
    MS-Off Ver
    2016
    Posts
    118

    Re: finding lowest number of moves to sort a list

    Hi Fluff13
    actually that is not what I need cause I do not need to sort the list in excel, this is done by clicking an arrow down or arrow up in the software I use.
    What I am looking for is the lowest number of "clicks" i need to do to achieve the result I want...

  4. #4
    Registered User
    Join Date
    03-12-2018
    Location
    USA
    MS-Off Ver
    2013
    Posts
    1

    Re: finding lowest number of moves to sort a list

    Are you testing some sort of theory? Otherwise this doesn't sound to resourceful.

  5. #5
    Forum Contributor
    Join Date
    03-12-2018
    Location
    London, England
    MS-Off Ver
    2016
    Posts
    118

    Re: finding lowest number of moves to sort a list

    No theory. Pure practice, unfortunately.
    I have a list of 41 items and I only can move them up and down. But they are not in order and I wanted to see if using excel I can get the shorte list of moves to have them in the order I want

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: finding lowest number of moves to sort a list

    I am not an expert on this. Is this the kind of question that requires you to actually perform the sort in order to know how many moves it took? What kind of sorting algorithm are you considering, or is this a case of choosing the "best" sorting algorithm?

    I put "sort algorithm determine number of moves" into my favorite internet search engine. I saw several hits -- most of them discussions of different sorting algorithms. Most of those that addressed the number of moves seemed to imply that one must actually perform the sort to know how many moves it took. Without thoroughly researching the question, the solution I am seeing is:
    1) perform the sort using a variety of sorting algorithms
    2) count the number of moves each algorithm takes
    3) Determine which algorithm used the fewest moves

    If you are aware of a better algorithm than this "brute force" algorithm for determining number of moves (and fewest number of moves), explain the desired algorithm to us and we will try to help you program that algorithm into Excel.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  7. #7
    Forum Contributor
    Join Date
    03-12-2018
    Location
    London, England
    MS-Off Ver
    2016
    Posts
    118

    Re: finding lowest number of moves to sort a list

    The solution you suggest may work.
    I was just trying to see if I can get a list of moves that will end up making the list to be in the order I want. Considering that if you move one item up, automatically you are changing the position of all the others below, I wanted to see if if there was a way to determine the moves to do to get the final order without wasting time...
    Sorry I really don't know how to express myself clearer, not being English native it's a bit difficult

    Thank you

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: finding lowest number of moves to sort a list

    In your example the colors seem to be clumped together, if that's the case you may be able to copy each to its own range and then just paste them in your desired order - is that the case??
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  9. #9
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: finding lowest number of moves to sort a list

    There are more sort algorithms than you can shake a stick at. Some are more efficient sorting small lists (e.g. 'Bubble Sort') and others are more efficient at large lists. There is no telling ahead of time which algorithm is the most efficient for a specific set of data, although there are algorithms that will indicate general sort efficiency based on list size, and how 'out of order' the list is to start with.

    In the past I've used the following algorithms:
    a. Bubble Sort (extremely small code size and efficient use of memory - very important in the old days when mini-computers has 32 Kilobytes of memory and 1 Megabyte Hard Disk media was 15 inches in diameter and cost $300 US)
    b. Insertion Sort
    c. Shell Sort (which has many variations)

    By eyeballing the data, your manual algorithm is probably an 'Insertion Sort', where you insert data where you think it will fit best.

    Lewis

  10. #10
    Forum Contributor
    Join Date
    03-12-2018
    Location
    London, England
    MS-Off Ver
    2016
    Posts
    118

    Re: finding lowest number of moves to sort a list

    Ok thank you. Can I replicate an algorithm using vba?

    To better explain what I mean is:

    Going back to the colours example:

    Initial is:
    Red
    Blue
    Yellow
    Green

    Order I want to achieve:
    Blue
    Yellow
    Green
    Red


    Now, I can do:

    Move up Blue by 1
    Move up Yellow by 1
    Move up Green by 1

    OR - and this is what I wanted to see if was possible to get using a macro

    Move Red down by 3.


    What I need from the vba is the list of the moves! not that the macro sorts the order.

  11. #11
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: finding lowest number of moves to sort a list

    Please note that your request is for a 'custom sort order' which is beyond the scope I am willing to go. I prepended a letter to each of your colors to make the 'custom sort order' fit into existing standard sort algorithms. In most instances, the 'Shell Sort' seems to be the most efficient of the 4 algorithms below. I generally either use a 'Bubble Sort' for small sorts or a 'Sheet Sort' if I have a large amount of data to sort. I coded the other two algorithms today for comparison purposes and do not use them.

    See the attached file which benchmarks 4 sort algorithms and counts the number of moves (swaps) done by each algorithm:
    a. Bubble Sort: https://en.wikipedia.org/wiki/Bubble_sort
    b. Insertion Sort: https://en.wikipedia.org/wiki/Insertion_sort
    c. Selection Sort: https://en.wikipedia.org/wiki/Selection_sort
    d. Shell Sort: https://en.wikipedia.org/wiki/Shellsort

    The 'number of moves' is a misleading way to count efficiency. When sorting manually, you 'eyeball' the data and determine how to move the data, and each 'move' may take several seconds, especially with a larger amount of data. Computer algorithms work in much the same way.

    In the sample file the data can be arranged in four different ways prior to sorting:
    a. Evenly spaced pseudo-pseudo-random data (I was too lazy to implement computer generated random data)
    b. List in order except that the the 1st item should be the last item.
    c. List in order except that the last item should be the first item.
    d. List in reverse order


    Sorting depends on many different conditions including:
    a. The amount of memory available (all the algorithms tested have 100% of the data in memory at the same time). There is a classic school exercise where data from multiple magnetic tapes must be merged and sorted.
    b. The original order of the data.
    c. The amount of data.

    In ordinary code module ModSortComparison:
    Please Login or Register  to view this content.
    Lewis
    Last edited by LJMetzger; 03-13-2018 at 04:30 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. finding out number of lowest quotes of each supplier
    By shankar.nstl in forum Excel General
    Replies: 4
    Last Post: 01-31-2018, 01:59 AM
  2. Need help finding lowest number in row
    By Mixed in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-17-2014, 09:45 AM
  3. Finding Lowest Cost from a List with Wildcard
    By 123Steve in forum Excel General
    Replies: 11
    Last Post: 11-15-2011, 12:16 AM
  4. Finding lowest number in each position
    By nebb in forum Excel General
    Replies: 5
    Last Post: 06-09-2009, 09:08 PM
  5. Excel 2007 : Finding Lowest Number in a range of numbers.
    By stevenson08 in forum Excel General
    Replies: 7
    Last Post: 11-11-2008, 08:19 AM
  6. Finding highest and lowest number between to key numbers
    By garbs in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-10-2007, 06:35 PM
  7. [SOLVED] finding lowest priced item in a list
    By TimH in forum Excel General
    Replies: 3
    Last Post: 01-13-2006, 02:29 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1