Hier:
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pkg10insertionsort;
import java.util.Arrays;
import java.util.Random;
/**
* @author CB, siehe https://de.wikipedia.org/wiki/Insertionsort
*/
public class Main {
public static <T extends Comparable<T>> void insertionsortA(T[] ta) {
for (int i = 1; i < ta.length; i++) {
T tmp = ta**;
int j = i - 1;
while (j >= 0 && ta[j].compareTo(tmp) > 0) {
ta[j + 1] = ta[j];
j--;
}
ta[j + 1] = tmp;
}
}
public static <T extends Comparable<T>> void insertionsortB(T[] ta) {
for (int i = 1; i < ta.length; i++) {
T tmp = ta**;
int j = i - 1;
while (j >= 0 && ta[j].compareTo(tmp) > 0) {
ta[j + 1] = ta[j];
j--;
}
ta[j + 1] = tmp;
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Random r = new Random();
Integer[] ia = new Integer[20];
for (int i = 0; i < ia.length; i++) {
ia** = r.nextInt(10);
}
System.out.println(Arrays.toString(ia));
insertionsortA(ia);
System.out.println(Arrays.toString(ia));
long sctm1 = System.currentTimeMillis();
//....
long sctm2 = System.currentTimeMillis();
//....
long sctm3 = System.currentTimeMillis();
System.out.println(sctm2 - sctm1 + " 1.");
System.out.println(sctm3 - sctm2 + " 2.");
}
}```
Damit kannst du testen, ob's funktioniert.
[8, 4, 2, 4, 8, 8, 3, 5, 9, 4, 0, 3, 5, 2, 1, 7, 3, 0, 9, 1]
[0, 0, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 7, 8, 8, 8, 9, 9]
Auf dem ersten Blick werden keine verschluckt.
In B kannst du die b/Binäre Suche auf dem Teilarray schreiben und dann mit sortierter und unsortierter Liste mit vielen Elementen die Zeit messen.
Ich habe alles nach de.wikipedia.org/wiki/Insertionsort geschrieben.
2 (Zähl-)Variablen bringen keinen Vorteil. j > -1 bringt keinen Vorteil. ta[++j] = tmp; bringt keinen Vorteil, weil das j hinterher nicht mehr benötigt wird.
Siehe dir Wrapper-Klassen noch mal an. Virle Grüße erst mal.