Sorting Algorithms
http://www.codeproject.com/Articles/79040/Sorting-Algorithms-Codes-in-C-NET
static void Main(string[] args)
{
List<int> objList = new List<int> { 2, 6, 3, 7, 1, 8, };
foreach (int a in objList)
{
Console.WriteLine("Before sort : {0}",a);
}
Console.WriteLine(" ");
List<int> objSortedList = bubblesort(objList);
foreach (int a in objSortedList)
{
Console.WriteLine("After sort : {0}", a);
}
Console.ReadLine();
}
public static List<int> bubblesort(List<int> a)
{
int temp;
// foreach(int i in a)
for (int i = 1; i <= a.Count; i++)
for (int j = 0; j < a.Count - i; j++)
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
return a;
}
static void Main(string[] args)
{
List<int> objList = new List<int> { 2, 6, 3, 7, 1, 8, };
foreach (int a in objList)
{
Console.WriteLine("Before sort : {0}",a);
}
Console.WriteLine(" ");
List<int> objSortedList = bubblesort(objList);
foreach (int a in objSortedList)
{
Console.WriteLine("After sort : {0}", a);
}
Console.ReadLine();
}
public static List<int> bubblesort(List<int> a)
{
int temp;
// foreach(int i in a)
for (int i = 1; i <= a.Count; i++)
for (int j = 0; j < a.Count - i; j++)
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
return a;
}
Comments
Post a Comment