Sorting algorithms/Bubble sort: Difference between revisions

(add C# radix sort)
Line 415:
</pre>
 
=={{header|C sharp|C#}}==
{{works with|C sharp|C#|3.0+}}
<lang csharp>using System;
using System.Collections.Generic;
 
namespace RadixSortRosettaCode.BubbleSort
{
public static class ProgramBubbleSortMethods
{
//The "this" keyword before the method parameter identifies this as a C# extension
static void Sort(int[] old)
//method, which can be called using instance method syntax on any generic list,
//without having to modify the generic List<T> code provided by the .NET framework.
public static void BubbleSort<T>(this List<T> list) where T : IComparable
{
intbool i, jmadeChanges;
int[] tmpitemCount = new int[oldlist.Length]Count;
for (int shift = 31; shift > -1; --shift)do
{
jmadeChanges = 0false;
for (i = 0itemCount--; i < old.Length; ++i)
for (int i = 0; i < itemCount; i++)
{
bool move =if (oldlist[i].CompareTo(list[i <<+ shift1]) >= 0;)
if (shift == 0 ? !move : move) // shift the 0's to old's head{
old[i-j]T temp = oldlist[i + 1];
else list[i // move the+ 1's] to= tmplist[i];
tmplist[j++i] = old[i]temp;
madeChanges = true;
}
}
} while Array.Copy(tmp, 0, old, old.Length-j, jmadeChanges);
}
}
}
static void Main(string[] args)
 
//A short test program to demonstrate the BubbleSort. The compiler will change the
//call to testList.BubbleSort() into one to BubbleSortMethods.BubbleSort<T>(testList).
class Program
{
static void SortMain(int[] old)
{
List<int[]> oldtestList = new List<int[]> { 23, 57, 3, 2, 1, -34, 10, 12, 4 };
ConsoletestList.WriteLineBubbleSort(string.Join(", ", old));
Sortforeach (oldvar t in testList) Console.Write(t + " ");
Console.WriteLine(string.Join(", ", old));
Console.Read();
}
}
Anonymous user