-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubblesort_array.cs
90 lines (75 loc) · 2.01 KB
/
bubblesort_array.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
using System;
// To execute C#, please define "static void Main" on a class
// named Solution.
class Solution
{
static void Main(string[] args)
{
int[] arr = new int[4]{40,30,20,10};
//SelectionSort1(arr);
BubbleSort(arr);
for(int i=0; i<arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
}
//big O(n^2), best case O(n)--> array is already sorted
public static void BubbleSortEff(int[] arr)
{
int length = arr.Length;
for(int i=0; i< length; i++)
{
bool swapped = false;
for(int j = 0; j <length -i-1; j++)
{
if(arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] =temp;
swapped = true;
}
}
if(swapped == false)
break;
}
}
//big O(n^2)
public static void BubbleSort(int[] arr)
{
int length = arr.Length;
for(int i=0; i< length; i++)
{
for(int j=0; j < length-1-i; j++)
{
if(arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j +1] = temp;
}
}
}
}
//big O(n^2), on average it's O(n)
public static void SelectionSort(int[] arr)
{
int length = arr.Length;
int min_idx = 0;
for(int i=0; i<length -1; i++)
{
min_idx = i;
for(int j = i+1; j<length; j++)
{
if(arr[j] < arr[min_idx])
{
min_idx = j;
}
}
//swap
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
}