Informatiker Board (http://www.informatikerboard.de/board/index.php)
- Themengebiete (http://www.informatikerboard.de/board/board.php?boardid=1)
-- Praktische Informatik (http://www.informatikerboard.de/board/board.php?boardid=6)
--- Array sotieren (http://www.informatikerboard.de/board/thread.php?threadid=2123)


Geschrieben von neuling96 am 29.01.2015 um 23:43:

  Array sotieren

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:
public class SelectionSort{

public  static void sort(int[] a){

for(int i =0; i<a.length-1; i++){
int z=0;
int min=a[i];
for(int y =i+1; y<a.length; y++){
if(min>a[y]){
min=a[y];
z=y;
}
 }
if(z!=0){
int temp=a[i];
a[i]=min;
a[z]=temp;

}
}


}
 
 

public static void main (String[] args){
int [] y= {0,5,1,6,-8,3,4};
sort(y);
for (int i=0; i< y.length;i++){

System.out.println(y[i]);
}
}

}





Die Test waren erfolgreich, allerdings bin mir nicht ob die Idee wie sie verlangt auch so umgesetzt habe



Geschrieben von eulerscheZahl am 30.01.2015 um 08:12:

 

Es funktioniert und auch auf die Weise, auf die es gefordert war Daumen hoch
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.



Geschrieben von neuling96 am 30.01.2015 um 18:38:

 

vielen dank


weiter gehts

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:
public class Countsort{

public  static void sort(int[] a){
int [] x= new int [a.length];
for(int i =0; i<a.length; i++){
int z=0;

for(int y =0; y<a.length; y++){
if(a[i]>a[y]){
z+=1;

}
 }
x[z]=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,3,4,4};
sort(y);
for (int i=0; i< y.length;i++){

System.out.println(y[i]);
}
}

}




Die Tests soweit erfolgreich, allerdings bin mir nicht ob die Idee wie sie verlangt auch so umgesetzt habe



Geschrieben von eulerscheZahl am 30.01.2015 um 19:02:

 

Zitat:
Die Tests soweit erfolgreich

wirklich? Das Gegenbeispiel hast du ja gleich mitgeliefert: in deinem Beispiel wird eine 4 zur 0.



Geschrieben von neuling96 am 30.01.2015 um 19:14:

 

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 geschockt
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



Geschrieben von eulerscheZahl am 30.01.2015 um 19:28:

 

In der Aufgabenstellung wirst du sogar vor dieser Möglichkeit gewarnt.
Und, irgendwelche Ideen, wie man dem entgegenwirken kann?



Geschrieben von neuling96 am 30.01.2015 um 19:41:

 

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]);
}
}

}





Geschrieben von eulerscheZahl am 30.01.2015 um 19:54:

 

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];
	}
}



Geschrieben von neuling96 am 30.01.2015 um 21:17:

 

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]);
}
}

}




Forensoftware: Burning Board, entwickelt von WoltLab GmbH