FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Basic-Sorting-Algorithms/SelectionSort.java at main · Shivansh-Rajput01/Basic-Sorting-Algorithms · GitHub
Shivansh-Rajput01
/
Basic-Sorting-Algorithms
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
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
Basic-Sorting-Algorithms
/
SelectionSort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
41 lines (37 loc) · 1.1 KB
Breadcrumbs
Basic-Sorting-Algorithms
/
SelectionSort.java
Copy path
File metadata and controls
41 lines (37 loc) · 1.1 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
class
SelectionSort
{
static
void
selectionSort
(
int
arr
[]){
for
(
int
i
=
0
;
i
<
arr
.
length
-
1
;
i
++){
int
minPos
=
i
;
boolean
swaps
=
false
;
for
(
int
j
=
i
+
1
;
j
<
arr
.
length
;
j
++){
if
(
arr
[
j
] <
arr
[
minPos
]){
minPos
=
j
;
}
}
//swapping minimum element to starting index of array
if
(
minPos
!=
i
){
int
temp
=
arr
[
minPos
];
arr
[
minPos
] =
arr
[
i
];
arr
[
i
] =
temp
;
swaps
=
true
;
}
//checking if no swaps happens in 1st pass
if
(!
swaps
){
break
;
}
}
}
public
static
void
main
(
String
[]
args
) {
int
arr
[] = {
5
,
3
,
4
,
2
,
1
};
System
.
out
.
println
(
"Array before sorting : "
);
for
(
int
val
:
arr
){
System
.
out
.
print
(
val
+
" "
);
}
System
.
out
.
println
();
selectionSort
(
arr
);
System
.
out
.
println
(
"Array aftr sorting : "
);
for
(
int
val
:
arr
){
System
.
out
.
print
(
val
+
" "
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL