Guest User

Untitled

a guest
May 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. /**
  2.  * An implementation of InsertionSort
  3.  *
  4.  * Main.java
  5.  *
  6.  * @author Stefan Löwen <stefan.loewen@googlemail.com>
  7.  *
  8.  */
  9.  
  10. public class Main {
  11.     public static void main(String[] args) {
  12.         int[] array = { 6,5,4,3,2,1 };
  13.  
  14.         /*
  15.          * print the initial array
  16.          */
  17.         for (int i = 0; i < array.length; i++) {
  18.             System.out.print(array[i] + ", ");
  19.         }
  20.         System.out.println();
  21.        
  22.        
  23.         /*
  24.          * sort the array
  25.          * ala Wikipedia
  26.          */
  27.         /*
  28.         for (int i = 1; i < array.length; i++) {
  29.             int clipboard = array[i]; // Lesezugriff
  30.            
  31.             int j = i;
  32.             while (j>0 && array[j-1]>clipboard) {   // 1 Vergleich
  33.                 array[j] = array[j-1];  // Tauschoperation
  34.                 j--;
  35.             }
  36.            
  37.             array[j] = clipboard; // Schreibzugriff
  38.         }
  39.         */
  40.        
  41.         /*
  42.          * sort the array
  43.          * using bubbles
  44.          */
  45.         int tauschOps = 0;
  46.         int vergleichOps = 0;
  47.        
  48.         for (int i = 1; i < array.length; i++) {
  49.             int j = i;
  50.            
  51.             /* bubble it up */
  52.             while (true) {
  53.                 if (! (j>0)) {
  54.                     break;
  55.                 }
  56.                
  57.                 vergleichOps++;
  58.  
  59.                 if (! (array[j-1]>array[j])) {
  60.                     break;
  61.                 }
  62.                
  63.                 /* do the switch */
  64.                 int clipboard = array[j];
  65.                 array[j] = array[j-1];
  66.                 array[j-1] = clipboard;
  67.                 tauschOps++;
  68.                
  69.                 j--;
  70.             }
  71.         }
  72.         System.out.println("#Vergleich:\t" + vergleichOps);
  73.         System.out.println("#Tausch:\t" + tauschOps);
  74.  
  75.         /*
  76.          * print the sorted array
  77.          */
  78.         for (int i = 0; i < array.length; i++) {
  79.             System.out.print(array[i] + ", ");
  80.         }
  81.     }
  82. }
  83.  
  84. /*
  85.  * Laufzeitanalyse
  86.  * ---
  87.  * 2 Elemente:
  88.  *  BestCase:
  89.  *      1 Vergleich
  90.  *      0 Tausch
  91.  *  WorstCase:
  92.  *      1 Vergleich
  93.  *      1 Tausch
  94.  * ---
  95.  * 3 Elemente:
  96.  *  BestCase:
  97.  *      2 Vergleich
  98.  *      0 Tausch
  99.  *  WorstCase:
  100.  *      3 Vergleich
  101.  *      3 Tausch
  102.  * ---
  103.  * 4 Elemente:
  104.  *  BestCase:
  105.  *      3 Vergleich
  106.  *      0 Tausch
  107.  *  WorstCase:
  108.  *      6 Vergleich
  109.  *      6 Tausch
  110.  * ---
  111.  * 5 Elemente:
  112.  *  BestCase:
  113.  *      4 Vergleich
  114.  *      0 Tausch
  115.  *  WorstCase:
  116.  *      10 Vergleich
  117.  *      10 Tausch
  118.  * ---
  119.  * 6 Elemente:
  120.  *  BestCase:
  121.  *      5 Vergleich
  122.  *      0 Tausch
  123.  *  WorstCase:
  124.  *      15 Vergleich
  125.  *      15 Tausch
  126.  */
Add Comment
Please, Sign In to add comment