sharp]
view plain
copy
print
?
- class BubbleSortAlgorithm
- {
- public void BubbleSort(int[] array)
- {
- bool exchanged = false; // to check whether the array is sorted.
- for (int i = 1; i < array.Length; i++)
- {
- for (int j = array.Length - 1; j >= i; j--)
- {
- if (array[j] < array[j - 1])
- {
- exchanged = true;
- int temp = array[j];
- array[j] = array[j - 1];
- array[j - 1] = temp;
- }
- }
- if (exchanged == false)
- {
- return;
- }
- }
- }
- }