Advertisement
Bujkoffer

Sortalgoryhtmen

Mar 23rd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.94 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3.  
  4. public class Sortierung {
  5.     private static int[] _array;
  6.     private final static int _shortViewItemCount = 20;
  7.     private static int _count = 9000;
  8.     private static int _range = 1000;
  9.  
  10.     /**
  11.      * @param args
  12.      */
  13.     public static void main(String[] args) {
  14.         //*****Initializing*****
  15.         initArray(_count, _range);
  16.         printArray(_array, "Initial array");
  17.        
  18.         //*****BubbleSort*****
  19.         int[] bubbled = bubbleSort(_array, false);
  20.         printArray(bubbled, "BubbleSort");
  21.        
  22.         int[] bubbledRev = bubbleSort(_array, true);
  23.         printArray(bubbledRev, "BubbleSort reversed");
  24.        
  25.         //*****InsertionSort*****
  26.         int[] inserted = insertionSort(_array, false);
  27.         printArray(inserted, "InsertionSort");
  28.        
  29.         int[] insertedRev = insertionSort(_array, true);
  30.         printArray(insertedRev, "InsertionSort reversed");
  31.        
  32.         //*****SelectionSort*****
  33.         int[] selected = selectionSort(_array, false);
  34.         printArray(selected, "SelectionSort");
  35.        
  36.         int[] selectedRev = selectionSort(_array, true);
  37.         printArray(selectedRev, "SelectionSort reversed");
  38.        
  39.         System.out.println("Icke hab en jelbet T-Shirt an.");
  40.     }
  41.    
  42.     public static void initArray(int count, int range) {
  43.         _array = new int[count];
  44.        
  45.         for(int i = 0; i < _array.length; i++) {
  46.             int num = (int)(Math.random() * range);
  47.             _array[i] = num;
  48.         }
  49.     }
  50.    
  51.     // TODO:
  52.     public static int[] bubbleSort(int[] arr, boolean reverse) {
  53.         //important: do a copy of our array, so the passed one is not altered!
  54.         int[] array = Arrays.copyOf(arr, arr.length);
  55.        
  56.         long start = System.nanoTime();
  57.        
  58.         boolean swapped = true;
  59.         int j = 0;
  60.         int swap = 0;
  61.    
  62.           while (swapped) {
  63.                 swapped = false;
  64.                 j++;
  65.                 for (int i = 0; i < array.length - j; i++) {                                      
  66.                       if (array[i] > array[i + 1]) {                          
  67.                             swap = array[i];
  68.                             array[i] = array[i + 1];
  69.                             array[i + 1] = swap;
  70.                             swapped = true;
  71.                       }
  72.                 }                
  73.           }
  74.          
  75.           long elapsed = System.nanoTime() - start;
  76.           System.out.println("BubbleSort took " + elapsed / 1000000 + " ms");
  77.          
  78.           if(reverse)
  79.               reverseArray(array);
  80.          
  81.           return array;
  82.     }
  83.  
  84.     public static void quickSort(int[] arr, boolean reverse) {
  85.         //important: do a copy of our array, so the passed one is not altered!
  86.         int[] array = Arrays.copyOf(arr, arr.length);
  87.     }
  88.    
  89.     //TODO: https://www.inf.fh-flensburg.de/lang/algorithmen/sortieren/insert/insertion.htm
  90.     public static int[] insertionSort(int[] arr, boolean reverse) {
  91.         //important: do a copy of our array, so the passed one is not altered!
  92.         int[] array = Arrays.copyOf(arr, arr.length);
  93.        
  94.         long start = System.nanoTime();
  95.        
  96.         int len = array.length;
  97.         int j = 0;
  98.         int t = 0;
  99.        
  100.         for (int i = 0; i < len; i++) {
  101.             j= i;
  102.             t=array[j];
  103.             while(j > 0 && array[j - 1] > t) {
  104.                 array[j] = array[j-1];
  105.                 j--;
  106.             }
  107.             array[j] = t;
  108.         }
  109.        
  110.         long elapsed = System.nanoTime() - start;
  111.         System.out.println("InsertionSort took " + elapsed / 1000000 + " ms");
  112.          
  113.         if(reverse)
  114.             reverseArray(array);
  115.        
  116.         return array;
  117.     }
  118.    
  119.     // TODO: https://de.wikipedia.org/wiki/Selectionsort
  120.     public static int[] selectionSort(int[] arr, boolean reverse) {
  121.         //important: do a copy of our array, so the passed one is not altered!
  122.         int[] array = Arrays.copyOf(arr, arr.length);
  123.        
  124.         long start = System.nanoTime();
  125.        
  126.         int len = array.length - 1;
  127.         int left = 0;
  128.        
  129.         do {
  130.             int min = left;
  131.            
  132.             for(int i = left + 1; i < len; i++) {
  133.                 if(array[i] < array[min])
  134.                     min = i;
  135.             }
  136.            
  137.             int tmp = array[min];
  138.             array[min] = array[left];
  139.             array[left] = tmp;
  140.            
  141.             left = left + 1;
  142.         } while(left < len);
  143.        
  144.        
  145.         long elapsed = System.nanoTime() - start;
  146.         System.out.println("SelectionSort took " + elapsed / 1000000 + " ms");
  147.          
  148.         if(reverse)
  149.             reverseArray(array);
  150.  
  151.         return array;
  152.     }
  153.    
  154.     public static void printArray(int[] array, String desc) {
  155.         int len = array.length;
  156.         boolean shortenedView = false;
  157.  
  158.         if(len > _shortViewItemCount) {
  159.             len = _shortViewItemCount;
  160.             shortenedView = true;
  161.         }
  162.        
  163.         System.out.print(desc + ": { ");
  164.        
  165.         // TODO: join method?
  166.         for(int i = 0; i < len; i++) {
  167.             System.out.print(array[i]);
  168.  
  169.             if(i < len - 1)
  170.                 System.out.print(", ");
  171.             else
  172.                 if(shortenedView)
  173.                     System.out.print("...");
  174.         }
  175.        
  176.         System.out.print(" }");
  177.         if(shortenedView) {
  178.             System.out.print(" (");
  179.             System.out.print(array.length);
  180.             System.out.print(" items in total)");
  181.         }
  182.         System.out.println();
  183.     }
  184.    
  185.     //TODO: https://stackoverflow.com/questions/2137755/how-do-i-reverse-an-int-array-in-java
  186.     public static void reverseArray(int[] data) {
  187.         for (int left = 0, right = data.length - 1; left < right; left++, right--) {
  188.             int temp = data[left];
  189.             data[left]  = data[right];
  190.             data[right] = temp;
  191.         }
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement