Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #split the unsorted arrays into the 3 sub arrays which are sorted (i*3,i*3+1,i*3+2)
- for (i to (round(n/3)+1))
- #checking for going outside the array size
- if (i*3 < n)
- B[i] <-A[i*3]
- endif
- if (i*3+1 < n)
- C[i] <- A[i*3+1]
- endif
- if (i*3+2 < n)
- D[i] <- A[i*3+2]
- endif
- endfor
- a,b,c,d <- 0;
- #merge method but modified for 3 arrays, works until one array gets used up
- while ( b<=Blength AND c<=Clength AND d<=Dlength )
- if (B[b]<=C[c] AND B[b]<=D[d]){
- A[a] <- B[b]
- b <- b + 1
- else if (C[c]<=B[b] AND C[c]<=D[d]){
- A[a] <- C[C]
- c <- c + 1
- else
- A[a] <- D[d]
- d <- d + 1
- endif
- a <- a + 1
- endwhile
- #one array got used so now we can use the normal Merge method, just need to check which array got used so we know which ones to use
- if (b>Blength)
- while ( c<=Clength AND d<=Dlength )
- if (C[c]<D[d]){
- A[a] <- C[c]
- c <- c + 1
- else
- A[a] <- D[d]
- d <- d + 1
- endif
- a <- a + 1
- endwhile
- else if (c>Clength)
- while ( b<=Blength AND d<=Dlength )
- if (B[b]<D[d]){
- A[a] <- B[b]
- b <- b + 1
- else
- A[a] <- D[d]
- d <- d + 1
- endif
- a <- a + 1
- endwhile
- else
- while ( b<=Blength AND c<=Clength )
- if (B[b]<C[c]){
- A[a] <- B[b]
- b <- b + 1
- else
- A[a] <- C[c]
- c <- c + 1
- endif
- a <- a + 1
- endwhile
- endif
- #now just need to fill the final array with the left array that there are some elements left
- while (b<Blength)
- A[a] <- B[b]
- b <- b + 1
- a <- a + 1
- endwhile
- while (c<Clength)
- A[a] <- C[c]
- c <- c + 1
- a <- a + 1
- endwhile
- while (d<Dlength)
- A[a] <- D[d]
- d <- d + 1
- a <- a + 1
- endwhile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement