-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSortAlgorithm.cs
More file actions
55 lines (47 loc) · 1.53 KB
/
BubbleSortAlgorithm.cs
File metadata and controls
55 lines (47 loc) · 1.53 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
53
54
55
using System;
namespace BubbleSortAlgorithm
{
class BubbleSortAlgorithm
{
static void BubbleSort(int[] arr)
{
for(int i = 0; i < arr.Length - 1; i++)
{
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;
}
}
}
}
static void Main(string[] args)
{
Console.Write("Enter the lenght of your array: ");
int n = int.Parse(Console.ReadLine());
// Create an array of the given length
int[] arr = new int[n];
Console.WriteLine("Enter the corresponding values of the array: ");
for (int i = 0; i < n; i++)
{
Console.Write("Array[{0}] = ", i);
arr[i] = int.Parse(Console.ReadLine());
}
// Sort the array
Console.WriteLine(DateTime.Now);
BubbleSort(arr);
// Print the sorted array
Console.WriteLine("Sorted arrays");
for(int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i] + " ");
}
Console.WriteLine();
Console.WriteLine(DateTime.Now);
Console.Read();
}
}
}