Thread: Selection Sort

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Selection Sort

    I'd greatly appreciate it if someone could explain for me exactly how the selection sort algorithm works.

    Code:
    for (startScan = 0; startScan < (elems - 1); startScan++)
    {
    	minIndex = startScan;
    	minValue = array[startScan];
    
    	for (int index = startScan + 1; index < elems; index++)
    	{
    		if (array[index] < minValue)
    		{
    			minValue = array[index];
    			minIndex = index;
    		}
    	}
    	array[minIndex]  = array[startScan];
    	array[startScan] = minValue;
    }
    The relationship between

    Code:
    minIndex = startScan;
    minValue = array[startScan];
    and

    Code:
    array[minIndex]  = array[startScan];
    array[startScan] = minValue;
    is especially confusing me.

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    It really is good practice for you to be able to work through this on your own, otherwise algorithms will always be confusing to you. I'd suggest taking a piece of paper, creating a random unsorted array like say {10,5,7,1,0,14,12,2} and working through the sort one line at a time keeping track of all of the variables. I guarantee that in only a couple of times through the loop you will understand exactly what those lines are doing, and you'll learn a heck of a lot more than by having someone explain it to you.

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Smile

    Use a deck of cards or some good visual aid. This is what I do.
    Mr. C: Author and Instructor

  4. #4
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Hi,

    I'm a beginner too, and i also find it difficult to read through algorithms and find what they're doing easily, mind you...i've never really tried, so this seemed like a good piece of code.

    So i sat down, pasted the algorithm into msvc, and commented it all out, and i must say i'm very satisfied with myself, i understand it all and not only that, can write my own selection sort with relative ease.

    -Hybrid
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insertion and selection sort
    By Dashing Boy in forum C Programming
    Replies: 4
    Last Post: 08-29-2006, 04:42 PM
  2. Selection Sort problem #2
    By Twigstar in forum C++ Programming
    Replies: 7
    Last Post: 07-11-2005, 07:27 PM
  3. Selection Sort help
    By Twigstar in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2005, 08:39 PM
  4. Selection Sort
    By Bleueyes515 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2002, 08:33 PM
  5. selection sort records of chars
    By hew in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2002, 03:49 PM