Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import import import import import import import javax.swing.JButton; javax.swing.JFrame; javax.swing.JLabel; javax.swing.

JTextField; java.awt.event.*; java.awt.*; java.util.Random;

public class GUI extends JFrame { JButton Setzen; JButton BubbleSort; JButton SelectionSort; JTextField Eingabe; int Array[] = new int[5]; Random rnd = new Random(); public GUI() { setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500, 300); 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); Eingabe = new JTextField (); Eingabe.setBounds(100, 30, 300, 30); add(Eingabe); } 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(150); text += Array[i]+" "; } Eingabe.setText(text); } else if (arg0.getSource()==BubbleSort) { BubbleSort(); 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()==SelectionSort) { SelectionSort(); String text = ""; for(int i=0; i < Array.length; i++) { //Array[i] = rnd.nextInt(150); text += Array[i]+" "; } Eingabe.setText(text); } } }

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++){ for(int x=0;x<Array.length-1;x++) { if(Array[x]>Array[x+1]) { int z=Array[x+1]; Array[x+1]=Array[x]; Array[x]=z;

} else {} }} } public void SelectionSort() { for (int i=0; i<Array.length-1; i++) { int minimum = i; for (int j=i+1; j<Array.length; j++) { if (Array[minimum] > Array[j]) { minimum = j; } } if (minimum != i) { int temp = Array[i]; Array[i] = Array[minimum]; Array[minimum] = temp; } } } }

You might also like