public void ageKVADRA_selectSort(Person[] personsToAdd){
Person[] tmp = personsToAdd;
for (int i = personsToAdd.length-1; i >= 1; i--) {
int maxID = 0; //pořadi v poli
for (int j = 0; j <= i; j++) {
if(tmp[maxID].getAge() < tmp[j].getAge()){
tmp[maxID] = tmp[j];
maxID = j; //zapsani pořadi v poli
}
}
Person tempPerson = tmp[i];
tmp[i] = tmp[maxID];
tmp[maxID] = tempPerson;
//System.out.println(maxID + " " + tmp[maxID].toString());
}
//this.tmp = tmp;
//System.out.println(tmp[maxID].toString() + maxID + " " + tmp.length);
System.out.println();
}
public void ageKVADRA_bubbleSort(Person[] tmpArry){
//Person[] tmp = personsToAdd;
boolean vymena = false;
int n = tmpArry.length-1;
int j = 0;
for (int i = 0; i <= n-1; i++) {
if(tmpArry[i].getAge() > tmpArry[i+1].getAge()){
Person pom = tmpArry[i];
tmpArry[i] = tmpArry[i+1];
tmpArry[i+1] = pom;
vymena = true;
j = i;
}
System.out.println(vymena);
}
n=j;
System.out.println(n);
}
public Person[] getPersons() {
return persons;
}
public void quickSortHeight(Person[] a, int l, int r){
int i,j,x,k;
Person pom;
k = (l+r)/2;
x = a[k].getHeight();
i = l;
j = r;
do {
while (x > a[i].getHeight()) i++;
while (x < a[j].getHeight()) i--;
if (i <= j) {
pom = a[i];
a[i] = a[j];
a[j] = pom;
i++;
j--;
}
} while (i < j);
if (l < j) quickSortHeight(a,l,j);
if (i < r) quickSortHeight(a,i,r);
}
public void heapSortHeight(Person[] a){
int r, k;
Person pom;
int n = a.length-1;
k = n/2 + 1;
r = n;
while (k>0){
k--;
zarad(a,k,r);
}
while (r>0){
pom = a[0];
a[0] = a[r];
a[r]=pom;
r--;
zarad(a,k,r);
}
}
public void zarad(Person[] a, int k, int r){
int i, j;
Person pom;
i = k;
j = 2*i;
if(j==0) j=1;
pom = a[i];
while (j <= r){
if(j < r && a[j].getHeight() < a[j+1].getHeight()) j++;
if(pom.getHeight() > a[j].getHeight()) break;
a[i] = a[j];
i = j;
j = 2*i;
}
a[i] = pom;
}
Part 2
static void selectSort(int[] a){
int k,max;
int j = a.length-1;
while(j >= 1){
max = a[j];
k = j;
for (int i = 0; i <= j; i++){
if (a[i] > max) {
max = a[i];
k = i;
}
}
a[k] = a[j];
a[j] = max; //umisteni maxima na posledni
misto
j--; //zkraceni pole o jeden prvek
}
}
int n = a.length-1;
for (int i = 0; i <= n-1; i++){ //jeden pruchod polem
if (a[i] > a[i+1]) {
pom = a[i];
a[i] = a[i+1];
a[i+1] = pom;
}
}
static void bubleSort(int[] a){
int pom, n = a.length-1;;
int j = 1;
while(j <= n){ // urcuje pocet pruchodu polem
for (int i = 0; i <= n-j; i++){ // jeden
pruchod polem
if (a[i] > a[i+1]) {
pom = a[i];
a[i] = a[i+1];
a[i+1] = pom;
} }
j = j+1;
}
}
static void quickSort(int[] a, int l, int r){
int i,j,x,k, pom;
k = (l+r)/2;
x = a[k];
i = l;
j = r;
do {
while (x > a[i]) i++; //posun indexu v levé části pole
while (x < a[j]) j--; //posun indexu v pravé části pole
if (i <= j) {
pom = a[i];
a[i] = a[j];
a[j] = pom;
i++;
j--;
}
} while (i < j);
if (l < j) quickSort(a,l,j);
if (i < r) quickSort(a,i,r);
}
static void heapSort (int[] a){
int r, k, pom;
int n = a.length -1;
k = n/2 +1;
r = n;
while (k>0){
k--;
zarad(a,k,r);
}
while (r>0){
pom =a[0];
a[0]=a[r];
a[r]=pom;
r--;
zarad(a,k,r);
}
}
static void zarad(int [] a, int k, int r){
int i, j, pom;
i = k;
j = 2*i;
if (j==0) j =1;
pom = a[i];
while (j <=r ){
if (j < r && a[j] <a[j+1]) j++;
if (pom > a[j]) break;
a[i] = a[j];
i = j;
j = 2*i;
}
a[i] = pom;
}
Train --
public void addWagon(Wagon wagon){
Wagon lastWagon = locomotive.getFirstWagon();
if(lastWagon == null){
locomotive.setFirstWagon(wagon);
}
else {
HashSet<Wagon> wagons = new HashSet<>();
wagons.add(lastWagon);
while (lastWagon.getNext() != null){
lastWagon = lastWagon.getNext();
if(wagons.contains(wagon)){
System.out.println("Train contains loop, cant add wagon");
return;
}
}
lastWagon.setNext(wagon);
wagon.setNext(null);
}