FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SortExtravaganzaCSharp/HeapSort/Program.cs at master · exceptionnotfound/SortExtravaganzaCSharp · GitHub
exceptionnotfound
/
SortExtravaganzaCSharp
Public
Notifications
You must be signed in to change notification settings
Fork
15
Star
70
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
SortExtravaganzaCSharp
/
HeapSort
/
Program.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
68 lines (62 loc) · 2.15 KB
Breadcrumbs
SortExtravaganzaCSharp
/
HeapSort
/
Program.cs
Copy path
File metadata and controls
68 lines (62 loc) · 2.15 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
using
SortExtravaganza
.
Common
;
using
System
;
namespace
HeapSortDemo
{
//HeapSort takes advantage of a heap data structure to sort an unsorted list.
//It can be thought of as an improved version of selection sort.
//ALGORITHM:
//1. Build a "max heap" out of the unsorted data (a heap with the largest value as the first node).
//2. Swap the first element of the heap with the final element. That element (now at final position) is considered sorted.
// In effect, this makes the largest element the last one in the considered range.
//3. Decrease the range of considered elements (those still needing to be sorted) by 1.
//4. Continue until the considered range of elements is 1.
public
class
HeapSort
{
static
void
Sort
(
int
[
]
array
)
{
var
length
=
array
.
Length
;
for
(
int
i
=
length
/
2
-
1
;
i
>=
0
;
i
--
)
{
Heapify
(
array
,
length
,
i
)
;
}
for
(
int
i
=
length
-
1
;
i
>=
0
;
i
--
)
{
int
temp
=
array
[
0
]
;
array
[
0
]
=
array
[
i
]
;
array
[
i
]
=
temp
;
Heapify
(
array
,
i
,
0
)
;
}
}
//Rebuilds the heap
static
void
Heapify
(
int
[
]
array
,
int
length
,
int
i
)
{
int
largest
=
i
;
int
left
=
2
*
i
+
1
;
int
right
=
2
*
i
+
2
;
if
(
left
<
length
&&
array
[
left
]
>
array
[
largest
]
)
{
largest
=
left
;
}
if
(
right
<
length
&&
array
[
right
]
>
array
[
largest
]
)
{
largest
=
right
;
}
if
(
largest
!=
i
)
{
int
swap
=
array
[
i
]
;
array
[
i
]
=
array
[
largest
]
;
array
[
largest
]
=
swap
;
Heapify
(
array
,
length
,
largest
)
;
}
}
public
static
void
Main
(
)
{
int
[
]
arr
=
{
74
,
19
,
24
,
5
,
8
,
79
,
42
,
15
,
20
,
53
,
11
}
;
Console
.
WriteLine
(
"Heap Sort"
)
;
CommonFunctions
.
PrintInitial
(
arr
)
;
Sort
(
arr
)
;
CommonFunctions
.
PrintFinal
(
arr
)
;
Console
.
ReadKey
(
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL