Sorting algorithms/Bubble sort: Difference between revisions

Line 460:
}</lang>
 
=={{header|CleanC sharp|C#}}==
{{works with|C sharp|C#|3.0+}}
Bubble sorting an array in-situ (using destructive updates), using Clean's uniqueness typing. We specified the type of <tt>sweep</tt> using strictness annotations to improve performance.
<lang cleancsharp>importusing StdEnvSystem;
using System.Collections.Generic;
 
namespace RosettaCode.BubbleSort
bsort :: *(a e) -> *(a e) | Array a e & < e
{
bsort array
public static class BubbleSortMethods
# (done, array) = sweep 1 True array
{
= if done array (bsort array)
//The "this" keyword before the method parameter identifies this as a C# extension
where
//method, which can be called using instance method syntax on any generic list,
sweep :: !Int !Bool !*(a e) -> (!Bool, !*(a e)) | Array a e & < e
//without having to modify the generic List<T> code provided by the .NET framework.
sweep i done array
public static void BubbleSort<T>(this List<T> list) where T : IComparable
| i >= size array = (done, array)
# (e1, array) = array![i - 1]{
(e2, array) =bool array![i]madeChanges;
int itemCount = list.Count;
| e1 > e2 = sweep (i + 1) False {array & [i - 1] = e2, [i] = e1}
= sweep (i + 1) done array</lang>do
{
Using it to sort an array of a hundred numbers:
madeChanges = false;
<lang clean>Start :: {Int}
itemCount--;
Start = bsort {x \\ x <- [100,99..1]}</lang>
for (int i = 0; i < itemCount; i++)
{
if (list[i].CompareTo(list[i + 1]) > 0)
{
T temp = list[i + 1];
list[i + 1] = list[i];
list[i] = temp;
madeChanges = true;
}
}
} while (madeChanges);
}
}
 
//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 Main()
{
List<int> testList = new List<int> { 3, 7, 3, 2, 1, -4, 10, 12, 4 };
testList.BubbleSort();
foreach (var t in testList) Console.Write(t + " ");
}
}
}</lang>
 
=={{header|Clojure}}==
Anonymous user