FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java_Fundamentals/AllSortingAlgorithms.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
/
AllSortingAlgorithms.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
167 lines (165 loc) · 5.09 KB
Breadcrumbs
Java_Fundamentals
/
AllSortingAlgorithms.java
Copy path
File metadata and controls
167 lines (165 loc) · 5.09 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import
java
.
util
.
Scanner
;
public
class
AllSortingAlgorithms
{
//Selection Sort
public
static
void
selectionSort
(
int
[]
arr
) {
int
n
=
arr
.
length
;
for
(
int
i
=
0
;
i
<
n
-
1
;
i
++) {
int
min
=
i
;
for
(
int
j
=
i
+
1
;
j
<
n
;
j
++) {
if
(
arr
[
j
] <
arr
[
min
]) {
min
=
j
;
}
}
int
temp
=
arr
[
min
];
arr
[
min
] =
arr
[
i
];
arr
[
i
] =
temp
;
}
}
//Bubble Sort
public
static
void
bubbleSort
(
int
[]
arr
) {
int
n
=
arr
.
length
;
for
(
int
i
=
0
;
i
<
n
-
1
;
i
++) {
for
(
int
j
=
0
;
j
<
n
-
i
-
1
;
j
++) {
if
(
arr
[
j
] >
arr
[
j
+
1
]) {
int
temp
=
arr
[
j
];
arr
[
j
] =
arr
[
j
+
1
];
arr
[
j
+
1
] =
temp
;
}
}
}
}
//Insertion Sort
public
static
void
insertionSort
(
int
[]
arr
) {
int
n
=
arr
.
length
;
for
(
int
i
=
1
;
i
<
n
;
i
++) {
int
key
=
arr
[
i
];
int
j
=
i
-
1
;
while
(
j
>=
0
&&
arr
[
j
] >
key
) {
arr
[
j
+
1
] =
arr
[
j
];
j
--;
}
arr
[
j
+
1
] =
key
;
}
}
//Merge Sort
public
static
void
merge
(
int
[]
arr
,
int
l
,
int
m
,
int
r
) {
int
n1
=
m
-
l
+
1
;
int
n2
=
r
-
m
;
int
[]
L
=
new
int
[
n1
];
int
[]
R
=
new
int
[
n2
];
for
(
int
i
=
0
;
i
<
n1
;
i
++) {
L
[
i
] =
arr
[
l
+
i
];
}
for
(
int
j
=
0
;
j
<
n2
;
j
++) {
R
[
j
] =
arr
[
m
+
1
+
j
];
}
int
i
=
0
,
j
=
0
;
int
k
=
l
;
while
(
i
<
n1
&&
j
<
n2
) {
if
(
L
[
i
] <=
R
[
j
]) {
arr
[
k
] =
L
[
i
];
i
++;
}
else
{
arr
[
k
] =
R
[
j
];
j
++;
}
k
++;
}
while
(
i
<
n1
) {
arr
[
k
] =
L
[
i
];
i
++;
k
++;
}
while
(
j
<
n2
) {
arr
[
k
] =
R
[
j
];
j
++;
k
++;
}
}
public
static
void
mergeSort
(
int
[]
arr
,
int
l
,
int
r
) {
if
(
l
<
r
) {
int
m
= (
l
+
r
) /
2
;
mergeSort
(
arr
,
l
,
m
);
mergeSort
(
arr
,
m
+
1
,
r
);
merge
(
arr
,
l
,
m
,
r
);
}
}
//Quick Sort
public
static
int
partition
(
int
[]
arr
,
int
low
,
int
high
) {
int
pivot
=
arr
[
high
];
int
i
=
low
-
1
;
for
(
int
j
=
low
;
j
<
high
;
j
++) {
if
(
arr
[
j
] <
pivot
) {
i
++;
int
temp
=
arr
[
i
];
arr
[
i
] =
arr
[
j
];
arr
[
j
] =
temp
;
}
}
int
temp
=
arr
[
i
+
1
];
arr
[
i
+
1
] =
arr
[
high
];
arr
[
high
] =
temp
;
return
i
+
1
;
}
public
static
void
quickSort
(
int
[]
arr
,
int
low
,
int
high
) {
if
(
low
<
high
) {
int
pi
=
partition
(
arr
,
low
,
high
);
quickSort
(
arr
,
low
,
pi
-
1
);
quickSort
(
arr
,
pi
+
1
,
high
);
}
}
// Print Array
public
static
void
printArray
(
int
[]
arr
) {
int
n
=
arr
.
length
;
for
(
int
i
=
0
;
i
<
n
;
i
++) {
System
.
out
.
print
(
arr
[
i
] +
" "
);
}
System
.
out
.
println
();
}
// Main Method
public
static
void
main
(
String
[]
args
) {
try
(
Scanner
sc
=
new
Scanner
(
System
.
in
)) {
System
.
out
.
println
(
"Enter the number of test cases"
);
int
t
=
sc
.
nextInt
();
while
(
t
>
0
) {
System
.
out
.
println
(
"Enter the size of the array"
);
int
n
=
sc
.
nextInt
();
System
.
out
.
println
(
"Enter the elements of the array"
);
int
[]
arr
=
new
int
[
n
];
for
(
int
i
=
0
;
i
<
n
;
i
++) {
arr
[
i
] =
sc
.
nextInt
();
}
System
.
out
.
println
(
"Enter the sorting algorithm to be used"
);
System
.
out
.
println
(
"1. Selection Sort"
);
System
.
out
.
println
(
"2. Bubble Sort"
);
System
.
out
.
println
(
"3. Insertion Sort"
);
System
.
out
.
println
(
"4. Merge Sort"
);
System
.
out
.
println
(
"5. Quick Sort"
);
int
choice
=
sc
.
nextInt
();
switch
(
choice
) {
case
1
:
selectionSort
(
arr
);
break
;
case
2
:
bubbleSort
(
arr
);
break
;
case
3
:
insertionSort
(
arr
);
break
;
case
4
:
mergeSort
(
arr
,
0
,
n
-
1
);
break
;
case
5
:
quickSort
(
arr
,
0
,
n
-
1
);
break
;
default
:
System
.
out
.
println
(
"Invalid choice"
);
break
;
}
System
.
out
.
println
(
"The sorted array is"
);
printArray
(
arr
);
t
--;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL