-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
62 lines (52 loc) · 1.42 KB
/
Program.cs
File metadata and controls
62 lines (52 loc) · 1.42 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
56
57
58
59
60
61
62
/*
Author: Moksha Chowdary Guntupalli
Date: 2/28/2024
Comments: This C# Console Application code demonstrates the
Bubble Sort Algorithm. No input
is required for the script. It will execute without
input against two pre-populated arrays.
*/
using System;
class Program
{
static void Main()
{
int[] array = { 3, 62, 5, 16, 4, 10 };
Console.WriteLine("Original Array:");
PrintArray(array);
BubbleSort(array);
Console.WriteLine("\nSorted Array:");
PrintArray(array);
}
static void BubbleSort(int[] arr)
{
int n = arr.Length;
bool swapped;
do
{
swapped = false;
for (int i = 1; i < n; i++)
{
if (arr[i - 1] > arr[i])
{
// Swap arr[i-1] and arr[i]
int temp = arr[i - 1];
arr[i - 1] = arr[i];
arr[i] = temp;
swapped = true;
}
}
// After each pass, the largest element is guaranteed to be at the end,
// so we can reduce the size of the array to be considered in the next pass.
n--;
} while (swapped);
}
static void PrintArray(int[] arr)
{
foreach (var element in arr)
{
Console.Write(element + " ");
}
Console.WriteLine();
}
}