-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubbleSort.cs
58 lines (50 loc) · 1.64 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BUBBLESORT
{
class Program
{
static void BubbleSort(int[] intArray)
{
int temp = 0;
bool swapped;
for (int i = 0; i < intArray.Length; i++)
{
swapped = false;
for (int j = 0; j < intArray.Length - 1 - i; j++)
if (intArray[j] > intArray[j + 1])
{
temp = intArray[j];
intArray[j] = intArray[j + 1];
intArray[j + 1] = temp;
if (!swapped)
swapped = true;
}
if (!swapped)
return;
}
}
static void Main(string[] args)
{
Console.Write("Please input numbers: \n");
string input_str;
input_str = Console.ReadLine();
Console.WriteLine("-----After Sorting-----");
string[] input_str_arr = input_str.Split(' ');
int input_len = input_str_arr.Length;
int[] input_int_arr = Array.ConvertAll(input_str_arr, s => int.Parse(s));
// example of parsing string of one number
// int x = Int32.Parse(TextBoxD1.Text);
// Sorting Algorithms
BubbleSort(input_int_arr);
for (int i = 0; i < input_len; i++)
{
Console.Write(input_int_arr[i] + " ");
}
Console.ReadLine();
}
}
}