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

HBase RegionServer abort
How to invoke method of an object whose reference id is stored in String? [duplicate]
Validate request using GRPC server interceptor
Stop words removal method in java not working
listview items sets to default once scrolled
What are possible ways to access activity's or fragment's onRequestPermissionsResult from other classes
Exception in receiving hbase credentials oozie
“Script_name” is not generated after making a .rs file (in android source code)
Remove contents of Hashtable or just instantiate a new Hashtable?
Maven project not building
On Touch Method SurfaceView Android doesn't work
Data structure to represent Teams and Players
My jTree does not show the nodes
Filtering a 'null' ID in Talend. Using a tFilterRow
How can I turn a raw sql with many to many relationship into a spring java criteria
Apache Tika Exporting Files

Categories

HOME
cookies
ngrx
mfc
homebrew
objectgears
cplex
android-4.4-kitkat
youtube-dl
node-notifier
add
iggrid
jsprit
caml
graphlab
pc
lldb
swingx
nas
zapier
tokenize
java-7
uninstall
filezilla
spring-mybatis
ping
elasticsearch-net
tinymce-4
yadcf
disassembling
bitbucket-pipelines
neuroscience
code-search-engine
lumberjack
node-gyp
outlook-api
reactive-cocoa-5
.net-4.6.2
parentheses
network-flow
react-scripts
pubmed
tizen-native-app
sqldf
crosswalk-runtime
watchconnectivity
removechild
slicknav
home-directory
multi-level
merge-conflict-resolution
yaws
rhino-servicebus
cloudbees
srand
jms2
retina
pundit
quicklisp
deis
processmodel
iiviewdeckcontroller
skos
umbraco6
tt-news
cdt
socketexception
xojo
aapt
contenttype
dml
picturefill
didselectrowatindexpath
resty-gwt
viadeo
html5-notifications
pyhdf
ivyde
inbox
listings
mylyn
gdata-api
cascalog
labwindows
punbb
eclipse-templates
qt-jambi
external-accessory
deobfuscation
tomcat-valve
cxxtest
nhibernate.search
virtual-functions
noscript
ubuntu-9.04

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