Skip to content

Commit b719e2b

Browse files
Manoj PawarManoj Pawar
Manoj Pawar
authored and
Manoj Pawar
committed
first commit
0 parents  commit b719e2b

20 files changed

+653
-0
lines changed

.vscode/launch.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"configurations": [
3+
{
4+
"type": "java",
5+
"name": "CodeLens (Launch) - App",
6+
"request": "launch",
7+
"mainClass": "org.manoj.App"
8+
}
9+
]
10+
}

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"java.configuration.updateBuildConfiguration": "automatic"
3+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# sorting-and-search-algorithms-in-java

ds/.editorconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Editor configuration, see http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 4
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
max_line_length = off
13+
trim_trailing_whitespace = false
14+
15+
[*.yml]
16+
indent_size = 2
17+
18+
[*.sh]
19+
end_of_line = lf

ds/.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# When shell scripts end in CRLF, bash gives a cryptic error message
2+
*.sh text eol=lf

ds/.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# Standard Maven .gitignore
3+
#
4+
target/
5+
pom.xml.tag
6+
pom.xml.releaseBackup
7+
pom.xml.versionsBackup
8+
pom.xml.next
9+
release.properties
10+
dependency-reduced-pom.xml
11+
buildNumber.properties
12+
.mvn/timing.properties
13+
14+
#
15+
# IntelliJ
16+
#
17+
*.iml
18+
.idea/*
19+
!.idea/runConfigurations/
20+
21+
#
22+
# Visual Studio Code
23+
#
24+
.settings/
25+
.classpath
26+
.project
27+
.vscode/

ds/.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: java
2+
jdk: oraclejdk8
3+
after_success:
4+
- mvn coveralls:report

ds/pom.xml

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.manoj</groupId>
5+
<artifactId>ds</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<properties>
8+
<maven.compiler.source>1.8</maven.compiler.source>
9+
<maven.compiler.target>1.8</maven.compiler.target>
10+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11+
</properties>
12+
<dependencies>
13+
<dependency>
14+
<groupId>junit</groupId>
15+
<artifactId>junit</artifactId>
16+
<version>4.12</version>
17+
<scope>test</scope>
18+
</dependency>
19+
<dependency>
20+
<groupId>log4j</groupId>
21+
<artifactId>log4j</artifactId>
22+
<version>1.2.17</version>
23+
</dependency>
24+
</dependencies>
25+
<build>
26+
<pluginManagement>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.apache.maven.plugins</groupId>
30+
<artifactId>maven-checkstyle-plugin</artifactId>
31+
<version>3.0.0</version>
32+
<dependencies>
33+
<dependency>
34+
<groupId>com.puppycrawl.tools</groupId>
35+
<artifactId>checkstyle</artifactId>
36+
<version>8.10</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>com.github.ngeor</groupId>
40+
<artifactId>checkstyle-rules</artifactId>
41+
<version>1.1.0</version>
42+
</dependency>
43+
</dependencies>
44+
<configuration>
45+
<configLocation>com/github/ngeor/checkstyle.xml</configLocation>
46+
<includeTestSourceDirectory>true</includeTestSourceDirectory>
47+
</configuration>
48+
</plugin>
49+
</plugins>
50+
</pluginManagement>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-checkstyle-plugin</artifactId>
55+
</plugin>
56+
57+
<!--
58+
You can run jacoco in the default profile with:
59+
mvn jacoco:prepare-agent test jacoco:report
60+
-->
61+
<plugin>
62+
<groupId>org.jacoco</groupId>
63+
<artifactId>jacoco-maven-plugin</artifactId>
64+
<version>0.8.1</version>
65+
</plugin>
66+
</plugins>
67+
</build>
68+
<reporting>
69+
<plugins>
70+
<plugin>
71+
<groupId>org.apache.maven.plugins</groupId>
72+
<artifactId>maven-javadoc-plugin</artifactId>
73+
<version>3.0.0</version>
74+
</plugin>
75+
<plugin>
76+
<groupId>org.apache.maven.plugins</groupId>
77+
<artifactId>maven-checkstyle-plugin</artifactId>
78+
<configuration>
79+
<configLocation>com/github/ngeor/checkstyle.xml</configLocation>
80+
<includeTestSourceDirectory>true</includeTestSourceDirectory>
81+
</configuration>
82+
</plugin>
83+
</plugins>
84+
</reporting>
85+
86+
<profiles>
87+
<!--
88+
This profile enables jacoco when unit tests are run.
89+
You can run it with mvn -P jacoco test.
90+
It also activates itself on Travis.
91+
-->
92+
<profile>
93+
<id>jacoco</id>
94+
<activation>
95+
<property>
96+
<name>env.TRAVIS</name>
97+
</property>
98+
</activation>
99+
<build>
100+
<plugins>
101+
<plugin>
102+
<groupId>org.jacoco</groupId>
103+
<artifactId>jacoco-maven-plugin</artifactId>
104+
<executions>
105+
<execution>
106+
<id>prepare-agent</id>
107+
<phase>validate</phase>
108+
<goals>
109+
<goal>prepare-agent</goal>
110+
</goals>
111+
</execution>
112+
<execution>
113+
<id>report</id>
114+
<phase>test</phase>
115+
<goals>
116+
<goal>report</goal>
117+
</goals>
118+
</execution>
119+
</executions>
120+
</plugin>
121+
</plugins>
122+
</build>
123+
</profile>
124+
125+
<!--
126+
For the Travis profile:
127+
- we want to break the build on any checkstyle violation.
128+
- we want to be able to publish coverage report to coveralls.
129+
-->
130+
<profile>
131+
<id>travis</id>
132+
<activation>
133+
<property>
134+
<name>env.TRAVIS</name>
135+
</property>
136+
</activation>
137+
<build>
138+
<plugins>
139+
<plugin>
140+
<groupId>org.apache.maven.plugins</groupId>
141+
<artifactId>maven-checkstyle-plugin</artifactId>
142+
<executions>
143+
<execution>
144+
<id>checkstyle</id>
145+
<phase>test</phase>
146+
<goals>
147+
<goal>check</goal>
148+
</goals>
149+
</execution>
150+
</executions>
151+
</plugin>
152+
<plugin>
153+
<groupId>org.eluder.coveralls</groupId>
154+
<artifactId>coveralls-maven-plugin</artifactId>
155+
<version>4.3.0</version>
156+
</plugin>
157+
</plugins>
158+
</build>
159+
</profile>
160+
</profiles>
161+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.manoj.ds;
2+
3+
/**
4+
* Hello world!
5+
*/
6+
public final class App {
7+
private App() {
8+
}
9+
10+
/**
11+
* Says hello to the world.
12+
* @param args The arguments of the program.
13+
*/
14+
public static void main(String[] args) {
15+
System.out.println("Hello World!");
16+
}
17+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
The time complexity of the Algorithm is measured in the notation called Big-O notation.
2+
3+
Constant => O(1) - It is ideal time complexity for any alogrithm
4+
Logarithmic => O(log n)
5+
Linear => O(n)
6+
N log star N => O(n log n)
7+
Quadratic => O(n^2) - It is wrost time complexity for any alogrithm
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.manoj.ds.sort;
2+
3+
import java.time.Duration;
4+
import java.time.Instant;
5+
6+
import org.apache.log4j.Logger;
7+
8+
/**
9+
* In Bubble Sort algorithm, the array of elements are partitioned into two
10+
* parts, the unsorted partition and sorted partition The larger element of
11+
* array are bubbled to top of the array.
12+
*
13+
* It is inplace algorithm. Not depend of the no of items in array The time
14+
* complexity is O(n^2) i.e. quadratic. Algorithm performance degrade as element
15+
* increases in array.
16+
*
17+
* It is stable sorting algorithms. As element are getting swapping only if
18+
* comparison greater than or less than based upon the ascending or descending
19+
* sorting parameter.
20+
*/
21+
public class BubbleSort implements Sort {
22+
23+
final static Logger logger = Logger.getLogger(BubbleSort.class);
24+
private SortingMetadata metadata = null;
25+
26+
/**
27+
*
28+
*/
29+
@Override
30+
public <E extends Comparable<E>> void sort(E[] elements) {
31+
sort(elements, true);
32+
}
33+
34+
/**
35+
*
36+
*/
37+
@Override
38+
public <E extends Comparable<E>> void sort(E[] elements, boolean ascending) {
39+
metadata = new SortingMetadata(elements.length);
40+
Instant start = Instant.now();
41+
for (int lastUnsortedIndex = elements.length - 1; lastUnsortedIndex > 0; lastUnsortedIndex--) {
42+
logger.info("lastUnsortedIndex=" + lastUnsortedIndex + ":");
43+
for (int bubbledIndex = 0; bubbledIndex < lastUnsortedIndex; bubbledIndex++) {
44+
if (ascending) {
45+
if (elements[bubbledIndex].compareTo(elements[bubbledIndex + 1]) > 0) {
46+
Sort.swap(elements, bubbledIndex, bubbledIndex + 1);
47+
metadata.increamentSwapOperationByOne();
48+
}
49+
} else {
50+
if (elements[bubbledIndex].compareTo(elements[bubbledIndex + 1]) < 0) {
51+
Sort.swap(elements, bubbledIndex, bubbledIndex + 1);
52+
metadata.increamentSwapOperationByOne();
53+
}
54+
}
55+
logger.info("\tbubbledIndex=" + bubbledIndex + ":");
56+
Sort.logInfo(logger, elements);
57+
metadata.incrementIterationByOne();
58+
}
59+
}
60+
Instant finish = Instant.now();
61+
metadata.setTimeTakenToExecute(Duration.between(start, finish).toNanos());
62+
}
63+
64+
/**
65+
* @return SortingMetadata return the metadata
66+
*/
67+
@Override
68+
public SortingMetadata getMetadata() {
69+
return metadata;
70+
}
71+
72+
}

0 commit comments

Comments
 (0)