forked from VAR-solutions/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.cs
More file actions
52 lines (44 loc) · 1.22 KB
/
BubbleSort.cs
File metadata and controls
52 lines (44 loc) · 1.22 KB
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
/*
* Bubble Sort implementation in C#;
* Author : Lucas Madeira;
* Input : Array elements;
* Output : Sorted array elements;
*/
using System;
namespace BubbleSort
{
class Program
{
static void Main(string[] args)
{
int[] array = { 3, 2, 6, 7, 1, 5, 8, 4, 9, 0 };
Console.WriteLine("Before: ");
PrintArray(array);
BubbleSort(array);
Console.WriteLine("\n\nAfter: ");
PrintArray(array);
Console.WriteLine("\n\nPress any key to exit.");
Console.ReadKey();
}
public static void BubbleSort(int[] array)
{
for (int i = 0; i < array.Length - 1; i++)
{
for (int j = 0; j < array.Length - (i + 1); j++)
{
if (array[j] > array[j + 1])
{
int aux = array[j];
array[j] = array[j + 1];
array[j + 1] = aux;
}
}
}
}
public static void PrintArray(int[] array)
{
foreach (int value in array)
Console.Write(" {0}", value);
}
}
}