-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubbleSort.cs
101 lines (96 loc) · 4.13 KB
/
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
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
namespace BubbleSort
{
class Program
{
// The Main entry point of the Application
static void Main(string[] args)
{
// Creates an Integer Array with 12 Elements
int[] arr = { 2, 5, 2, 235, 2, 35, 2, 62, 51, 4, 2124, 2 };
// Prints the Array before Sorting to the Console
// to better view changes between sorted and unsorted
Console.WriteLine("Array before Sorting: [{0}]", string.Join(", ", arr));
// Calls the bubbleSort method and sorts the passed array
BubbleSortOptimized(arr);
// Prints the Sorted Array to the Console where you can
// check if the array is sorted
Console.WriteLine("Array after Sorting: [{0}]", string.Join(", ", arr));
}
/// <summary>
/// Bubble Sort repeatedly steps through the list,
/// compares adjacent elements and swaps them if they are in the wrong order
/// </summary>
/// <param name="arr">The Array to Sort</param>
public static void BubbleSort(int[] arr)
{
// a boolean that shows if the Array is sorted or not
bool IsSorted = false;
// Sorts the Array until isSorted is true what means that
// the Array is sorted
while (!IsSorted)
{
// Sets isSorted to true (even if the array is not sorted)
// to escape from the loop when it is sorted
IsSorted = true;
// loops through the Array and compares adjacent elements
for (int i = 0; i < arr.Length - 1; i++)
{
// Checks if the current Array Element is Bigger than the next
// Array Element.
// if you want the Array from Biggest to Smallest just change
// the > to an <.
if (arr[i] > arr[i + 1])
{
// swaps the Current Array Element and the
// next Array Element because ther are out of order
Swap(arr, i, i + 1);
// sets isSorted to false because the Array is possibly not sorted
IsSorted = false;
}
}
}
}
/// <summary>
/// Bubble Sort repeatedly steps through the list,
/// compares adjacent elements and swaps them if they are in the wrong order
/// Ignores all Sorted Elements
/// </summary>
/// <param name="arr">The Array to Sort</param>
public static void BubbleSortOptimized(int[] arr)
{
// loops through the Array and splits sorted and unsorted Array
for (int i = arr.Length - 1; i != 0; i--)
{
// loops through the unsorted Array to sort it
for (int j = 0; j < i; j++)
{
// Checks if the current Array Element is Bigger than the next
// Array Element.
// if you want the Array from Biggest to Smallest just change
// the > to an <.
if (arr[j] > arr[j + 1])
{
// swaps the Current Array Element and the
// next Array Element because ther are out of order
Swap(arr, j, j + 1);
}
}
}
}
/// <summary>Swaps to Array Elements</summary>
/// <param name="arr">The Array where to swap Elements</param>
/// <param name="a">Index of the first Element</param>
/// <param name="b">Index of the second Element</param>
// Swaps two Elements in a given Array
public static void Swap(int[] arr, int a, int b)
{
// Stores a's value in t
int t = arr[a];
// Sets the Element at a's position to the value in b's position
arr[a] = arr[b];
// Sets the Element at a's position to the value of t
arr[b] = t;
}
}
}