FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java_Fundamentals/SelectionSortCode.java at main · devdeepak06/Java_Fundamentals · GitHub
devdeepak06
/
Java_Fundamentals
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
Java_Fundamentals
/
SelectionSortCode.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (43 loc) · 1.53 KB
Breadcrumbs
Java_Fundamentals
/
SelectionSortCode.java
Copy path
File metadata and controls
43 lines (43 loc) · 1.53 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
import
java
.
util
.
Scanner
;
public
class
SelectionSortCode
{
public
static
void
printArray
(
int
[]
arr
) {
for
(
int
i
=
0
;
i
<
arr
.
length
;
i
++) {
System
.
out
.
print
(
arr
[
i
] +
" "
);
}
System
.
out
.
println
();
}
public
static
void
selectionSort
(
int
[]
arr
) {
for
(
int
i
=
0
;
i
<
arr
.
length
-
1
;
i
++) {
int
min
=
Integer
.
MAX_VALUE
;
int
minIndex
= -
1
;
for
(
int
j
=
i
;
j
<
arr
.
length
;
j
++) {
if
(
arr
[
j
] <
min
) {
min
=
arr
[
j
];
minIndex
=
j
;
}
}
int
temp
=
arr
[
minIndex
];
arr
[
minIndex
] =
arr
[
i
];
arr
[
i
] =
temp
;
}
}
public
static
void
main
(
String
[]
args
) {
try
(
Scanner
s
=
new
Scanner
(
System
.
in
)) {
System
.
out
.
println
(
"Enter the number of test cases"
);
int
t
=
s
.
nextInt
();
while
(
t
>
0
) {
System
.
out
.
println
(
"Enter the size of the array"
);
int
n
=
s
.
nextInt
();
System
.
out
.
println
(
"Enter the elements of the array"
);
int
[]
arr
=
new
int
[
n
];
for
(
int
i
=
0
;
i
<
n
;
i
++) {
arr
[
i
] =
s
.
nextInt
();
}
selectionSort
(
arr
);
// Call the selectionSort method here to sort the array arr in ascending order
System
.
out
.
println
(
"The sorted array is"
);
printArray
(
arr
);
t
--;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL