Merge Sort
in
Programming Questions
•
2 years ago
Hey everybody !
My name is Duc, I'm from Germany and I want to use the Mergesort-Algorithm but I have no idea how to.
I already read tons of forums showing me how to do it but just in other languages like Java or C#,++.
But still, I tried to write a code by myself but it doesn't work so I hope u guys can help me in a way. :D
here's the code :
- void mergesort(int [] list)
- {
- if(list.length<=1)
- {
- return;
- }else{
- int [] left = new int[list.length/2];
- int [] right = new int[list.length - left.length];
- return merge(left,right)
- }
- }
- void merge(int [] left,int [] right){
- int x=0;
- int y=0;
- int z=0;
- while( x< left.length && y < right.length)
- {
- if(left[x] <= right[y])
- {
- list[z] = left[x];
- x++;
- }
- else
- {
- left[z] = right[y];
- y++;
- }
- z++;
- }
- for(int i=x; i< left.length;i++)
- {
- list[z] = left[i];
- z++;
- }
- for(int j=y;j<right.length, i++)
- {
- list[z]=right[i];
- z++;
- }
- }
okay , that's it . The code isn't finished yet and I really have no idea where to fix what.
There's another Question:
I wanna use the Mergesortalgorithm in
- int sy,sx,sy1;
- int [] b;
- int [] [] points;
- void setup ()
- {
- frameRate(3);
- size(400,400);
- sy1=height;
- sx=width;
- sy=200;
- points = new int [sx][sy];
- for(int i =0; i < sx; i++) {
- for(int j =199; j < sy; j++) {
- points[(int)random(sx)][(int)j]=(int)random(10);
- }
- }
- }
- void draw() {
- background (255);
- for (int x = 0; x < sx; x=x+1) {
- for (int y = 0; y < sy; y=y+1) {
- if ((points[x][y] == 1) )
- {
- stroke(#682292);
- line(x,y-60,x,y+60);
- }
- if ((points[x][y] == 2) )
- {
- stroke(#982828);
- line(x,y-120,x,y+120);
- }
- if ((points[x][y] == 3) )
- {
- stroke(#292898);
- line(x,y-30,x,y+30);
- }
- if ((points[x][y] == 4) )
- {
- stroke(#2AA044);
- line(x,y-10,x,y+130);
- }
- if ((points[x][y] == 5) )
- {
- stroke(#DBC928);
- line(x,y-160,x,y+160);
- }
- if ((points[x][y] == 6) )
- {
- stroke(#FF7048);
- line(x,y-210,x,y+50);
- }
- if ((points[x][y] == 7) )
- {
- stroke(#48FFB1);
- line(x,y-70,x,y+70);
- }
- if ((points[x][y] == 8) )
- {
- stroke(#000000);
- line(x,y-180,x,y+180);
- }
- if ((points[x][y] == 9) )
- {
- stroke(#151515);
- line(x,y-60,x,y+40);
- }
- if ((points[x][y] == 10) )
- {
- stroke(#383838);
- line(x,y-120,x,y+30);
- }
- }
- }
They're all indexed with numbers of course and I wanna sort them.
Where do I have to put mergesort() in and what do I have to put in these brackets ?
Because points[] isn't enough . It always tells me : "After "[" a character expected"
I hope u guys can help me , I'm looking forward to see results. And sorry for my weird English haha.
1