Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int[] doInsertionSort(int[] array) {
- int zwischenspeicher; // Zwischenspeicher erstellen (FÜR DEN TAUSCH)
- for (int i = 1; i < array.length; i++) { // Durchlauf fängt mit der Hausnummer 2 an
- for (int SortZahl = i; SortZahl > 0; SortZahl--) { // Immer durchsuchen, wo die unsortierte Zahl ist
- if (array[SortZahl] < array[SortZahl - 1]) { // Tausch und die Zahlen in die richtigen Stellen...
- zwischenspeicher = array[SortZahl];
- array[SortZahl] = array[SortZahl - 1];
- array[SortZahl - 1] = zwischenspeicher;
- }
- }
- }
- return array;
- }
- public static int[] doBubbleSort(int[] array) {
- int zwischenspeicher;
- for (int i = 0; i < array.length; i++) { // Bestimme die Anzahl der Tauschfensterdurchläufe
- for (int linkesFenster = 0; linkesFenster < array.length - 1; linkesFenster++) { // Schiebt Tauschfenster von links nach rechts
- int stelleRechts = linkesFenster + 1;
- if (array[linkesFenster] > array[stelleRechts]) {
- zwischenspeicher = array[linkesFenster];
- array[linkesFenster] = array[stelleRechts];
- array[stelleRechts] = zwischenspeicher;
- }
- }
- }
- return array;
- }
- public static int[] doOptimizedBubbleSort(int[] array) {
- int zwischenspeicher;
- int n = array.length;
- do {
- int newn = 1;
- for (int i = 0; i < n-1; i++) {
- if (array[i] > array[i+1]) {
- zwischenspeicher = array[i];
- array[i] = array[i+1]; //SWAP
- array[i+1] = zwischenspeicher;
- newn = i+1;
- }
- }
- n = newn;
- } while (n > 1);
- return array;
- }
- public static int[] doSelectionSort(int[] array) {
- int gefundenesMinimum;
- int minimumPosition;
- int zwischenspeicher;
- for (int stelleZuBesetzen = 0; stelleZuBesetzen < array.length; stelleZuBesetzen++) {
- gefundenesMinimum = array[stelleZuBesetzen];
- minimumPosition = stelleZuBesetzen;
- // durchsuche unsortierten Bereich
- for (int i = stelleZuBesetzen; i < array.length; i++) {
- if (gefundenesMinimum > array[i]) {
- gefundenesMinimum = array[i];
- minimumPosition = i;
- }
- }
- // Tausch
- zwischenspeicher = array[stelleZuBesetzen];
- array[stelleZuBesetzen] = array[minimumPosition];
- array[minimumPosition] = zwischenspeicher;
- }
- return array;
- }
- public static void doBucketSort(int[] a, int b) {
- int[] buckets = new int[b];
- for (int i = 0; i < a.length; i++) {
- buckets[a[i]]++;
- }
- int c = 0;
- for (int i = 0; i < b; i++) {
- while (buckets[i] > 0) {
- a[c] = i;
- c++;
- buckets[i]--;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement