-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubbleSorting.cs
52 lines (47 loc) · 1.32 KB
/
BubbleSorting.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
namespace Algorithms.Sorting
{
public static class BubbleSort
{
public static void BubbleSorting (ref int[] arr)
{
int i, j;
int length = arr.Length - 1;
for (i = 0; i < length; i++)
{
for (j = 0; j < length - i; j++)
{
if (arr[j] > arr[j + 1])
{
Swap (ref arr[j], ref arr[j + 1]);
}
}
}
}
public static void BubbleSorting_Optimized (ref int[] arr)
{
int i, j;
int length = arr.Length - 1;
bool swapped;
for (i = 0; i < length; i++)
{
swapped = false;
for (j = 0; j < length - i; j++)
{
if (arr[j] > arr[j + 1])
{
Swap (ref arr[j], ref arr[j + 1]);
swapped = true;
}
}
if (!swapped) break;
}
}
public static void Swap (ref int first, ref int second)
{
int temp = first;
first = second;
second = temp;
}
}
}