Προς το περιεχόμενο

Πρόβλημα με HeapSort σε JAVA !! Help


SpY

Προτεινόμενες αναρτήσεις

Δημοσ.

Παιδιά έχω ένα πρόβλημα με μια μέθοδο στη Java !

Θέλω να υλοποιήσω την μέθοδο Heap Sort για να ταξινομεί έναν πίνακα .

 

>public class HeapSort {
 public static void main(String args[]) {  
   int inputSequence[] = { 5, 7, 3, 2, 1, 
                  8, 9, 6, 10, 4 }; 
     int dimention;  
    dimention = 10; // Enfanisi stoixeiwn gia taxinomisi  
 
   // Enfanisi stoixeiwn arxikou pinaka (ataxinomitou)  
   System.out.print("Arxikos Pinakas :"); 
   for(int i=0; i < dimention; i++) 
     System.out.print(" " + inputSequence[i]);  
   System.out.println();  
   //-----------Telos Enfanisis----------------

//--------Algoritmos HeapSort---------------------------

   

//------------------Telos Algoritmou--------------------

   // Stoixeia pinaka taxinomimena  
   System.out.print("Taxinomimenos Pinakas :");  
   for(int i=0; i < dimention; i++) 
     System.out.print(" " + inputSequence[i]);  
   System.out.println();
  } 
}

 

Και για αλγόριθμο χρησιμοποιώ αυτόν : (χωρις το class HeapSortAlgorithm εννοείται)

>
class HeapSortAlgorithm {
void sort(int inputSequence[]) throws Exception {
	int N = inputSequence.length;
	for (int k = N/2; k > 0; k--) {
		downheap(inputSequence, k, N);
		}
		do {
			int T = inputSequence[0];
			inputSequence[0] = inputSequence[N - 1];
			inputSequence[N - 1] = T;
			N = N - 1;
			downheap(inputSequence, 1, N);
			} while (N > 1);
			 }
 void downheap(int inputSequence[], int k, int N) throws Exception {
 	int T = inputSequence[k - 1];
 	while (k <= N/2) {
 		int j = k + k;
 		if ((j < N) && (inputSequence[j - 1] < inputSequence[j])) {
 			 j++;
 	   }
 	   if (T >= inputSequence[j - 1]) {
 		} else {
 			 inputSequence[k - 1] = inputSequence[j - 1];
 			 k = j;
 		  }
 		}
 	 inputSequence[k - 1] = T;
 }
}

 

Και παίρνω τα παρακάτω errors:

 

non-static method downheap(int[],int,int) cannot be referenced from a static context

downheap(inputSequence, k, N);

 

ο αλγόριθμος μόνος του και το προγραμματάκι για τους πίνακες λειτουργούν! Δεν μπορώ να τα ενώσω, καμια βοήθεια παιδιά

Δημοσ.

Καλησπέρα,

Δεν είδα τον κώδικα ακριβώς, αλλά αν κάνεις τις δύο μεθόδους του αλγορίθμου static, δηλαδή τις δηλώσεις ως εξής:

static void sort(int inputSequence[]) και static void downheap(int inputSequence[], int k, int N), νομίζω πως όλα θα είναι εντάξει.

Δημοσ.

Σε εμένα πάντως το παρακάτω γίνεται compile κανονικά:

>
public class HeapSort {
   
   public static void main(String[] args) {       
   int inputSequence[] = { 5, 7, 3, 2, 1, 
                  8, 9, 6, 10, 4 }; 
     int dimention;  
    dimention = 10; // Enfanisi stoixeiwn gia taxinomisi  
 
   // Enfanisi stoixeiwn arxikou pinaka (ataxinomitou)  
   System.out.print("Arxikos Pinakas :"); 
   for(int i=0; i < dimention; i++) 
     System.out.print(" " + inputSequence[i]);  
   System.out.println();  
   //-----------Telos Enfanisis---------------- 

//--------Algoritmos HeapSort--------------------------- 
   try
   { 
      sort(inputSequence);
   }
   catch (Exception e)
   {}
   

//------------------Telos Algoritmou-------------------- 

   // Stoixeia pinaka taxinomimena  
   System.out.print("Taxinomimenos Pinakas :");  
   for(int i=0; i < dimention; i++) 
     System.out.print(" " + inputSequence[i]);  
   System.out.println(); 
  }
  static void sort(int inputSequence[]) throws Exception { 
     int N = inputSequence.length; 
     for (int k = N/2; k > 0; k--) { 
        downheap(inputSequence, k, N); 
        } 
        do { 
           int T = inputSequence[0]; 
           inputSequence[0] = inputSequence[N - 1]; 
           inputSequence[N - 1] = T; 
           N = N - 1; 
           downheap(inputSequence, 1, N); 
           } while (N > 1); 
   } 
   static void downheap(int inputSequence[], int k, int N) throws Exception          
   { 
      int T = inputSequence[k - 1]; 
      while (k <= N/2) { 
         int j = k + k; 
         if ((j < N) && (inputSequence[j - 1] < inputSequence[j])) { 
             j++; 
         } 
         if (T >= inputSequence[j - 1]) { 
         } else { 
             inputSequence[k - 1] = inputSequence[j - 1]; 
             k = j; 
           } 
         } 
       inputSequence[k - 1] = T; 
   }
}

Δημοσ.

Euxaristw file Sta kanei twra compile apla den mou vgazei ta apotelesmata pou thelw, alla twra kapou ftaiw egw

den eixa grapsei

>
try
   {
      sort(inputSequence);
   }
   catch (Exception e)
   {} 

 

tis entoles try kai catch den tis ixsera! Ti akrivws kanoun ?

Δημοσ.

Αν ένα block κώδικα είναι πιθανό να εγείρει κάποια εξαίρεση, τότε το τοποθετούμε μετά από μία εντολή try ώστε να συλληφθεί η εξαίρεση. Με διαδοχικές εντολές catch κάνουμε χειρισμό των αντιστοίχων εξαιρέσεων που συλλαμβάνουμε. Αυτό σε πολύ αδρές γραμμές.Κοίταξε το reference σου για περισσότερες λεπτομέρειες.

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...