fork download
  1. #include <stdio.h>
  2. int main() {
  3. int arr[] = {60,33,44,55,69,33,75};
  4. int n = sizeof(arr) / sizeof(arr[0]);
  5. for (int i = n / 2 - 1; i >= 0; i--) {
  6. int largest = i;
  7. int l = 2 * i + 1;
  8. int r = 2 * i + 2;
  9. if (l < n && arr[l] > arr[largest]) {
  10. largest = l;
  11. }
  12. if (r < n && arr[r] > arr[largest]) {
  13. largest = r;
  14. }
  15. if (largest != i) {
  16. int temp = arr[i];
  17. arr[i] = arr[largest];
  18. arr[largest] = temp;
  19. i = n;
  20. }
  21. }
  22.  
  23. printf("Heap before deleting root:\n");
  24. for (int i = 0; i < n; i++) {
  25. printf("%d ", arr[i]);
  26. }
  27. printf("\n");
  28. if (n > 0) {
  29. arr[0] = arr[n - 1];
  30. n--;
  31. int i = 0;
  32. while (i < n) {
  33. int largest = i;
  34. int l = 2 * i + 1;
  35. int r = 2 * i + 2;
  36. if (l < n && arr[l] > arr[largest]) {
  37. largest = l;
  38. }
  39. if (r < n && arr[r] > arr[largest]) {
  40. largest = r;
  41. }
  42. if (largest != i) {
  43. int temp = arr[i];
  44. arr[i] = arr[largest];
  45. arr[largest] = temp;
  46. i = largest;
  47. } else {
  48. break;
  49. }
  50. }
  51. }
  52. printf("Heap after deleting root:\n");
  53. for (int i = 0; i < n; i++) {
  54. printf("%d ", arr[i]);
  55. }
  56. printf("\n");
  57.  
  58. return 0;
  59. }
Success #stdin #stdout 0.02s 25460KB
stdin
Standard input is empty
stdout
#include <stdio.h>
int main() {
    int arr[] = {60,33,44,55,69,33,75}; 
    int n = sizeof(arr) / sizeof(arr[0]);
    for (int i = n / 2 - 1; i >= 0; i--) {
        int largest = i; 
        int l = 2 * i + 1; 
        int r = 2 * i + 2;
        if (l < n && arr[l] > arr[largest]) {
            largest = l;
        }
        if (r < n && arr[r] > arr[largest]) {
            largest = r;
        }
        if (largest != i) {
            int temp = arr[i];
            arr[i] = arr[largest];
            arr[largest] = temp;
            i = n;
        }
    }

    printf("Heap before deleting root:\n");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    if (n > 0) {
        arr[0] = arr[n - 1];
        n--; 
        int i = 0; 
        while (i < n) {
            int largest = i; 
            int l = 2 * i + 1; 
            int r = 2 * i + 2;
            if (l < n && arr[l] > arr[largest]) {
                largest = l;
            }
            if (r < n && arr[r] > arr[largest]) {
                largest = r;
            }
            if (largest != i) {
                int temp = arr[i];
                arr[i] = arr[largest];
                arr[largest] = temp;
                i = largest; 
            } else {
                break; 
            }
        }
    }
    printf("Heap after deleting root:\n");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}