full coding for selection sort in c#

i need a coding for selection sort in c# the below coding i had search

Page 1 of 1

1 Replies - 2464 Views - Last Post: 25 August 2009 - 10:32 PM Rate Topic: -----

#1 vaday_rockey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-August 09

full coding for selection sort in c#

Post icon  Posted 25 August 2009 - 10:07 PM

/// <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!");
	}
}



Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: full coding for selection sort in c#

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1633
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: full coding for selection sort in c#

Posted 25 August 2009 - 10:32 PM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1