Skip to content

Commit 20f2d3a

Browse files
Josephine BarbozaJosephine Barboza
authored andcommitted
Merge remote-tracking branch 'upstream/master'
2 parents f916c46 + 53d22be commit 20f2d3a

76 files changed

Lines changed: 2258 additions & 385 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ jmeter/src/main/resources/*-JMeter.csv
6262
**/dist
6363
**/tmp
6464
**/out-tsc
65+
**/nbproject/
66+
**/nb-configuration.xml
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.algorithms.insertionsort;
2+
3+
public class InsertionSort {
4+
5+
public static void insertionSortImperative(int[] input) {
6+
for (int i = 1; i < input.length; i++) {
7+
int key = input[i];
8+
int j = i - 1;
9+
while (j >= 0 && input[j] > key) {
10+
input[j + 1] = input[j];
11+
j = j - 1;
12+
}
13+
input[j + 1] = key;
14+
}
15+
}
16+
17+
public static void insertionSortRecursive(int[] input) {
18+
insertionSortRecursive(input, input.length);
19+
}
20+
21+
private static void insertionSortRecursive(int[] input, int i) {
22+
// base case
23+
if (i <= 1) {
24+
return;
25+
}
26+
27+
// sort the first i - 1 elements of the array
28+
insertionSortRecursive(input, i - 1);
29+
30+
// then find the correct position of the element at position i
31+
int key = input[i - 1];
32+
int j = i - 2;
33+
// shifting the elements from their position by 1
34+
while (j >= 0 && input[j] > key) {
35+
input[j + 1] = input[j];
36+
j = j - 1;
37+
}
38+
// inserting the key at the appropriate position
39+
input[j + 1] = key;
40+
}
41+
}

‎algorithms/src/main/java/com/baeldung/algorithms/kthlargest/FindKthLargest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public int findKthElementByRandomizedQuickSelect(Integer[] arr, int left, int ri
9898

9999
private int randomPartition(Integer arr[], int left, int right) {
100100
int n = right - left + 1;
101-
int pivot = (int) (Math.random()) % n;
101+
int pivot = (int) (Math.random() * n);
102102
swap(arr, left + pivot, right);
103103
return partition(arr, left, right);
104104
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.algorithms.insertionsort;
2+
3+
import com.baeldung.algorithms.insertionsort.InsertionSort;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertArrayEquals;
7+
8+
public class InsertionSortUnitTest {
9+
10+
@Test
11+
public void givenUnsortedArray_whenInsertionSortImperative_thenSortedAsc() {
12+
int[] input = {6, 2, 3, 4, 5, 1};
13+
InsertionSort.insertionSortImperative(input);
14+
int[] expected = {1, 2, 3, 4, 5, 6};
15+
assertArrayEquals("the two arrays are not equal", expected, input);
16+
}
17+
18+
@Test
19+
public void givenUnsortedArray_whenInsertionSortRecursive_thenSortedAsc() {
20+
int[] input = {6, 4, 5, 2, 3, 1};
21+
InsertionSort.insertionSortRecursive(input);
22+
int[] expected = {1, 2, 3, 4, 5, 6};
23+
assertArrayEquals("the two arrays are not equal", expected, input);
24+
}
25+
}

‎core-java-8/src/test/java/com/baeldung/internationalization/DateTimeFormatterUnitTest.java‎

Lines changed: 0 additions & 46 deletions
This file was deleted.

‎core-java-collections/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
- [Thread Safe LIFO Data Structure Implementations](https://www.baeldung.com/java-lifo-thread-safe)
4949
- [Collections.emptyList() vs. New List Instance](https://www.baeldung.com/java-collections-emptylist-new-list)
5050
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
51+
- [Differences Between Collection.clear() and Collection.removeAll()](https://www.baeldung.com/java-collection-clear-vs-removeall)
5152
- [Performance of contains() in a HashSet vs ArrayList](https://www.baeldung.com/java-hashset-arraylist-contains-performance)
5253
- [Get the Key for a Value from a Java Map](https://www.baeldung.com/java-map-key-from-value)
5354
- [Time Complexity of Java Collections](https://www.baeldung.com/java-collections-complexity)

‎core-java-collections/pom.xml‎

Lines changed: 83 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,83 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
<modelVersion>4.0.0</modelVersion>
4-
<groupId>com.baeldung</groupId>
5-
<artifactId>core-java-collections</artifactId>
6-
<version>0.1.0-SNAPSHOT</version>
7-
<packaging>jar</packaging>
8-
<name>core-java-collections</name>
9-
10-
<parent>
11-
<groupId>com.baeldung</groupId>
12-
<artifactId>parent-java</artifactId>
13-
<version>0.0.1-SNAPSHOT</version>
14-
<relativePath>../parent-java</relativePath>
15-
</parent>
16-
17-
<dependencies>
18-
<dependency>
19-
<groupId>net.sourceforge.collections</groupId>
20-
<artifactId>collections-generic</artifactId>
21-
<version>${collections-generic.version}</version>
22-
</dependency>
23-
<dependency>
24-
<groupId>org.apache.commons</groupId>
25-
<artifactId>commons-collections4</artifactId>
26-
<version>${commons-collections4.version}</version>
27-
</dependency>
28-
<dependency>
29-
<groupId>com.jayway.awaitility</groupId>
30-
<artifactId>awaitility</artifactId>
31-
<version>${avaitility.version}</version>
32-
<scope>test</scope>
33-
</dependency>
34-
<dependency>
35-
<groupId>org.apache.commons</groupId>
36-
<artifactId>commons-lang3</artifactId>
37-
<version>${commons-lang3.version}</version>
38-
</dependency>
39-
<dependency>
40-
<groupId>org.eclipse.collections</groupId>
41-
<artifactId>eclipse-collections</artifactId>
42-
<version>${eclipse.collections.version}</version>
43-
</dependency>
44-
<dependency>
45-
<groupId>org.assertj</groupId>
46-
<artifactId>assertj-core</artifactId>
47-
<version>${assertj.version}</version>
48-
<scope>test</scope>
49-
</dependency>
50-
<dependency>
51-
<groupId>org.junit.platform</groupId>
52-
<artifactId>junit-platform-runner</artifactId>
53-
<version>${junit.platform.version}</version>
54-
<scope>test</scope>
55-
</dependency>
56-
<dependency>
57-
<groupId>org.openjdk.jmh</groupId>
58-
<artifactId>jmh-core</artifactId>
59-
<version>${openjdk.jmh.version}</version>
60-
</dependency>
61-
<dependency>
62-
<groupId>org.openjdk.jmh</groupId>
63-
<artifactId>jmh-generator-annprocess</artifactId>
64-
<version>${openjdk.jmh.version}</version>
65-
</dependency>
66-
<dependency>
67-
<groupId>org.apache.commons</groupId>
68-
<artifactId>commons-exec</artifactId>
69-
<version>1.3</version>
70-
</dependency>
71-
72-
</dependencies>
73-
74-
75-
<properties>
76-
<openjdk.jmh.version>1.19</openjdk.jmh.version>
77-
<junit.platform.version>1.2.0</junit.platform.version>
78-
<commons-lang3.version>3.5</commons-lang3.version>
79-
<commons-collections4.version>4.1</commons-collections4.version>
80-
<collections-generic.version>4.01</collections-generic.version>
81-
<avaitility.version>1.7.0</avaitility.version>
82-
<assertj.version>3.6.1</assertj.version>
83-
<eclipse.collections.version>7.1.0</eclipse.collections.version>
84-
</properties>
85-
</project>
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>core-java-collections</artifactId>
6+
<version>0.1.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<name>core-java-collections</name>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>parent-java</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<relativePath>../parent-java</relativePath>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>net.sourceforge.collections</groupId>
20+
<artifactId>collections-generic</artifactId>
21+
<version>${collections-generic.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.apache.commons</groupId>
25+
<artifactId>commons-collections4</artifactId>
26+
<version>${commons-collections4.version}</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.jayway.awaitility</groupId>
30+
<artifactId>awaitility</artifactId>
31+
<version>${avaitility.version}</version>
32+
<scope>test</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.apache.commons</groupId>
36+
<artifactId>commons-lang3</artifactId>
37+
<version>${commons-lang3.version}</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.eclipse.collections</groupId>
41+
<artifactId>eclipse-collections</artifactId>
42+
<version>${eclipse.collections.version}</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.assertj</groupId>
46+
<artifactId>assertj-core</artifactId>
47+
<version>${assertj.version}</version>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.junit.platform</groupId>
52+
<artifactId>junit-platform-runner</artifactId>
53+
<version>${junit.platform.version}</version>
54+
<scope>test</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.openjdk.jmh</groupId>
58+
<artifactId>jmh-core</artifactId>
59+
<version>${openjdk.jmh.version}</version>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.openjdk.jmh</groupId>
63+
<artifactId>jmh-generator-annprocess</artifactId>
64+
<version>${openjdk.jmh.version}</version>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.apache.commons</groupId>
68+
<artifactId>commons-exec</artifactId>
69+
<version>1.3</version>
70+
</dependency>
71+
</dependencies>
72+
73+
<properties>
74+
<openjdk.jmh.version>1.19</openjdk.jmh.version>
75+
<junit.platform.version>1.2.0</junit.platform.version>
76+
<commons-lang3.version>3.5</commons-lang3.version>
77+
<commons-collections4.version>4.1</commons-collections4.version>
78+
<collections-generic.version>4.01</collections-generic.version>
79+
<avaitility.version>1.7.0</avaitility.version>
80+
<assertj.version>3.6.1</assertj.version>
81+
<eclipse.collections.version>7.1.0</eclipse.collections.version>
82+
</properties>
83+
</project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.convertcollectiontoarraylist;
2+
3+
/**
4+
* This POJO is the element type of our collection. It has a deepCopy() method.
5+
*
6+
* @author chris
7+
*/
8+
public class Foo {
9+
10+
private int id;
11+
private String name;
12+
private Foo parent;
13+
14+
public Foo() {
15+
}
16+
17+
public Foo(int id, String name, Foo parent) {
18+
this.id = id;
19+
this.name = name;
20+
this.parent = parent;
21+
}
22+
23+
public int getId() {
24+
return id;
25+
}
26+
27+
public void setId(int id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
39+
public Foo getParent() {
40+
return parent;
41+
}
42+
43+
public void setParent(Foo parent) {
44+
this.parent = parent;
45+
}
46+
47+
public Foo deepCopy() {
48+
return new Foo(
49+
this.id, this.name, this.parent != null ? this.parent.deepCopy() : null);
50+
}
51+
52+
}

0 commit comments

Comments
 (0)