Pr_robot_factory/domain/R2D2.java

47 lines
1.0 KiB
Java

package domain;
import robot.interfaces.Sorting;
import java.util.Arrays;
public class R2D2 extends RobotBasics {
/**
* Constructor
* @param id> int
* @param name> String
*/
public R2D2(int id, String name){
super(id, name);
}
public String sorting(int[] arr) {
//Selectionsort
int small;
for (int i = 0; i <arr.length - 1; i++) {
small = i;
for (int j = i + 1; j < arr.length; j++) {
//if there is a smaller number on this position
if (arr[j] < arr[small]) {
small = j;
//swap values
int temp = arr[i];
arr[i] = arr[small];
arr[small] = temp;
}
}
}
//Writing all values of the array into a String
String result = "";
if(arr.length != 0) {
result = "" + arr[0];
if (arr.length > 1) {
for (int i = 1; i < arr.length; i++) {
result += "," + arr[i];
}
}
}
return result;
}
}