Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7b08c6b

Browse files
Added Insertion Sort custom implementation
Added draft of Insertion Sort Move test to specific class
1 parent 1f8ddbd commit 7b08c6b

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package br.com.zevolution.algorithms.sorting.insertionsort;
2+
3+
public class InsertionSort {
4+
5+
public static Product[] sortingByCheapest(Product[] products, int length) {
6+
Product[] array = products.clone();
7+
for (int current = 1; current < length; current++) {
8+
int beingAnalyzed = current;
9+
10+
while (beingAnalyzed > 0) {
11+
int previousBeingAnalyzed = beingAnalyzed - 1;
12+
13+
Product currentProductBeingAnalyzed = array[beingAnalyzed];
14+
Product previousProductBeingAnalyzed = array[previousBeingAnalyzed];
15+
if (currentProductBeingAnalyzed.getPrice() < previousProductBeingAnalyzed.getPrice()) {
16+
array[beingAnalyzed] = previousProductBeingAnalyzed;
17+
array[previousBeingAnalyzed] = currentProductBeingAnalyzed;
18+
19+
beingAnalyzed--;
20+
} else {
21+
break;
22+
}
23+
}
24+
}
25+
return array;
26+
}
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package br.com.zevolution.algorithms.sorting.insertionsort;
2+
3+
public class Product {
4+
5+
private String description;
6+
private double price;
7+
8+
public Product(String description, double price) {
9+
this.description = description;
10+
this.price = price;
11+
}
12+
13+
public double getPrice() {
14+
return price;
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return this.description;
20+
}
21+
22+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package br.com.zevolution.algorithms.sorting.insertionsort;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.Arrays;
6+
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
import br.com.zevolution.algorithms.sorting.insertionsort.Product;
11+
import br.com.zevolution.algorithms.sorting.insertionsort.InsertionSort;
12+
13+
public class InsertionSortTest {
14+
15+
private static final double CHEAPEST_PRODUCT = 5;
16+
private static final double MOST_EXPENSIVE_PRODUCT = 50;
17+
private Product[] products;
18+
19+
@Before
20+
public void init() {
21+
Product[] array = {
22+
new Product("iMac", MOST_EXPENSIVE_PRODUCT),
23+
new Product("iPhone", 8),
24+
new Product("Notebook", 7),
25+
new Product("Keyboard", 9),
26+
new Product("Mouse", CHEAPEST_PRODUCT)
27+
};
28+
this.products = array;
29+
}
30+
31+
@Test
32+
public void should_Get_CheapestProduct() {
33+
Product[] ordened = InsertionSort.sortingByCheapest(this.products, this.products.length);
34+
assertEquals(CHEAPEST_PRODUCT, ordened[0].getPrice(), 0);
35+
}
36+
37+
@Test
38+
public void should_Get_MostExpensiveProduct() {
39+
Product[] ordened = InsertionSort.sortingByCheapest(this.products, this.products.length);
40+
assertEquals(MOST_EXPENSIVE_PRODUCT, ordened[ordened.length - 1].getPrice(), 0);
41+
}
42+
43+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /