Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Kursovaa2;
- import java.util.Arrays;
- public class BubbleSortVsQuckSort2 {
- public static void main(String[] args) {
- int[] arr = new int[]{3, 1, 5, 9, 2, 7};
- System.out.println(Arrays.toString(bubbleSort(arr)));
- System.out.println(Arrays.toString(sort(arr, 0, arr.length - 1)));
- /*
- Quick heapSort has better average time complexity (O(n log(n)))
- but Bubble heapSort has better space complexity O(1)
- */
- }
- static int partition(int arr[], int low, int high) {
- int pivot = arr[high];
- int i = (low - 1); // index of smaller element
- for (int j = low; j < high; j++) {
- // If current element is smaller than or
- // equal to pivot
- if (arr[j] <= pivot) {
- i++;
- // swap arr[i] and arr[j]
- int temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- }
- // swap arr[i+1] and arr[high] (or pivot)
- int temp = arr[i + 1];
- arr[i + 1] = arr[high];
- arr[high] = temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement