friendsterTalk - Friendster Forum

friendsterTalk - Friendster Forum

Welcome guest! Please Login or Register.

#1  2008-11-11 17:35:50

creamownedz
 active server protocol
creamownedz's display avatar
» FriendsterTalker
PsychoCoder
Class-S
Location: Ftalk & MCTF
Registered: 2008-03-22
Posts: 115
Last visit: Yesterday
Reputation: 7

Perform a selection sort [C#]

This a snippet to perform a selection sort on an array. Though it was originally written for a console application it can easily be ported to a Windows application



Code:

/// <summary>
/// method to perform a selection sort on an array
/// </summary>
public static void SelectionSort()
{
    //variable to hold the array size
    int[] arrayNum = new int[100];
    int minNum;
    bool isNum = false;
    int sizeNum;
    //ask the user for the size of the array
    Console.WriteLine("Number of elements in the array ?");
    //read in the value
    string sizeString = Console.ReadLine();
    isNum = Int32.TryParse(sizeString, out sizeNum);
    //now check for a numeric value
    if(isNum)
    {
        //ask the user for the array values
        Console.WriteLine("-----------------------");
        Console.WriteLine("Enter array values (numeric).");
        Console.WriteLine("-----------------------");
        //now we loop to the size of the array
        for (int i = 0; i < sizeNum; i++)
        {
            //temp value for the loop
            int temp;
            //read in the users value
            string arrayValue = Console.ReadLine();
            //use TryParse to convert to number
            isNum = Int32.TryParse(arrayValue, out temp);
            //now make sure it's numeric
            if (isNum)
            {
                arrayNum[i] = temp;
            }
            else
            {
                Console.WriteLine("Array values must be numeric!");
                break;
            }
        }

        //start a 2nd loop for the size of the array
        for (int j = 0; j < sizeNum - 1; j++)
        {
            //assign value to value of the incrementer
            minNum = j;

            //start a 3rd loop
            for (int k = j + 1; k < sizeNum; k++)
            {
                //check this index of the array to see if it's larger
                //that the index of the second loop
                if (arrayNum[minNum] > arrayNum[k])
                {
                    minNum = k;
                }                            
            }

            //now see if the minNum isnt equal to the index of the 3rd loop
            if (minNum != j)
            {
                //now we start the sorting
                int finalNum = arrayNum[j];
                arrayNum[j] = arrayNum[minNum];
                arrayNum[minNum] = finalNum;
            }
        }

        //write out the sorted value of the initial array
        Console.WriteLine("--------------------------------------------");
        Console.WriteLine("Selection Sort results: ");
        //now a final loop to retrieve the items in the sorted value
        for (int l = 0; l < sizeNum; l++)
        {
            Console.WriteLine(arrayNum[l]);
        }
    }
    else
    {
        Console.WriteLine("The array size must be numeric!");
    }
}

...Giving of reputation are highly appreciating for me...
Computers don't make errors what they do they do on purpose
...computer is my expertise...
....an addicted coder....
....don't ripp all my tutorials....

Search Friendstertalk

Board footer

FriendsterTalk is not affiliated with Friendster.com
Copyright © 2002–2009 PunBB

[ 8 queries - 8.120 second ]

Pay Per Click Ads by pay per click advertising by Kontera

FriendsterTalk.com x

Welcome to FriendsterTalk! You'll need to login in order to fully use all the features and view all the sections of this site.

Please register if you're not yet a member. =)