• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

QuickSort

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote a QuickSort but it just works sometimes, and i cant find the bug.
can you tell me whats wrong?

public class Probe
{
public static void main (String []args){

int []feld = new int [10];
feld [0]=8;
feld [1]=3;
feld [2]=8;
feld [3]=1;
feld [4]=12;
feld [5]=4;
feld [6]=2;
feld [7]=11;
feld [8]=5;
feld [9]=9;


//qsort(feld, feld.length-1, 0);
qsort(feld,4,0);

for(int g=0; g<feld.length; g++)
System.out.println(feld[g]);

}

public static void qsort(int[]feld, int o , int u){

int pivot= feld[(o+u)/2];
int pindex=(o+u)/2;
int exchange=0;
int re=o;
int li=u;

while (o>u)
{
while (feld[o]>pivot && o>pindex)
o--;

while (feld[u]<pivot &&u <pindex)
u++;


if (o!=u)
{ exchange = feld[o];
feld[o]=feld[u];
feld[u]=exchange;


if (pindex==o)
pindex=u;

if (pindex==u)
pindex=o;
}
}
/* if (re- pindex>1)
qsort (feld, re, pindex+1);
if (pindex-li>1)
qsort(feld, pindex-1,li);*/

}}
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not really sure I know what I'm doing here, but could it be that in:

int pivot= feld[(o+u)/2];
int pindex=(o+u)/2;

You are loosing precision while (o+u) is being converted to an int.

 
reply
    Bookmark Topic Watch Topic
  • New Topic