java


Why doesn't my quicksort implemented in Java work? [closed]


Here's the code:
public class QuickSort {
public static void sort(int[] a) {
sort(a, 0, a.length-1);
}
private static void sort(int[] a, int lo, int hi) {
if(hi <= lo) return;
int j = partition(a, lo, hi);
sort(a, lo, j-1);
sort(a, j+1, hi);
}
private static int partition(int[] a, int lo, int hi) {
int v = a[0];
int i = lo, j = hi+1;
while(true) {
while(a[++i] < v) {
if(i == hi) {
break;
}
}
while(a[--j] > v) {
if(j == lo) {
break;
}
}
if(i >= j) {
break;
} else {
exch(a, i, j);
}
}
exch(a, lo, j); // <- exchange the value of a[lo] and a[j]
return j;
}
}
What's wrong with my code? I know it's a really simple algorithm but just cannot figure out where the problem is.
Please help me, thx in advance.
I guess the partition function of yours has a bug. The vertex (pivot) v that you are selecting should be set to a[lo] and not a[0] everytime.
And, then set i = lo + 1 and j = hi

Related Links

Package projects in eclipse
How to specify display name for web app configured without web.xml
Parse Json in Java not working [closed]
How do I fill an array with randomly chosen characters from a different array
Use static google maps in desktop application
JavaFX: Binding Timeline's duration to a property
MBean in JMX JConsole using Spring without XML
javamail can't read sum mail
Convert sql date to Joda Datetime
2D Arrays in Java Program
DOM Java - Parse right number of attributes from empty nodes
Is there a difference between float[] x and float x[]? [duplicate]
Why there is no pure java djvu encoders?
how to associate a resource to an other using Jena API
Why doesn't this code to change text on Android work?
how to customize Show History eclipse plugin

Categories

HOME
coq
webpack
angular-material
json.net
hp-exstream
hashmap
routes
cmd
datastax-java-driver
swagger-ui
basic
applepay
slurm
gz
serverless-framework
foselasticabundle
jsdoc
jprofiler
ml
cx-freeze
google-search-api
claims-based-identity
rst2pdf
fopen
opentype
bpel
socialengine
phonegap
preg-match-all
.net-4.0
data-manipulation
column-family
caret
cloud-code
wpf-controls
disassembling
neuroscience
unobtrusive-validation
fedex
ruby-on-rails-3.1
gulp-sourcemaps
fakeiteasy
checkboxlist
auto-update
fault
filepicker
zip4j
pango
typescript1.8
domain-model
veracode
rdfs
svmlight
forever
google-perftools
trash
packagist
vhd
maven-tomcat-plugin
prerequisites
jms2
wireshark-dissector
emailrelay
cakephp-3.1
arcanist
website-monitoring
operator-precedence
jsonpickle
jscript.net
tablelayout
id3v2
html-helper
xojo
has-many-through
htmlcleaner
concurrent-collections
farseer
xsockets.net
commoncrypto
aqtime
ora-00911
typoscript2
word-processor
manchester-syntax
hosts-file
custom-backend
nsdatecomponents
xmlspy
yetanotherforum
django-notification

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App