Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
root-devvoo
Java-Data_Structure
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
Java-Data_Structure
/
Chapter1
/
src
/
Section2
/
Code18.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
37 lines (34 loc) · 1.58 KB
main
Breadcrumbs
Java-Data_Structure
/
Chapter1
/
src
/
Section2
/
Code18.java
Copy path
Top
File metadata and controls
Code
Blame
37 lines (34 loc) · 1.58 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
package
Section2
;
import
java
.
util
.
Scanner
;
// 사용자로부터 n개의 정수를 입력받은 후 오름차순으로 정렬(sort)하여 출력하는 코드이다.
// (버블정렬)
public
class
Code18
{
public
static
void
main
(
String
[]
args
) {
Scanner
sc
=
new
Scanner
(
System
.
in
);
int
n
=
sc
.
nextInt
();
int
[]
data
=
new
int
[
n
];
for
(
int
i
=
0
;
i
<
n
;
i
++)
data
[
i
] =
sc
.
nextInt
();
sc
.
close
();
// 1. 메서드 bubbleSort를 호출하면서 매개변수로 배열 data와 n의 값을 건낸다.
bubbleSort
(
n
,
data
);
// 배열에 n개의 데이터가 저장되어있는데, 그 배열을 통째로 (매개변수로) 넘겨줘야 정렬을 할 수 있다.
// 이렇게 배열을 넘겨주면 된다. 그리고, 데이터의 개수 n도 함께 넘겨준다.
System
.
out
.
println
(
"Sorted data: "
);
for
(
int
i
=
0
;
i
<
n
;
i
++)
System
.
out
.
println
(
data
[
i
]);
}
// 2. 메서드 bubbleSort는 매개변수로 하나의 배열과 하나의 정수를 건내 받으며 각각을 data와 n이라고 이름 짓는다.
static
void
bubbleSort
(
int
n
,
int
[]
data
) {
// 얘는 자기가 받은 배열 안에 있는 데이터들을 정렬하는 것이 임무이다. 다른 값을 리턴해줄 필요는 없으니 void
for
(
int
i
=
n
-
1
;
i
>
0
;
i
--) {
for
(
int
j
=
0
;
j
<
i
;
j
++) {
if
(
data
[
j
] >
data
[
j
+
1
]) {
// swap data[j] and data[j+1]
int
tmp
=
data
[
j
];
data
[
j
] =
data
[
j
+
1
];
data
[
j
+
1
] =
tmp
;
// 3. 메서드 bubbleSort는 아무 것도 return하지 않는다. 하지만 이 메서드가 한 일은 배열 data에 이미 반영되어 있다.
}
}
}
}
}
You can’t perform that action at this time.