diff --git a/Added Heapsort Program in Java b/Added Heapsort Program in Java new file mode 100644 index 0000000..06ed95c --- /dev/null +++ b/Added Heapsort Program in Java @@ -0,0 +1,45 @@ +public class HeapSort { + public static void main(String[] args) { + int[] arr = {12, 11, 13, 5, 6, 7}; + System.out.println("Original array:"); + printArray(arr); + heapSort(arr); + System.out.println("\nSorted array:"); + printArray(arr); + } + public static void heapSort(int[] arr) { + int n = arr.length; + for (int i = n / 2 - 1; i >= 0; i--) { + heapify(arr, n, i); + } + for (int i = n - 1; i > 0; i--) { + int temp = arr[0]; + arr[0] = arr[i]; + arr[i] = temp; + heapify(arr, i, 0); + } + } + public static void heapify(int[] arr, int n, int i) { + int largest = i; + int left = 2 * i + 1; + int right = 2 * i + 2; + if (left < n && arr[left] > arr[largest]) { + largest = left; + } + if (right < n && arr[right] > arr[largest]) { + largest = right; + } + if (largest != i) { + int swap = arr[i]; + arr[i] = arr[largest]; + arr[largest] = swap; + heapify(arr, n, largest); + } + } + public static void printArray(int[] arr) { + for (int value : arr) { + System.out.print(value + " "); + } + System.out.println(); + } +}