# what is wrong with my quicksort? Classic List Threaded 9 messages Open this post in threaded view
|

## what is wrong with my quicksort?

 Hey guys, I tried to program quicksort like this but somethings wrong. please help         >partition <- function(x, links, rechts){ > > i <- links > j <- rechts > t <- 0 > pivot <- sample(x[i:j],1) > > while(i <= j){ > > while(x[i] <= pivot){ > i = i+1} > > while(x[j] >= pivot){ > j = j-1} > > if( i <= j){ > > t = x[i] > x[i] = x[j] > x[j] = t > > i=i+1 > j=j-1 > > } > print(pivot) > > > } > #Rekursion > > if(links < j){ > partition(x, links, j)} > if(i < rechts){ > partition(x, i, rechts)} > > return(x) > } > > >quicksort <- function(x){ > > > > partition(x, 1, length(x)) >} thx
Open this post in threaded view
|

## Re: what is wrong with my quicksort?

Open this post in threaded view
|

## Re: what is wrong with my quicksort?

Open this post in threaded view
|

## Re: what is wrong with my quicksort?

Open this post in threaded view
|

## Re: what is wrong with my quicksort?

Open this post in threaded view
|

## Re: what is wrong with my quicksort?

Open this post in threaded view
|

## Re: what is wrong with my quicksort?

 In reply to this post by warc Hey again and thanks for all the help this is what i have for now but it still doesn't work, the main problem is the random pivot i think (error in while (x[j] >= pivot) { : Argument has length 0) >partition <- function(x, links, rechts){ > > i <- links > j <- rechts > t <- 0 > pivot <- x[sample((links:rechts),1)] > > > while(i <= j){ > > while(x[i] <= pivot){ >                      i = i+1} > > while(x[j] >= pivot){ > j = j-1} > > if( i <= j){ > > > t = x[i] > x[i] = x[j] > x[j] = t > > i=i+1 > j=j-1 > > } > } > return(pivot) > } > >qsort <- function(x, links, rechts){ > > index <- partition(x, links, rechts) > > if((links < (index+1))&(length(x)>1)){ > qsort(x, links, index+1)} > > > if((index < rechts)&(length(x)>1)){ > qsort(x, index, rechts)} > > return(x) > } > > >quicksort <- function(x){ > > if(length(x) == 0)stop("empty Vector") > > qsort(x, 1, length(x)) >} but whatever i will just keep on trying thank you again