Array sotieren |
|
Es funktioniert und auch auf die Weise, auf die es gefordert war
Vorschläge zu Verbesserung der Lesbarkeit:
Die Funktion von z ist nicht sofort klar, dafür findet man sicherlich einen besseren Namen (z.B. indexMin).
Wenn das Minimum bei a[i] ist (Zeile 7), dann würde ich z auch auf i setzen, statt auf 0. In Zeile 14 musst du dann auf z != i prüfen.
__________________ Syntax Highlighting fürs Board (Link)
|
|
30.01.2015 08:12 |
|
|
|
Zitat: |
Die Tests soweit erfolgreich |
wirklich? Das Gegenbeispiel hast du ja gleich mitgeliefert: in deinem Beispiel wird eine 4 zur 0.
__________________ Syntax Highlighting fürs Board (Link)
|
|
30.01.2015 19:02 |
|
|
neuling96 unregistriert
|
|
Zitat: |
Original von eulerscheZahl
Zitat: |
Die Tests soweit erfolgreich |
wirklich? Das Gegenbeispiel hast du ja gleich mitgeliefert: in deinem Beispiel wird eine 4 zur 0. |
Ach stimmt
Das ist mir gestern aufgefallen, dass wenn ein Element mehrmals vorkommt, das es dann nicht aufgeht,
allerdings bin dann davon ausgegangen, dass immer mit arrays gearbeitet, in denen ein Element einmal vorkommt
|
|
30.01.2015 19:14 |
|
|
neuling96 unregistriert
|
|
probiert und geht, allerdings sehr unschön der code
code: |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
|
public class SelectionSort{
public static void sort(int[] a){
int [] x= new int [a.length];
for(int i =0; i<a.length; i++){
int z=0;
int w=0;
for(int y =0; y<a.length; y++){
if(a[i]>a[y]){
z+=1;
}
if(a[i]== a[y]){
w++;
}
}
x[z]=a[i];
if(w!=0){
for(int p=1; p<= w-1; p++){
x[z+p]=a[i];
}
}
}
for(int i =0; i<a.length; i++){
a[i]=x[i];
}
}
public static void main (String[] args){
int [] y= {0,5,1,6,-8,4,4,4};
sort(y);
for (int i=0; i< y.length;i++){
System.out.println(y[i]);
}
}
}
|
|
|
|
30.01.2015 19:41 |
|
|
|
Meine Testfälle gehen auch.
Alternativlösung:
code: |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
|
public static void sort(int[] a) {
int[] x = new int[a.length];
for (int i = 0; i < a.length; i++) {
if (a[i] == 0) { // es stehen sowieso überall 0er
continue;
}
int z = 0;
for (int y = 0; y < a.length; y++) {
if (a[i] > a[y]) {
z += 1;
}
}
while (x[z] != 0) { //Feld schon belegt, gehe 1 weiter
z++;
}
x[z] = a[i];
}
for (int i = 0; i < a.length; i++) {
a[i] = x[i];
}
} |
|
__________________ Syntax Highlighting fürs Board (Link)
|
|
30.01.2015 19:54 |
|
|
neuling96 unregistriert
|
|
deine codes sind immer super kompakt^^
dieser code geht auch, bissal kompakter als mein alter code
code: |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
|
public class SelectionSort{
public static void sort(int[] a){
int [] x= new int [a.length];
for(int i =0; i<a.length; i++){
int z=0;
int w=0;
for(int y =0; y<a.length; y++){
if(a[i]>a[y]){
z+=1;
}
if(a[i]== a[y]){
w++;
}
}
while (w>0){
x[z+w-1]=a[i];
w--;
}
}
for(int i =0; i<a.length; i++){
a[i]=x[i];
}
}
public static void main (String[] args){
int [] y= {4,4,-5,-5,4,7,4,8,125,-125};
sort(y);
for (int i=0; i< y.length;i++){
System.out.println(y[i]);
}
}
}
|
|
|
|
30.01.2015 21:17 |
|
|
|