Advertisement
16225

3,1 Ралица

Jun 16th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. package Kursovaa2;
  2. import java.util.Arrays;
  3. public class BubbleSortVsQuckSort2 {
  4.     public static void main(String[] args) {
  5.  
  6.         int[] arr = new int[]{3, 1, 5, 9, 2, 7};
  7.         System.out.println(Arrays.toString(bubbleSort(arr)));
  8.         System.out.println(Arrays.toString(sort(arr, 0, arr.length - 1)));
  9.         /*
  10.         Quick heapSort has better average time complexity (O(n log(n)))
  11.         but Bubble heapSort has better space complexity O(1)
  12.          */
  13.     }
  14.     static int partition(int arr[], int low, int high) {
  15.  
  16.         int pivot = arr[high];
  17.         int i = (low - 1); // index of smaller element
  18.         for (int j = low; j < high; j++) {
  19.             // If current element is smaller than or
  20.             // equal to pivot
  21.             if (arr[j] <= pivot) {
  22.                 i++;
  23.                 // swap arr[i] and arr[j]
  24.                 int temp = arr[i];
  25.                 arr[i] = arr[j];
  26.                 arr[j] = temp;
  27.             }
  28.         }
  29.         // swap arr[i+1] and arr[high] (or pivot)
  30.         int temp = arr[i + 1];
  31.         arr[i + 1] = arr[high];
  32.         arr[high] = temp;
  33.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement