Advertisement
cr34tiv3

Untitled

Oct 1st, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.62 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3.  
  4. import javax.swing.JButton;
  5. import javax.swing.JFrame;
  6. import javax.swing.JLabel;
  7. import javax.swing.JTextField;
  8. import java.awt.event.*;
  9. import java.awt.*;
  10. import java.util.Random;
  11.  
  12.  
  13.  
  14.  
  15.  
  16. public class GUI extends JFrame {
  17.  
  18.     private int zahl;
  19.     JButton Setzen;
  20.     JButton BubbleSort;
  21.     JButton SelectionSort;
  22.     JButton InsertionSort;
  23.     JButton QuickSort;
  24.     JLabel Label;
  25.    
  26.     JTextField Eingabe;
  27.     JTextField Anzahl;
  28.    
  29.     int Array[] = new int[200];
  30.     int bArray[] = new int[5];
  31.     Random rnd = new Random();
  32.     int anz = 0;
  33.     int anz2 = 0;
  34.    
  35.     public GUI() {
  36.        
  37.         //setVisible(true);
  38.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.         setSize(650, 400);
  40.         setTitle("Sortieren");
  41.         setLayout(null);
  42.        
  43.         Setzen = new JButton("Setzen");
  44.         Setzen.setBounds(10, 100, 100, 25);
  45.         Setzen.addActionListener(new UnserListener());
  46.         add(Setzen);
  47.        
  48.         BubbleSort = new JButton("BubbleSort");
  49.         BubbleSort.setBounds(10, 150, 100, 25);
  50.         BubbleSort.addActionListener(new UnserListener());
  51.         add(BubbleSort);
  52.        
  53.         SelectionSort = new JButton("SelectionSort");
  54.         SelectionSort.setBounds(10, 200, 100, 25);
  55.         SelectionSort.addActionListener(new UnserListener());
  56.         add(SelectionSort);
  57.        
  58.         InsertionSort = new JButton("InsertionSort");
  59.         InsertionSort.setBounds(10, 250, 100, 25);
  60.         InsertionSort.addActionListener(new UnserListener());
  61.         add(InsertionSort);
  62.        
  63.         QuickSort = new JButton("QuickSort");
  64.         QuickSort.setBounds(10, 300, 100, 25);
  65.         QuickSort.addActionListener(new UnserListener());
  66.         add(QuickSort);
  67.        
  68.         Eingabe = new JTextField ();
  69.         Eingabe.setBounds(100, 30, 300, 30);
  70.         add(Eingabe);
  71.        
  72.         Anzahl = new JTextField();
  73.         Anzahl.setBounds(150, 110, 300, 30);
  74.         add(Anzahl);
  75.        
  76.         Label = new JLabel("Anzahl der Vergleiche/Vertauschungen");
  77.         Label.setBounds(150, 70, 300, 30);     
  78.         add(Label);
  79.        
  80.        
  81.        
  82.     }
  83.    
  84.     public class UnserListener implements ActionListener{
  85.  
  86.          public void actionPerformed(ActionEvent arg0) {
  87.            
  88.              if(arg0.getSource()==Setzen)
  89.              {
  90.                  //Belege();
  91.                  String text = "";
  92.                  for(int i=0; i < Array.length; i++)
  93.                  {
  94.                          Array[i] = rnd.nextInt(1000);
  95.                          text += Array[i]+" ";
  96.                  }
  97.                  Eingabe.setText(text);
  98.                  anz=0;
  99.                  anz2=0;
  100.              }
  101.              else if (arg0.getSource()==BubbleSort)
  102.              {
  103.                  BubbleSort();
  104.                  String text = "";
  105.                  //String text2 = anz + " "+ anz2;
  106.                  anz = 0;
  107.                  anz2 = 0;
  108.                  for(int i=0; i < Array.length; i++)
  109.                  {
  110.                          //Array[i] = rnd.nextInt(150);
  111.                          text += Array[i]+" ";
  112.                  }
  113.                  Eingabe.setText(text);
  114.                  //Anzahl.setText(text2);
  115.              }
  116.              else if (arg0.getSource()==SelectionSort)
  117.              {
  118.                  SelectionSort();
  119.                  String text = "";
  120.                  for(int i=0; i < Array.length; i++)
  121.                  {
  122.                          //Array[i] = rnd.nextInt(150);
  123.                          text += Array[i]+" ";
  124.                  }
  125.                  Eingabe.setText(text);
  126.              }
  127.            
  128.              else if (arg0.getSource()==InsertionSort)
  129.              {
  130.                  isort();
  131.                  String text = "";
  132.                  for(int i=0; i < Array.length; i++)
  133.                  {
  134.                          //Array[i] = rnd.nextInt(150);
  135.                          text += Array[i]+" ";
  136.                  }
  137.                  Eingabe.setText(text);
  138.              }
  139.              
  140.              else if (arg0.getSource()==QuickSort)
  141.              {
  142.                  quicksort(0,Array.length-1);
  143.                  String text = "";
  144.                  String text2 = "Vergleiche:"+ anz + " "+"Zuweisungen:"+ anz2;
  145.                  for(int i=0; i < Array.length; i++)
  146.                  {
  147.                          //Array[i] = rnd.nextInt(150);
  148.                          text += Array[i]+" ";
  149.                  }
  150.                  Eingabe.setText(text);
  151.                  Anzahl.setText(text2);
  152.              }
  153.             }
  154.        
  155.     }
  156.  
  157.    
  158.    
  159.     public void Belege(){
  160.        
  161.         for(int i=0;i<=Array.length-1;i++)
  162.         {
  163.             Array[i]=rnd.nextInt(100);
  164.         }
  165.        
  166.        
  167.     }
  168.    
  169.    
  170.     public void BubbleSort(){
  171.         for(int x1=0; x1<Array.length-1;x1++){  // Die äußere for-Schleife definiert die Anzahl der Durchgänge
  172.         for(int x=0;x<Array.length-1;x++)       // Die innere for-Schleife definiert den einzelnen Durchgang
  173.         {
  174.             if(Array[x]>Array[x+1])             // Die if-Abfrage, fragt ab welche Stelle der Array kleiner ist
  175.             {
  176.                 int z=Array[x+1];               // Hier wird getauscht
  177.                 Array[x+1]=Array[x];
  178.                 Array[x]=z;
  179.                 anz = anz+1;
  180.                 anz2 = anz2+1;
  181.             }
  182.             else
  183.             {
  184.                 anz = anz +1;
  185.                 }
  186.         }}
  187.     }
  188.    
  189.     public void SelectionSort() {
  190.         for (int i=0; i<Array.length-1; i++) {
  191.             int minimum = i;                        // Hier wird ein minimum definiert, Das minimum wird mit der kleinsten Arraystelle beschrieben.
  192.             for (int j=i+1; j<Array.length; j++) {  // "j=i+1" steht dafür das im nächsten Durchgang die erste Stelle nichtmehr genommen werden soll.
  193.                 if (Array[minimum] > Array[j]) {
  194.                     minimum = j;                    // Das Minimum wird mit der kleineren Stelle beschrieben.
  195.                 }
  196.             }
  197.             if (minimum != i) {                     // Die if-Abfrage steht dafür das nur getauscht wird, wenn das minimum und i ungleich sind
  198.                 int temp = Array[i];                // TAUSCH
  199.                 Array[i] = Array[minimum];
  200.                 Array[minimum] = temp;
  201.             }
  202.         }
  203.     }
  204.        
  205.    
  206.        
  207.  
  208.         public void isort()
  209.         {
  210.             this.Array=Array;
  211.             zahl=Array.length;
  212.             insertionsort();
  213.         }
  214.  
  215.         private void insertionsort()
  216.         {
  217.             int x, y, z;
  218.             for (x=1; x<zahl; x++)
  219.             {
  220.                 y=x;
  221.                 z=Array[y];
  222.                 while (y>0 && Array[y-1]>z)
  223.                 {
  224.                     Array[y]=Array[y-1];
  225.                     y--;
  226.                 }
  227.                 Array[y]=z;
  228.             }
  229.         }
  230.  
  231.    
  232.        
  233.        /* public void mergesort(int low, int high){
  234.            
  235.             if(low<high)
  236.             {
  237.                 int medium = (low+high)/2;
  238.                 mergesort (low, medium);
  239.                 mergesort (medium+1, high);
  240.                 merge (low, medium, high);             
  241.             }  
  242.            
  243.         }
  244.        
  245.      
  246.         void merge(int low, int medium, int high)
  247.         {
  248.             int i, j, k;           
  249.             for (i=low; i<=high; i++)
  250.                 bArray[i]=Array[i];
  251.  
  252.             i=low; j=medium+1; k=low;
  253.             while (i<=medium && j<=high)
  254.                 if (bArray[i]<=bArray[j])
  255.                     Array[k++]=bArray
  256.                     [i++];
  257.                 else
  258.                     Array[k++]=bArray[j++];
  259.             while (i<=medium)
  260.                 Array[k++]=bArray[i++];
  261.         }*/
  262.  
  263.  
  264.         public int[] quicksort(int links, int rechts)
  265.          {
  266.              int i=links;
  267.              int j=rechts;
  268.              anz2 = anz2 +2;
  269.              if(i < j)
  270.              {
  271.                  anz = anz+1;
  272.              while(i < j)
  273.              {
  274.                  anz = anz+1;
  275.                  int pivot = pivotelement(links, rechts);
  276.                  while(Array[i] < pivot)
  277.                 {
  278.                     i++;
  279.                     anz = anz +1;
  280.                 }
  281.                 while(Array[j] > pivot)
  282.                 {
  283.                     j--;
  284.                     anz = anz+1;
  285.                 }
  286.              
  287.                 if(i <= j)
  288.                 {
  289.                     int temp=Array[i];
  290.                     Array[i]=Array[j];
  291.                     Array[j] = temp;
  292.                     anz2 = anz2 +3;
  293.                     i++; j--;
  294.                 }
  295.              }
  296.              quicksort(links, j);
  297.              quicksort(i, rechts);
  298.              }
  299.              return Array;
  300.          }
  301.          
  302.          public int pivotelement(int links, int rechts)
  303.          {
  304.              int temp=0;
  305.              for(int i=links; i < rechts; i++)
  306.              {
  307.                  temp+=Array[i];
  308.              }
  309.              int pivot = temp/(rechts-links);
  310.              return pivot;
  311.          }
  312.  
  313.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement