-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubbleSort.cs
33 lines (32 loc) · 903 Bytes
/
BubbleSort.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
using System;
namespace SortingAlgorithms
{
static partial class Program
{
static void BubbleSort(int[] arr)
{
bool swapped;
for (int i = 0; i < arr.Length - 1; i++)
{
swapped = false;
for (int j = 0; j < arr.Length - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
if (swapped == false)
break;
}
Console.WriteLine("\nArray after bubble sort:");
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + "\t");
}
}
}
}