package testate; import java.applet.*; import java.awt.*; import java.awt.event.*; import java.lang.Thread; public class SortTestApp extends java.applet.Applet implements ActionListener, ItemListener, Runnable { int counter=0; int xOffset, yOffset; int[] array, backup; Graphics g; Thread t1; boolean change=false; boolean stopThread=false; // Animationsgeschwindigkeit: int delay=20; Choice speed; String[] animation={"1","5","10","20","50","100"}; // Sortieralgorithmen: int sortCode=0; Choice sort; String[] sortAlg={"BubbleSort", "InsertionSort", "MergeSort", "QuickSort", "SelectionSort"}; // Feldgroesse: int num=500; Choice feldSize; String[] feldGroesse={"50","100","200","300","400","500"}; // Maximalwert des Feldes: int max=500; Choice feldMax; String[] feldMaximum={"50","100","200","300","400","500"}; // Eigenschaft des Feldes: int typ=0; Choice feldTyp; String[] feldSortierung={"unsortiert", "absteigend", "aufsteigend"}; // Eigenschaft des Startknopfes: Button start; String[] startKnopf={"Start","Stop"}; // Grafische Feldausgabe: public void drawArray(Graphics g) { drawArray(g, -1, -1, -1); } public void drawArray(Graphics g, int wert) { drawArray(g, wert, -1, -1); } public void drawArray(Graphics g, int wert1, int wert2) { drawArray(g, wert1, wert2, -1); } public void drawArray(Graphics g, int wert1, int wert2, int wert3) { for (int index=0; index=0)&&(index==wert1)) { g.setColor(Color.red); } else if ((wert2>=0)&&(index==wert2)) { g.setColor(Color.blue); } else if ((wert3>=0)&&(index==wert3)) { g.setColor(Color.green); } else { g.setColor(Color.black); } g.fillOval(xOffset+array[index], (yOffset+500-index), 3, 3); } // Kopie für Kontrolle ob "veraltet": backup=copyArray(array); // Pause: try { Thread.sleep(delay); } catch (InterruptedException e) { } counter++; } // Ausgabefeld: public void paint(Graphics g) { // Statuszeile: getAppletContext().showStatus("Maximum: "+max+" Anzahl: "+num+ " Feldtyp: "+feldSortierung[typ]+" Geschwindigkeit: "+delay+ " Sortieralgorithmus: "+sortAlg[sortCode]); // Ausgabeposition bestimmen: getOffsets(); // Ausgabefenster: g.drawRect(xOffset-4,yOffset-4,508,508); g.setColor(Color.lightGray); g.fillRect(xOffset-3,yOffset-3,507,507); drawArray(g); } // sorgt fuer die Zentrierung der Ausgabe: void getOffsets(){ xOffset=(getSize().width - 508) / 2; yOffset=((getSize().height - 508) / 2) - 10; } // Appletinitialisierung: public void init() { // Größe des Bildschirmes bei Start des Programms: resize(600, 650); // vordefiniertes Layout: setLayout(new BorderLayout() ); Panel p1=new Panel(); p1.setBackground(Color.white); p1.setLayout(new FlowLayout(FlowLayout.CENTER)); add("North", p1); // Maximum des zu sortierenden Feldes auswählen: feldMax=new Choice(); for (int index=0; index0 && (array[marker-1]>temp)) { array[marker]=array[marker-1]; marker--; drawArray(g, marker); if (stopThread) break; } array[marker]=temp; drawArray(g); //Externer Abbruch if (stopThread) break; } } public void selectionSort(int[] array) { //SelectionSort Graphics g = this.getGraphics(); int laenge=array.length; int temp; for (int index1=0; index1array[index+1]) { temp=array[index+1]; array[index+1]=array[index]; array[index]=temp; drawArray(g); change=true; } //Externer Abbruch if (stopThread) { change=false; break; } } } } public void quickSort(int[] array) { //QuickSort quickSort2(array, 0, array.length-1); } public void quickSort2(int[] array, int links, int rechts) { Graphics g = this.getGraphics(); int left=links, right=rechts; int pivot=array[(links+rechts)/2], temp; do { //Tauschpartner links suchen: while (array[left]pivot) right--; //Vertauschen: if (left<=right) { temp=array[left]; array[left]=array[right]; array[right]=temp; left++; right--; drawArray(g, (links+rechts)/2, links, rechts); //Externer Abbruch if (stopThread) { left=links=rechts=right=0; break; } } } while (!(left>right)); if (linksmitte; index3--) { array2[index1++]=array1[index3]; } index1=links; index2=0; index3=laenge; while (index2<=index3) { if (array2[index2]<=array2[index3]) array1[index1++]=array2[index2++]; else array1[index1++]=array2[index3--]; drawArray(g, links, rechts, mitte); //Externer Abbruch if (stopThread) break; } } }