300x250 AD TOP

helooo
Tagged under:

Selection Sort



Source:

         using System; 
namespace venky
{
class Program
{
public static void Main()
{
int[] a = new int[100];
int min, pass, i;
Console.WriteLine("Number of elements in the array ?");
string s = Console.ReadLine();
int x = Int32.Parse(s);
Console.WriteLine("-----------------------");
Console.WriteLine(" array elements ");
Console.WriteLine("-----------------------");
for (int j = 0; j < x; j++)
{
string s1 = Console.ReadLine();
a[j] = Int32.Parse(s1);
}
for (pass = 0; pass < x - 1; pass++)
{
min = pass;
for (i = pass + 1; i < x; i++)
{
if (a[min] > a[i])
min = i;
}
if (min != pass)
{
int k = a[pass];
a[pass] = a[min];
a[min] = k;
}
}
Console.WriteLine("--------------------------------------------");
Console.WriteLine("Sorted elements of an array are(selection sort)");
for (int j = 0; j < x; j++)
Console.WriteLine(a[j]);
}
}
}

Output:-

Number of elements in the array ? 
4 
----------------------- 
array elements 
----------------------- 
12 
34 
23 
9 
-------------------------------------------- 
Sorted elements of an array are(selection sort) 
9 
12 
23 
34