sortowanie stogowe heap sort

0
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;



 public class heap_Sort{
  public static void main(String a[]){
  int i;
  int z = 0;
  int n = Integer.parseInt(getInput());
  int arr[] = new int [n];
  Random rand = new Random();
  for ( i=0; i< n; i++)
     {
       arr[i] = rand.nextInt(255);
     }



  System.out.println("\n  Heap Sort\n---------------\n");
  System.out.println("\n  Unsorted Array\n\n");
  for (i = 0; i < arr.length; i++)
  System.out.print(" "+arr[i]);
  for(i=arr.length; i>1; i--){
  fnSortHeap(arr, i - 1);
  }
  System.out.println("\n  Sorted array\n---------------\n");
  for (i = 0; i < arr.length; i++)
  System.out.print(" "+arr[i]);
  }

  public static void fnSortHeap(int array[], int arr_ubound){
  int i, o, comp, swap ;

  comp = 0;
  swap = 0;
  int lChild, rChild, mChild, root, temp;
  root = (arr_ubound-1)/2;

  for(o = root; o >= 0; o--)

      {
  for(i=root;i>=0;i--)
      {
  lChild = (2*i)+1;
  rChild = (2*i)+2;
  comp+=2;
  if((lChild <= arr_ubound) && (rChild <= arr_ubound))
      {
  if(array[rChild] >= array[lChild])
      mChild = rChild;
  else
      mChild = lChild;
  }
  else{
  if(rChild > arr_ubound)
  mChild = lChild;
  else
  mChild = rChild;
  }

  if(array[i] < array[mChild]){
  temp = array[i];
  array[i] = array[mChild];
  array[mChild] = temp;
  swap++;
  }
  }
  }
  temp = array[0];
  array[0] = array[arr_ubound];
  array[arr_ubound] = temp;



        System.out.println("\nZamiany: " + swap + ", porownania: " + comp + ", zlozonosc: "+ "\n");// złozonsc
    }

     public static String getInput()
    {
        try{
        BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
        String s = bufferRead.readLine();

        return s;
        }
        catch(IOException e)
        {
            e.printStackTrace();
            return "";
        }
    }
}

mam problem gdzie ustawić porównania i podstawienia by je zliczało prawidłowo i współczynnik śreniej złożoności

0

Takie rzeczy to się liczy na kartce.
A liczniki proponuje wstawić tam gdzie wykonujesz te operacje. Tzn jak porównujesz to zliczasz porównania. Wiem, odkrywcze.

1 użytkowników online, w tym zalogowanych: 0, gości: 1