FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
competitive-programming/Sorting/InsertionSort.java at master · kothariji/competitive-programming · GitHub
kothariji
/
competitive-programming
Public
Notifications
You must be signed in to change notification settings
Fork
500
Star
704
Code
Issues
1
Pull requests
2
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
competitive-programming
/
Sorting
/
InsertionSort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
51 lines (46 loc) · 1.18 KB
Breadcrumbs
competitive-programming
/
Sorting
/
InsertionSort.java
Copy path
File metadata and controls
51 lines (46 loc) · 1.18 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
// Java Code to illustate Insertion sorting
import
java
.
util
.*;
import
java
.
io
.*;
public
class
InsertionSort
{
public
static
void
main
(
String
args
[]){
//Declarartion of required variables
int
size
;
Scanner
sc
=
new
Scanner
(
System
.
in
);
//Reading Input
System
.
out
.
println
(
"Input :
\n
"
);
System
.
out
.
println
(
"Enter the size of an array :
\n
"
);
size
=
sc
.
nextInt
();
sc
.
nextLine
();
int
arr
[]=
new
int
[
size
];
System
.
out
.
println
(
"Enter the elements of an array :
\n
"
);
for
(
int
index
=
0
;
index
<
size
;
index
++) {
arr
[
index
]=
sc
.
nextInt
();
}
for
(
int
index
=
1
;
index
<
size
;
index
++) {
int
temp
=
arr
[
index
];
int
j
=
index
-
1
;
while
(
j
>=
0
&&
arr
[
j
] >
temp
) {
//Swapping the elements, temp is used to store the temporary variable
arr
[
j
+
1
] =
arr
[
j
];
j
=
j
-
1
;
}
arr
[
j
+
1
]=
temp
;
}
//Displaying Output
System
.
out
.
println
(
"Output :
\n
"
);
System
.
out
.
println
(
"The sorted array is :
\n
"
);
for
(
int
index
=
0
;
index
<
size
;
index
++) {
System
.
out
.
print
(
arr
[
index
]+
" "
);
}
}
}
/*
Input:
Enter the size of an array: 4
Enter the elements of an array :
10 8 1 4
Output :
1 4 8 10
Time Complexity :O(n^2)
Space Complexity :O(1)
*/
Back
|
FazBrowse Home
|
New Git URL