Skip to content

Commit 8f2a21c

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Sorting: Bubble Sort. Solved ✅.
1 parent 06df6ec commit 8f2a21c

File tree

4 files changed

+216
-0
lines changed

4 files changed

+216
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# [Sorting: Bubble Sort](https://www.hackerrank.com/challenges/ctci-bubble-sort/)
2+
3+
Find the minimum number of conditional checks taking place in Bubble Sort
4+
5+
- Difficulty: `#easy`
6+
- Category: `#ProblemSolvingBasic` `#sorting`
7+
8+
Consider the following version of Bubble Sort:
9+
10+
```c
11+
for (int i = 0; i < n; i++) {
12+
13+
for (int j = 0; j < n - 1; j++) {
14+
// Swap adjacent elements if they are in decreasing order
15+
if (a[j] > a[j + 1]) {
16+
swap(a[j], a[j + 1]);
17+
}
18+
}
19+
20+
}
21+
```
22+
23+
Given an array of integers, sort the array in ascending order
24+
using the Bubble Sort algorithm above. Once sorted, print the following three lines:
25+
26+
1. `Array is sorted in numSwaps swaps.`, where *`numSums`* is
27+
the number of swaps that took place.
28+
2. `First Element: firstElement`, where *`firstElement`* is
29+
the first element in the sorted array.
30+
3. `Last Element: lastElement`, where *`lastElement`* is
31+
the last element in the sorted array.
32+
33+
**Hint**: To complete this challenge, you must add a variable
34+
that keeps a running tally of all swaps that occur during execution.
35+
36+
## Example
37+
38+
```text
39+
swap a
40+
0 [6,4,1]
41+
1 [4,6,1]
42+
2 [4,1,6]
43+
3 [1,4,6]
44+
```
45+
46+
The steps of the bubble sort are shown above. It took `3` swaps to sort the array.
47+
Output is:
48+
49+
```text
50+
Array is sorted in 3 swaps.
51+
First Element: 1
52+
Last Element: 6
53+
```
54+
55+
## Function Description
56+
57+
Complete the function countSwaps in the editor below.
58+
59+
countSwaps has the following parameter(s):
60+
61+
- `int a[n]`: an array of integers to sort
62+
63+
## Prints
64+
65+
Print the three lines required, then return. No return value is expected.
66+
67+
## Input Format
68+
69+
The first line contains an integer, `n`, the size of the array `a`.
70+
The second line contains `n` space-separated integers `a[i]`.
71+
72+
## Constraints
73+
74+
- $ 2 \leq n \leq 600 $
75+
- $ a \leq a[i] \leq 2 \times 10^6 $
76+
77+
## Output Format
78+
79+
## Sample Input 0
80+
81+
```text
82+
STDIN Function
83+
----- --------
84+
3 a[] size n = 3
85+
1 2 3 a = [1, 2, 3]
86+
```
87+
88+
## Sample Output 0
89+
90+
```text
91+
Array is sorted in 0 swaps.
92+
First Element: 1
93+
Last Element: 3
94+
Explanation 0
95+
```
96+
97+
The array is already sorted, so `0` swaps take place.
98+
99+
## Sample Input 1
100+
101+
```text
102+
3
103+
3 2 1
104+
```
105+
106+
## Sample Output 1
107+
108+
```text
109+
Array is sorted in 3 swaps.
110+
First Element: 1
111+
Last Element: 3
112+
```
113+
114+
## Explanation 1
115+
116+
The array is not sorted, and its initial values are: `{1, 2, 3}`.
117+
The following `3` swaps take place:
118+
119+
```text
120+
{3, 2, 1} -> {2, 3, 1}
121+
{2, 3, 1} -> {2, 1, 3}
122+
{2, 1, 3} -> {1, 2, 3}
123+
```
124+
125+
At this point the array is sorted and the three lines of output are printed to stdout.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
3+
import { countSwaps, SortableGroup } from './ctci_bubble_sort';
4+
import TEST_CASES from './ctci_bubble_sort.testcases.json';
5+
6+
describe('countSwaps', () => {
7+
it('build tree and flattened tree test cases', () => {
8+
expect.assertions(6);
9+
10+
TEST_CASES.forEach((test) => {
11+
const sortable = new SortableGroup(test.input);
12+
const resultSort = sortable.bubble_sort().group;
13+
const resultPrint = countSwaps(test.input);
14+
15+
expect(resultPrint).toBeUndefined();
16+
expect(resultSort).toStrictEqual(test.sorted);
17+
});
18+
});
19+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"input": [6, 4, 1],
4+
"sorted": [1, 4, 6],
5+
"expected": "Array is sorted in 3 swaps.\nFirst Element: 1\nLast Element: 6\n"
6+
},
7+
{
8+
"input": [3, 2, 1],
9+
"sorted": [1, 2, 3],
10+
"expected": "Array is sorted in 3 swaps.\nFirst Element: 1\nLast Element: 3\n"
11+
},
12+
{
13+
"input": [1, 2, 3],
14+
"sorted": [1, 2, 3],
15+
"expected": "Array is sorted in 0 swaps.\nFirst Element: 1\nLast Element: 3\n"
16+
}
17+
]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/sort/ctci-bubble-sort.md]]
3+
*/
4+
5+
const SEPARATOR = '\n';
6+
7+
export class SortableGroup {
8+
group: number[];
9+
10+
count: number;
11+
12+
constructor(group: number[]) {
13+
this.count = 0;
14+
this.group = group;
15+
}
16+
17+
bubble_sort(): this {
18+
const group = [...this.group];
19+
const size: number = group.length;
20+
let count = 0;
21+
let i = 0;
22+
23+
while (i < size) {
24+
for (let j = 0; j < size - 1; j++) {
25+
if (group[j] > group[j + 1]) {
26+
[group[j], group[j + 1]] = [group[j + 1], group[j]];
27+
28+
count += 1;
29+
}
30+
}
31+
i += 1;
32+
}
33+
34+
this.count = count;
35+
this.group = group;
36+
37+
return this;
38+
}
39+
}
40+
41+
export function countSwaps(a: number[]): void {
42+
const sortableGroup = new SortableGroup(a);
43+
sortableGroup.bubble_sort();
44+
45+
const last = sortableGroup.group.length - 1;
46+
47+
const output =
48+
`Array is sorted in ${sortableGroup.count} swaps.${SEPARATOR}` +
49+
`First Element: ${sortableGroup.group[0]}${SEPARATOR}` +
50+
`Last Element: ${sortableGroup.group[last]}${SEPARATOR}`;
51+
52+
console.log(output);
53+
}
54+
55+
export default { countSwaps, SortableGroup };

0 commit comments

Comments
 (0)