Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Random;
- public class shaker {
- /**
- * @param args
- */
- public static void quickSort(int[] array, int links, int rechts)
- {
- int pivot, temp;
- int l, r;
- if (links < rechts)
- {
- l = links; r = rechts;
- pivot = array[(links+rechts) >> 1];
- do
- {
- while (array[l] < pivot) l++;
- while (array[r] > pivot) r--;
- if (l <= r)
- {
- temp = array[r]; array[r] = array[l]; array[l] = temp;
- l++; r--;
- }
- }
- while (l <= r);
- quickSort(array, links, r);
- quickSort(array, l, rechts);
- }
- }
- public static void shakerSort(int[] a) {
- int begin = -1;
- int ende = a.length - 2;
- int temp, i;
- boolean vertauscht;
- do {
- vertauscht = false;
- ++begin;
- for (i = begin; i<=ende; ++i) {
- if (a[i] > a[i+1]) {
- temp = a[i];
- a[i] = a[i+1];
- a[i+1] = temp;
- vertauscht = true;
- }
- }
- if (!vertauscht) break;
- vertauscht = false;
- --ende;
- for (i = ende; i >= begin; i--) {
- if (a[i] > a[i+1]) {
- temp = a[i];
- a[i] = a[i+1];
- a[i+1] = temp;
- vertauscht = true;
- }
- }
- } while (vertauscht);
- }
- public static void bubbleSort(int[] x) {
- boolean unsortiert = true;
- int temp;
- while (unsortiert){
- unsortiert = false;
- for (int i=0; i < x.length-1; i++)
- if (x[i] > x[i+1]) {
- temp = x[i];
- x[i] = x[i+1];
- x[i+1] = temp;
- unsortiert = true;
- }
- }
- }
- public static int[] r(int l, int n)
- {
- int[] a = new int[l];
- Random g = new Random();
- for (int i = 0; i < a.length; i++)
- a[i] = g.nextInt(n);
- return a;
- }
- public static void main(String[] args) {
- long timeStart, timeEnd;
- int elements = 1000;
- int[] arr1 = r(elements, elements*2);
- int[] arr2 = arr1.clone();
- int[] arr3 = arr1.clone();
- //----
- System.out.println("arr length: " + arr1.length + "\n");
- System.out.println("BubbleSort :");
- timeStart = System.currentTimeMillis();
- bubbleSort(arr1);
- //System.out.println(Arrays.toString(arr1));
- timeEnd = System.currentTimeMillis();
- System.out.println("Verlaufszeit des Sortierverfahrens: " + (timeEnd - timeStart) + " Millisekunden!");
- System.out.println("");
- System.out.println("ShakerSort :");
- timeStart = System.currentTimeMillis();
- shakerSort(arr2);
- //System.out.println(Arrays.toString(arr2));
- timeEnd = System.currentTimeMillis();
- System.out.println("Verlaufszeit des Sortierverfahrens: " + (timeEnd - timeStart) + " Millisekunden!");
- System.out.println("");
- System.out.println("QuickSort (rec.) :");
- timeStart = System.currentTimeMillis();
- quickSort(arr3, 0, arr3.length -1);
- //System.out.println(Arrays.toString(arr3));
- timeEnd = System.currentTimeMillis();
- System.out.println("Verlaufszeit des Sortierverfahrens: " + (timeEnd - timeStart) + " Millisekunden!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment