Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- import java.awt.event.*;
- import java.awt.*;
- import java.util.Random;
- public class GUI extends JFrame {
- private int zahl;
- JButton Setzen;
- JButton BubbleSort;
- JButton SelectionSort;
- JButton InsertionSort;
- JButton QuickSort;
- JLabel Label;
- JTextField Eingabe;
- JTextField Anzahl;
- int Array[] = new int[200];
- int bArray[] = new int[5];
- Random rnd = new Random();
- int anz = 0;
- int anz2 = 0;
- public GUI() {
- //setVisible(true);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setSize(650, 400);
- setTitle("Sortieren");
- setLayout(null);
- Setzen = new JButton("Setzen");
- Setzen.setBounds(10, 100, 100, 25);
- Setzen.addActionListener(new UnserListener());
- add(Setzen);
- BubbleSort = new JButton("BubbleSort");
- BubbleSort.setBounds(10, 150, 100, 25);
- BubbleSort.addActionListener(new UnserListener());
- add(BubbleSort);
- SelectionSort = new JButton("SelectionSort");
- SelectionSort.setBounds(10, 200, 100, 25);
- SelectionSort.addActionListener(new UnserListener());
- add(SelectionSort);
- InsertionSort = new JButton("InsertionSort");
- InsertionSort.setBounds(10, 250, 100, 25);
- InsertionSort.addActionListener(new UnserListener());
- add(InsertionSort);
- QuickSort = new JButton("QuickSort");
- QuickSort.setBounds(10, 300, 100, 25);
- QuickSort.addActionListener(new UnserListener());
- add(QuickSort);
- Eingabe = new JTextField ();
- Eingabe.setBounds(100, 30, 300, 30);
- add(Eingabe);
- Anzahl = new JTextField();
- Anzahl.setBounds(150, 110, 300, 30);
- add(Anzahl);
- Label = new JLabel("Anzahl der Vergleiche/Vertauschungen");
- Label.setBounds(150, 70, 300, 30);
- add(Label);
- }
- public class UnserListener implements ActionListener{
- public void actionPerformed(ActionEvent arg0) {
- if(arg0.getSource()==Setzen)
- {
- //Belege();
- String text = "";
- for(int i=0; i < Array.length; i++)
- {
- Array[i] = rnd.nextInt(1000);
- text += Array[i]+" ";
- }
- Eingabe.setText(text);
- anz=0;
- anz2=0;
- }
- else if (arg0.getSource()==BubbleSort)
- {
- BubbleSort();
- String text = "";
- //String text2 = anz + " "+ anz2;
- anz = 0;
- anz2 = 0;
- for(int i=0; i < Array.length; i++)
- {
- //Array[i] = rnd.nextInt(150);
- text += Array[i]+" ";
- }
- Eingabe.setText(text);
- //Anzahl.setText(text2);
- }
- else if (arg0.getSource()==SelectionSort)
- {
- SelectionSort();
- String text = "";
- for(int i=0; i < Array.length; i++)
- {
- //Array[i] = rnd.nextInt(150);
- text += Array[i]+" ";
- }
- Eingabe.setText(text);
- }
- else if (arg0.getSource()==InsertionSort)
- {
- isort();
- String text = "";
- for(int i=0; i < Array.length; i++)
- {
- //Array[i] = rnd.nextInt(150);
- text += Array[i]+" ";
- }
- Eingabe.setText(text);
- }
- else if (arg0.getSource()==QuickSort)
- {
- quicksort(0,Array.length-1);
- String text = "";
- String text2 = "Vergleiche:"+ anz + " "+"Zuweisungen:"+ anz2;
- for(int i=0; i < Array.length; i++)
- {
- //Array[i] = rnd.nextInt(150);
- text += Array[i]+" ";
- }
- Eingabe.setText(text);
- Anzahl.setText(text2);
- }
- }
- }
- public void Belege(){
- for(int i=0;i<=Array.length-1;i++)
- {
- Array[i]=rnd.nextInt(100);
- }
- }
- public void BubbleSort(){
- for(int x1=0; x1<Array.length-1;x1++){ // Die äußere for-Schleife definiert die Anzahl der Durchgänge
- for(int x=0;x<Array.length-1;x++) // Die innere for-Schleife definiert den einzelnen Durchgang
- {
- if(Array[x]>Array[x+1]) // Die if-Abfrage, fragt ab welche Stelle der Array kleiner ist
- {
- int z=Array[x+1]; // Hier wird getauscht
- Array[x+1]=Array[x];
- Array[x]=z;
- anz = anz+1;
- anz2 = anz2+1;
- }
- else
- {
- anz = anz +1;
- }
- }}
- }
- public void SelectionSort() {
- for (int i=0; i<Array.length-1; i++) {
- int minimum = i; // Hier wird ein minimum definiert, Das minimum wird mit der kleinsten Arraystelle beschrieben.
- 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.
- if (Array[minimum] > Array[j]) {
- minimum = j; // Das Minimum wird mit der kleineren Stelle beschrieben.
- }
- }
- if (minimum != i) { // Die if-Abfrage steht dafür das nur getauscht wird, wenn das minimum und i ungleich sind
- int temp = Array[i]; // TAUSCH
- Array[i] = Array[minimum];
- Array[minimum] = temp;
- }
- }
- }
- public void isort()
- {
- this.Array=Array;
- zahl=Array.length;
- insertionsort();
- }
- private void insertionsort()
- {
- int x, y, z;
- for (x=1; x<zahl; x++)
- {
- y=x;
- z=Array[y];
- while (y>0 && Array[y-1]>z)
- {
- Array[y]=Array[y-1];
- y--;
- }
- Array[y]=z;
- }
- }
- /* public void mergesort(int low, int high){
- if(low<high)
- {
- int medium = (low+high)/2;
- mergesort (low, medium);
- mergesort (medium+1, high);
- merge (low, medium, high);
- }
- }
- void merge(int low, int medium, int high)
- {
- int i, j, k;
- for (i=low; i<=high; i++)
- bArray[i]=Array[i];
- i=low; j=medium+1; k=low;
- while (i<=medium && j<=high)
- if (bArray[i]<=bArray[j])
- Array[k++]=bArray
- [i++];
- else
- Array[k++]=bArray[j++];
- while (i<=medium)
- Array[k++]=bArray[i++];
- }*/
- public int[] quicksort(int links, int rechts)
- {
- int i=links;
- int j=rechts;
- anz2 = anz2 +2;
- if(i < j)
- {
- anz = anz+1;
- while(i < j)
- {
- anz = anz+1;
- int pivot = pivotelement(links, rechts);
- while(Array[i] < pivot)
- {
- i++;
- anz = anz +1;
- }
- while(Array[j] > pivot)
- {
- j--;
- anz = anz+1;
- }
- if(i <= j)
- {
- int temp=Array[i];
- Array[i]=Array[j];
- Array[j] = temp;
- anz2 = anz2 +3;
- i++; j--;
- }
- }
- quicksort(links, j);
- quicksort(i, rechts);
- }
- return Array;
- }
- public int pivotelement(int links, int rechts)
- {
- int temp=0;
- for(int i=links; i < rechts; i++)
- {
- temp+=Array[i];
- }
- int pivot = temp/(rechts-links);
- return pivot;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement