2017-06-11 1 views
-1

Je dois faire un projet qui génère jusqu'à 100 nombres aléatoires, les montre, puis vous sélectionnez un des types de sortes (sélection, bulle, insertion, shell ..), et il les trie. J'ai déjà fait la partie génératrice, mais j'ai des problèmes avec le premier tri par tri. Il affiche uniquement des zéros à la place de mes nombres générés et triés.Mise en œuvre du tri de sélection dans un projet plus grand

package projekt; 

import java.util.Scanner; 
import java.util.Random; 

public class Projekt { 

    public static void main(String[] args) { 
     int[] tablica; 
     Scanner odczyt = new Scanner(System.in); 

     System.out.println("Give number of elements (max 100): "); 
     int liczbaElementow = odczyt.nextInt(); 
     tablica = new int[liczbaElementow]; 

     if (liczbaElementow <= 100 && liczbaElementow > 0){ 
      System.out.println("Generated:"); 
      Random Generator = new Random(); 
      for (int idx = 1; idx <= liczbaElementow; ++idx){ 
       int randomInt = Generator.nextInt(101); 
       System.out.print(" " +randomInt); 
      } 

      { 
       System.out.println(" "); 
       System.out.print("Choose sorting method (1, 2 lub 3):"); 
      } 
      Scanner odczyt2 = new Scanner(System.in); 
      int ktoresort = odczyt2.nextInt(); 

      switch (ktoresort) { 
       case 1: System.out.println("SORT 1"); 
       { 
        { 
         int min = 0; 

         for(int i = 0;i<liczbaElementow;i++) 
         { 
          min = i; 
          for(int j = i + 1;j<liczbaElementow;j++) 
          { 
           if(tablica[j] < tablica[min]) { min = j;} 
          } 
          int temp = tablica[i]; 
          tablica[i] = tablica[min]; 
          tablica[min] = temp; 
          System.out.println(tablica[i]); 
         } 
        } 
       } 
       break; 
       case 2: System.out.println("SORT 2"); 
       break; 
       case 3: System.out.println("SORT 3"); 
       break; 
       default: System.out.println("incorrect value"); 
       break; 
      } 

      odczyt2.close();  
     } 
     else if (liczbaElementow==0) { 
      System.out.println("entered 0");   
     } 

     else{ 
      System.out.println("value is biger than 100"); 
     } 
    } 
} 
+0

où remplir-vous le tableau? – Ollaw

+0

Bienvenue dans Stack Overflow! J'ai édité votre question pour enlever des lignes supplémentaires et corriger l'indentation. En aparté (pas lié à votre problème, mais puisque vous avez mentionné que vous n'êtes pas un programmeur) vous avez des parenthèses inutiles qui sont rendues plus évidentes par cela, je suggérerais de mettre votre IDE à auto-indent si possible. Aussi, pas besoin de l'argument dans System.out.println ("") ', System.out.println()' va juste imprimer une nouvelle ligne. À votre santé! – whrrgarbl

+0

Merci pour votre réponse. J'ai commencé à écrire ceci dans le bloc-notes classique de Windows, puis mon ami m'a parlé d'éclipse, donc il pourrait y avoir un petit désordre de ce début. Je n'ai jamais vraiment essayé de taper juste() pour créer une nouvelle ligne mais j'ai juste remplacé ceci dans mon code maintenant et cela a fonctionné :) – justme

Répondre

0

Dans votre code, vous jamais ajouté les valeurs générées au TABLICA ici ont la solution de mon ami

public class Projekt { 

public static void main(String[] args) { 


    int[] tablica; 
    Scanner odczyt = new Scanner(System.in); 

    System.out.println("Give number of elements (max 100): "); 
    int liczbaElementow = odczyt.nextInt(); 
    tablica = new int[liczbaElementow]; 


    if (liczbaElementow <= 100 && liczbaElementow > 0){ 

     System.out.println("Generated:"); 
     Random Generator = new Random(); 
     for (int idx = 1; idx <= liczbaElementow; ++idx){ 
      int randomInt = Generator.nextInt(101); 
      System.out.print(" " +randomInt); 
      tablica[idx-1]=randomInt; 


     } 

     { 
      System.out.println(" "); 
      System.out.print("Choose sorting method (1, 2 lub 3):"); 
     } 
     Scanner odczyt2 = new Scanner(System.in); 
     int ktoresort = odczyt2.nextInt(); 

     switch (ktoresort) { 
      case 1: System.out.println("SORT 1"); 
       bubbleSort(tablica); 
      break; 
      case 2: System.out.println("SORT 2"); 
       insertionSort(tablica); 
      case 3: System.out.println("SORT 3"); 
       shelSort(tablica); 
       break; 
      default: System.out.println("incorrect value"); 
       break; 
     } 

     System.out.println(Arrays.toString(tablica)); 
     odczyt2.close(); 
    } 
    else if (liczbaElementow==0) { 
     System.out.println("entered 0"); 
    } 

    else{ 
     System.out.println("value is biger than 100"); 
    } 
} 


public static void bubbleSort(int [ ] num) 
{ 
    int j; 
    boolean flag = true; // set flag to true to begin first pass 
    int temp; //holding variable 

    while (flag) 
    { 
     flag= false; //set flag to false awaiting a possible swap 
     for(j=0; j < num.length -1; j++) 
     { 
      if (num[ j ] < num[j+1]) // change to > for ascending sort 
      { 
       temp = num[ j ];    //swap elements 
       num[ j ] = num[ j+1 ]; 
       num[ j+1 ] = temp; 
       flag = true;    //shows a swap occurred 
      } 
     } 
    } 
} 


public static void insertionSort(int [ ] num) 
{ 
    int j;      // the number of items sorted so far 
    int key;    // the item to be inserted 
    int i; 

    for (j = 1; j < num.length; j++) // Start with 1 (not 0) 
    { 
     key = num[ j ]; 
     for(i = j - 1; (i >= 0) && (num[ i ] < key); i--) // Smaller values are moving up 
     { 
      num[ i+1 ] = num[ i ]; 
     } 
     num[ i+1 ] = key; // Put the key in its proper location 
    } 
} 


public static void shelSort(int[] array) { 
    int inner, outer; 
    int temp; 

    int h = 1; 
    while (h <= array.length/3) { 
     h = h * 3 + 1; 
    } 
    while (h > 0) { 
     for (outer = h; outer < array.length; outer++) { 
      temp = array[outer]; 
      inner = outer; 

      while (inner > h - 1 && array[inner - h] >= temp) { 
       array[inner] = array[inner - h]; 
       inner -= h; 
      } 
      array[inner] = temp; 
     } 
     h = (h - 1)/3; 
    } 
} 

}

+0

Merci beaucoup! Je ne sais pas pourquoi je n'ai pas ajouté de valeurs à tablica mais c'est logique maintenant. Je vais essayer d'ajouter trier la sélection une fois de plus maintenant et je reviendrai si j'ai encore des problèmes :) – justme

+0

Si cette réponse a résolu votre question, s'il vous plaît envisager de l'accepter en cliquant sur la coche. Ceci indique à la communauté plus large que vous avez trouvé une solution et donne une certaine réputation à la fois le répondeur et vous-même. Il n'y a aucune obligation de le faire – urag

+0

Je voulais juste laisser cela ouvert pendant un moment avant que je complète l'écriture (si j'avais des questions supplémentaires). J'ai ajouté tri de sélection et tout fonctionne si je n'accepte maintenant – justme