Please correct my iterative merge sort code. The lack of recursion in the code is the main condition.

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Please correct my iterative merge sort code. The lack of recursion in the code is the main condition.

Александр Дубровский
mrg <- function(A,B){
  R <- c()
  while(length(A)>0  length(B)>0){
    if(A[1]<B[1]){
      R <- c(R,A[1])
      A <- A[-1]
    } else{
      R <- c(R,B[1])
      B <- B[-1]
    }
  }
  return(c(R,A,B))
}
msort <- function(A){
  if(length(A)<2){
    return(A)
  } else{
    R <- c()
    W <- c()
    x <- 8
      for(i in 1:length(A)){
        if(i%%2==0){
      R <- c(R,mrg(A[(i-1)],A[i]))
        }
      }
     }
  if((length(A)%%2)==1){
    R <- c(R,A[length(A)])
  }
  for(i in 1:length(R)){
    if(i%%4==0){
      j <- i
      W <- c(W,mrg(R[(j-3):(j-2)],R[(j-1):j]))
    }
  }
  if((length(R)%%4)==3){
    W <- c(W,mrg(R[(j+1):(j+2)],R[(j+3)]))
  }
  if((length(R)%%4)<3 && (length(R)%%4)!=0){
    W <- c(W,R[(j+1):length(R)])
  }
  R <- W
  W <- c()
  while(x<length(R)){
    for(i in 1:length(R)){
      if(i%%x==0){
        j <- i
        W <- c(W,mrg(R[(j-(x-1)):(j-(x%/%2))],R[(j-(x%/%2)+1):j]))
      }
    }
    if((length(R)%%x)>(x%/%2)){
      W <- c(W,mrg(R[(j+1):(j+(x%/%2))],R[(j+(x%/%2)+1):length(R)]))
    }
    if((length(R)%%x)<=(x%/%2) && (length(R)%%x)!=0){
      W <- c(W,R[(j+1):length(R)])
    }
    x <- x*2
    R <- W
    W <- c()
  }
    R <- mrg(R[1:j],R[(j+1):length(R)])
  return(R)
}

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Please correct my iterative merge sort code. The lack of recursion in the code is the main condition.

Henrik Bengtsson-5
Folks on this list, this is my personal opinion, but please refrain from
answering this person's requests. It's pretty clear by now that they are
misusing your good intentions of trying to help people interested in R to
get their homework-like "questions" answered. There are no indications that
this person has even attempted to solve the "problems" themselves. This
smells bad intent to me.

/Henrik

On Mon, Dec 16, 2019, 07:40 Александр Дубровский <[hidden email]>
wrote:

> mrg <- function(A,B){
>   R <- c()
>   while(length(A)>0  length(B)>0){
>     if(A[1]<B[1]){
>       R <- c(R,A[1])
>       A <- A[-1]
>     } else{
>       R <- c(R,B[1])
>       B <- B[-1]
>     }
>   }
>   return(c(R,A,B))
> }
> msort <- function(A){
>   if(length(A)<2){
>     return(A)
>   } else{
>     R <- c()
>     W <- c()
>     x <- 8
>       for(i in 1:length(A)){
>         if(i%%2==0){
>       R <- c(R,mrg(A[(i-1)],A[i]))
>         }
>       }
>      }
>   if((length(A)%%2)==1){
>     R <- c(R,A[length(A)])
>   }
>   for(i in 1:length(R)){
>     if(i%%4==0){
>       j <- i
>       W <- c(W,mrg(R[(j-3):(j-2)],R[(j-1):j]))
>     }
>   }
>   if((length(R)%%4)==3){
>     W <- c(W,mrg(R[(j+1):(j+2)],R[(j+3)]))
>   }
>   if((length(R)%%4)<3 && (length(R)%%4)!=0){
>     W <- c(W,R[(j+1):length(R)])
>   }
>   R <- W
>   W <- c()
>   while(x<length(R)){
>     for(i in 1:length(R)){
>       if(i%%x==0){
>         j <- i
>         W <- c(W,mrg(R[(j-(x-1)):(j-(x%/%2))],R[(j-(x%/%2)+1):j]))
>       }
>     }
>     if((length(R)%%x)>(x%/%2)){
>       W <- c(W,mrg(R[(j+1):(j+(x%/%2))],R[(j+(x%/%2)+1):length(R)]))
>     }
>     if((length(R)%%x)<=(x%/%2) && (length(R)%%x)!=0){
>       W <- c(W,R[(j+1):length(R)])
>     }
>     x <- x*2
>     R <- W
>     W <- c()
>   }
>     R <- mrg(R[1:j],R[(j+1):length(R)])
>   return(R)
> }
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Please correct my iterative merge sort code. The lack of recursion in the code is the main condition.

Jeff Newmiller
The Posting Guide is pretty clear about "no homework" (it is also clear that this is a plain text mailing list). I think the recent replies to the OP have been clear on how this should be approached. I don't know what the OP "intends", but isolated (not replies to the original message) emails that have only code and no question in them are certainly not appropriate in this list.

"dubrovvsskkyy", please adhere to the Posting Guide in future posts. If you are_not_ asking for homework help then please explain why you are ignoring the advice given in previous posts.

On December 16, 2019 8:23:46 AM PST, Henrik Bengtsson <[hidden email]> wrote:

>Folks on this list, this is my personal opinion, but please refrain
>from
>answering this person's requests. It's pretty clear by now that they
>are
>misusing your good intentions of trying to help people interested in R
>to
>get their homework-like "questions" answered. There are no indications
>that
>this person has even attempted to solve the "problems" themselves. This
>smells bad intent to me.
>
>/Henrik
>
>On Mon, Dec 16, 2019, 07:40 Александр Дубровский
><[hidden email]>
>wrote:
>
>> mrg <- function(A,B){
>>   R <- c()
>>   while(length(A)>0  length(B)>0){
>>     if(A[1]<B[1]){
>>       R <- c(R,A[1])
>>       A <- A[-1]
>>     } else{
>>       R <- c(R,B[1])
>>       B <- B[-1]
>>     }
>>   }
>>   return(c(R,A,B))
>> }
>> msort <- function(A){
>>   if(length(A)<2){
>>     return(A)
>>   } else{
>>     R <- c()
>>     W <- c()
>>     x <- 8
>>       for(i in 1:length(A)){
>>         if(i%%2==0){
>>       R <- c(R,mrg(A[(i-1)],A[i]))
>>         }
>>       }
>>      }
>>   if((length(A)%%2)==1){
>>     R <- c(R,A[length(A)])
>>   }
>>   for(i in 1:length(R)){
>>     if(i%%4==0){
>>       j <- i
>>       W <- c(W,mrg(R[(j-3):(j-2)],R[(j-1):j]))
>>     }
>>   }
>>   if((length(R)%%4)==3){
>>     W <- c(W,mrg(R[(j+1):(j+2)],R[(j+3)]))
>>   }
>>   if((length(R)%%4)<3 && (length(R)%%4)!=0){
>>     W <- c(W,R[(j+1):length(R)])
>>   }
>>   R <- W
>>   W <- c()
>>   while(x<length(R)){
>>     for(i in 1:length(R)){
>>       if(i%%x==0){
>>         j <- i
>>         W <- c(W,mrg(R[(j-(x-1)):(j-(x%/%2))],R[(j-(x%/%2)+1):j]))
>>       }
>>     }
>>     if((length(R)%%x)>(x%/%2)){
>>       W <- c(W,mrg(R[(j+1):(j+(x%/%2))],R[(j+(x%/%2)+1):length(R)]))
>>     }
>>     if((length(R)%%x)<=(x%/%2) && (length(R)%%x)!=0){
>>       W <- c(W,R[(j+1):length(R)])
>>     }
>>     x <- x*2
>>     R <- W
>>     W <- c()
>>   }
>>     R <- mrg(R[1:j],R[(j+1):length(R)])
>>   return(R)
>> }
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.