Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
DSA_java
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Antony Nixen Raj
DSA_java
Commits
3afa1b6a
Commit
3afa1b6a
authored
6 days ago
by
Antony Nixen Raj
Browse files
Options
Downloads
Patches
Plain Diff
bubble sort
parent
f6df805c
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
bubble_sort.java
+41
-0
41 additions, 0 deletions
bubble_sort.java
with
41 additions
and
0 deletions
bubble_sort.java
0 → 100644
+
41
−
0
View file @
3afa1b6a
public
class
bubble_sort
{
public
static
void
bubbleSort
(
int
[]
arr
)
{
int
n
=
arr
.
length
;
// Traverse through all array elements
for
(
int
i
=
0
;
i
<
n
-
1
;
i
++)
{
// Flag to check if swapping occurred
boolean
swapped
=
false
;
// Last i elements are already sorted
for
(
int
j
=
0
;
j
<
n
-
i
-
1
;
j
++)
{
// Swap if the element is greater than the next
if
(
arr
[
j
]
>
arr
[
j
+
1
])
{
// Swap arr[j] and arr[j + 1]
int
temp
=
arr
[
j
];
arr
[
j
]
=
arr
[
j
+
1
];
arr
[
j
+
1
]
=
temp
;
swapped
=
true
;
}
}
// If no two elements were swapped, break
if
(!
swapped
)
break
;
}
}
public
static
void
main
(
String
[]
args
)
{
int
[]
data
=
{
64
,
34
,
25
,
12
,
22
,
11
,
90
};
System
.
out
.
println
(
"Original array:"
);
for
(
int
num
:
data
)
{
System
.
out
.
print
(
num
+
" "
);
}
// Perform bubble sort
bubbleSort
(
data
);
System
.
out
.
println
(
"\nSorted array:"
);
for
(
int
num
:
data
)
{
System
.
out
.
print
(
num
+
" "
);
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment