-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSortAlgorithm.cs
More file actions
93 lines (79 loc) · 1.86 KB
/
Copy pathbubbleSortAlgorithm.cs
File metadata and controls
93 lines (79 loc) · 1.86 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
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
/*
Question:
------------
Implement bubble sort
Assumptions:
------------
string doesn't contain special characters
string is not alphanumeric
string is not empty
array length is fixed adn preset to 9
*/
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Practice
{
public class Program
{
public static void Main(string[] args)
{
Program example = new Program();
example.bubbleSort(example.getInput());
}
private int[] getInput()
{
int[] arr = new int[9];
int i = 0;
while(i < 9)
{
string str = Console.ReadLine();
if(str.Trim() != "0")
{
arr[i] = Convert.ToInt32(str);
i++;
}
}
return arr;
}
private void bubbleSort(int[] arr)
{
int k = 1;
for(int i = 0; i <= arr.Length - 1; i++)
{
if( (i+1 <= arr.Length - k) && (arr[i] > arr[i+1]))
swap(arr,i);
if(i == arr.Length - k && k <= arr.Length )
{
i = -1;
k++;
}
}
print(arr);
}
private void swap(int[] arr, int i)
{
int tmp = arr[i+1];
arr[i+1] = arr[i];
arr[i] = tmp;
}
private void print(int[] arr)
{
for(int i = 0 ; i < arr.Length ; i++)
Console.Write(arr[i] + " ");
Console.WriteLine("");
}
}
}
/*
Tests
-------
1. duplicate element
*/
/*
Observations
------------------
n(n-1)/2 time complexity
*/