Skip to content

Commit 1f8ddbd

Browse files
committed
Added Selection Sort custom implementation
Created algorithm to get the cheapest value Created 'Product' as a representation model Extract algorithm to getCheapest method Created method 'sortingByCheapest' using getCheapest method Clone original array to prevents its modification Move test to specific class
1 parent 4f7fee9 commit 1f8ddbd

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package br.com.zevolution.algorithms.sorting.selectionsort;
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package br.com.zevolution.algorithms.sorting.selectionsort;
2+
3+
public class SelectionSort {
4+
5+
public static Product[] sortingByCheapest(Product[] products, int length) {
6+
Product[] array = products.clone();
7+
for (int current = 0; current < length - 1; current++) {
8+
int cheapest = getCheapest(array, current, length - 1);
9+
10+
Product currentProduct = array[current];
11+
Product cheapestProduct = array[cheapest];
12+
13+
array[current] = cheapestProduct;
14+
array[cheapest] = currentProduct;
15+
}
16+
return array;
17+
}
18+
19+
private static int getCheapest(Product[] products, int beginIndex, int endIndex) {
20+
int cheapest = beginIndex;
21+
for (int current = beginIndex; current <= endIndex; current++) {
22+
if (products[current].getPrice() < products[cheapest].getPrice()) {
23+
cheapest = current;
24+
}
25+
}
26+
return cheapest;
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package br.com.zevolution.algorithms.sorting.selectionsort;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
import org.junit.Before;
8+
9+
public class SelectionSortTest {
10+
11+
private static final double CHEAPEST_PRODUCT = 5;
12+
private static final double MOST_EXPENSIVE_PRODUCT = 50;
13+
private Product[] products;
14+
15+
@Before
16+
public void init() {
17+
Product[] array = {
18+
new Product("iMac", MOST_EXPENSIVE_PRODUCT),
19+
new Product("iPhone", 8),
20+
new Product("Notebook", 7),
21+
new Product("Keyboard", 9),
22+
new Product("Mouse", CHEAPEST_PRODUCT)
23+
};
24+
this.products = array;
25+
}
26+
27+
@Test
28+
public void should_Get_CheapestProduct() {
29+
Product[] ordened = SelectionSort.sortingByCheapest(this.products, this.products.length);
30+
assertEquals(CHEAPEST_PRODUCT, ordened[0].getPrice(), 0);
31+
}
32+
33+
@Test
34+
public void should_Get_MostExpensiveProduct() {
35+
Product[] ordened = SelectionSort.sortingByCheapest(this.products, this.products.length);
36+
assertEquals(MOST_EXPENSIVE_PRODUCT, ordened[ordened.length - 1].getPrice(), 0);
37+
}
38+
39+
}

0 commit comments

Comments
 (0)