FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Data-Structures-Algorithms/Sorting/SelectionSortAlt.cpp at master · pratik-a/Data-Structures-Algorithms · GitHub
pratik-a
/
Data-Structures-Algorithms
Public
forked from
CodersForLife/Data-Structures-Algorithms
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Data-Structures-Algorithms
/
Sorting
/
SelectionSortAlt.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
47 lines (35 loc) · 950 Bytes
Breadcrumbs
Data-Structures-Algorithms
/
Sorting
/
SelectionSortAlt.cpp
Copy path
File metadata and controls
47 lines (35 loc) · 950 Bytes
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
//
A fairly verbose implementation of selection sort
#
include
<
iostream
>
#
include
<
algorithm
>
#
include
<
functional
>
inline
bool
ascending
(
int
x,
int
y)
{
return
x > y;
}
inline
bool
descending
(
int
x,
int
y)
{
return
x < y;
}
void
selection_sort
(
int
*array, std::
size_t
size, std::function<
bool
(
int
,
int
)> direction)
{
for
(std::
size_t
current_index =
0
; current_index < size; ++current_index)
{
//
The element we want to swap
int
preferred_index = current_index;
for
(std::
size_t
next_index = current_index +
1
; next_index < size; ++next_index)
{
if
(
direction
(array[preferred_index], array[next_index]))
preferred_index = next_index;
}
std::swap
(array[current_index], array[preferred_index]);
}
}
int
main
()
{
const
std::
size_t
size =
5
;
int
array[size] = {
8
,
5
,
4
,
2
,
1
};
selection_sort
(array, size, ascending);
for
(std::
size_t
i =
0
; i < size; ++i)
std::cout << array[i] <<
'
\n
'
;
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL