Skip to content

Commit 674bc14

Browse files
authored
feat(7-kyu): kata/bubblesort-once (#456)
* feat(7-kyu): kata/bubblesort-once * test: cover already sorted array case
1 parent b54d97f commit 674bc14

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed

docs/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Codewars Handbook ☕️🚀
22

33
[![Views statistics +1 👀](https://img.shields.io/badge/dynamic/xml?color=success&label=views&query=//*[name()=%27text%27][3]&url=https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2FParanoidUser%2Fcodewars-handbook)](https://hits.seeyoufarm.com/api/count/graph/dailyhits.svg?url=https://github.com/ParanoidUser/codewars-handbook)
4-
[![Solved kata 👌](https://img.shields.io/badge/solved%20kata-1374-red.svg)](https://www.codewars.com/kata/search/java)
4+
[![Solved kata 👌](https://img.shields.io/badge/solved%20kata-1375-red.svg)](https://www.codewars.com/kata/search/java)
55
[![CI pipeline 🛠](https://img.shields.io/github/actions/workflow/status/ParanoidUser/codewars-handbook/build.yml?branch=main)](https://github.com/ParanoidUser/codewars-handbook/actions/workflows/build.yml)
66
[![Quality gate 🔎](https://img.shields.io/sonar/alert_status/codewars-handbook?server=https%3A%2F%2Fsonarcloud.io)](https://sonarcloud.io/dashboard?id=codewars-handbook)
77
[![Let's have a chat! 📞](https://img.shields.io/gitter/room/ParanoidUser/codewars-handbook?color=49c39e)](https://gitter.im/ParanoidUser/codewars-handbook)
@@ -25,7 +25,7 @@ slug.
2525

2626
| [1 kyu](/kata/1-kyu/index.md) | [2 kyu](/kata/2-kyu/index.md) | [3 kyu](/kata/3-kyu/index.md) | [4 kyu](/kata/4-kyu/index.md) | [5 kyu](/kata/5-kyu/index.md) | [6 kyu](/kata/6-kyu/index.md) | [7 kyu](/kata/7-kyu/index.md) | [8 kyu](/kata/8-kyu/index.md) | [beta](/kata/beta/index.md) | [retired](/kata/retired/index.md) |
2727
|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:---------------------------:|:---------------------------------:|
28-
| - | 1 | 2 | 22 | 40 | 413 | 556 | 205 | 58 | 76 |
28+
| - | 1 | 2 | 22 | 40 | 413 | 557 | 205 | 58 | 76 |
2929

3030
**Note:** The source code is written in Java 17 and may use language features that are incompatible
3131
with Java 8, 11.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# [Bubblesort Once](https://www.codewars.com/kata/bubblesort-once "https://www.codewars.com/kata/56b97b776ffcea598a0006f2")
2+
3+
## Overview
4+
5+
[Bubblesort](https://en.wikipedia.org/wiki/Bubble_sort) is an inefficient sorting algorithm that is
6+
simple to understand and therefore often taught in introductory computer science courses as an
7+
example how _not_ to sort a list. Nevertheless, it is correct in the sense that it eventually
8+
produces a sorted version of the original list when executed to completion.
9+
10+
At the heart of Bubblesort is what is known as a _pass_. Let's look at an example at how a pass
11+
works.
12+
13+
Consider the following list:
14+
15+
```
16+
9, 7, 5, 3, 1, 2, 4, 6, 8
17+
```
18+
19+
We initiate a pass by comparing the first two elements of the list. Is the first element greater
20+
than the second? If so, we swap the two elements. Since `9` is greater than `7` in this case, we
21+
swap them to give `7, 9`. The list then becomes:
22+
23+
```
24+
7, 9, 5, 3, 1, 2, 4, 6, 8
25+
```
26+
27+
We then continue the process for the 2nd and 3rd elements, 3rd and 4th elements ... all the way up
28+
to the last two elements. When the pass is complete, our list becomes:
29+
30+
```
31+
7, 5, 3, 1, 2, 4, 6, 8, 9
32+
```
33+
34+
Notice that the largest value `9` "bubbled up" to the end of the list. This is precisely how
35+
Bubblesort got its name.
36+
37+
## Task
38+
39+
Given an array of integers, your function `bubblesortOnce`/`bubblesort_once`/`BubblesortOnce` (or
40+
equivalent, depending on your language's naming conventions) should return a *new* array equivalent
41+
to performing exactly **1 complete pass** on the original array. Your function should be pure, i.e.
42+
it should **not** mutate the input array.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
interface BubblesortOnce {
2+
static int[] bubbleSortOnce(int[] array) {
3+
int[] copy = array.clone();
4+
for (int i = 0; i < copy.length - 1; i++) {
5+
if (copy[i] > copy[i + 1]) {
6+
copy[i] += copy[i + 1];
7+
copy[i + 1] = copy[i] - copy[i + 1];
8+
copy[i] -= copy[i + 1];
9+
}
10+
}
11+
return copy;
12+
}
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import org.junit.jupiter.api.Test;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
class SolutionTest {
6+
@Test
7+
void sample() {
8+
assertArrayEquals(new int[]{7, 5, 3, 1, 2, 4, 6, 8, 9}, BubblesortOnce.bubbleSortOnce(new int[]{9, 7, 5, 3, 1, 2, 4, 6, 8}));
9+
assertArrayEquals(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, BubblesortOnce.bubbleSortOnce(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}));
10+
}
11+
}

kata/7-kyu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
- [Bits Battle](bits-battle)
5959
- [Breaking chocolate problem](breaking-chocolate-problem)
6060
- [Broken sequence](broken-sequence)
61+
- [Bubblesort Once](bubblesort-once)
6162
- [Build a square](build-a-square)
6263
- [Building blocks](building-blocks)
6364
- [Bulls and Cows](bulls-and-cows)

0 commit comments

Comments
 (0)