Advertisement
Guest User

Untitled

a guest
May 25th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.64 KB | None | 0 0
  1. #split the unsorted arrays into the 3 sub arrays which are sorted (i*3,i*3+1,i*3+2)
  2. for (i to (round(n/3)+1))
  3.  
  4.     #checking for going outside the array size
  5.     if (i*3 < n)
  6.         B[i] <-A[i*3]
  7.     endif
  8.  
  9.     if (i*3+1 < n)
  10.     C[i] <- A[i*3+1]
  11.     endif
  12.  
  13.     if (i*3+2 < n)
  14.     D[i] <- A[i*3+2]
  15.     endif
  16.  
  17. endfor
  18.  
  19. a,b,c,d <- 0;
  20. #merge method but modified for 3 arrays, works until one array gets used up
  21. while ( b<=Blength AND c<=Clength AND d<=Dlength )
  22.         if (B[b]<=C[c] AND B[b]<=D[d]){
  23.             A[a] <- B[b]
  24.             b <- b + 1
  25.         else if (C[c]<=B[b] AND C[c]<=D[d]){
  26.             A[a] <- C[C]
  27.             c <- c + 1
  28.         else
  29.             A[a] <- D[d]
  30.             d <- d + 1
  31.         endif
  32.         a <- a + 1
  33. endwhile
  34.  
  35. #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
  36.  
  37. if (b>Blength)
  38.    
  39.     while ( c<=Clength AND d<=Dlength )
  40.         if (C[c]<D[d]){
  41.             A[a] <- C[c]
  42.             c <- c + 1
  43.         else
  44.             A[a] <- D[d]
  45.             d <- d + 1
  46.         endif
  47.         a <- a + 1
  48.     endwhile
  49.  
  50. else if (c>Clength)
  51.  
  52.     while ( b<=Blength AND d<=Dlength )
  53.         if (B[b]<D[d]){
  54.             A[a] <- B[b]
  55.             b <- b + 1
  56.         else
  57.             A[a] <- D[d]
  58.             d <- d + 1
  59.         endif
  60.         a <- a + 1
  61.     endwhile
  62.  
  63. else
  64.  
  65.     while ( b<=Blength AND c<=Clength )
  66.         if (B[b]<C[c]){
  67.             A[a] <- B[b]
  68.             b <- b + 1
  69.         else
  70.             A[a] <- C[c]
  71.             c <- c + 1
  72.         endif
  73.         a <- a + 1
  74.     endwhile
  75.  
  76. endif
  77.  
  78. #now just need to fill the final array with the left array that there are some elements left
  79.  
  80. while (b<Blength)
  81.    
  82.     A[a] <- B[b]
  83.     b <- b + 1
  84.     a <- a + 1
  85. endwhile
  86.  
  87. while (c<Clength)
  88.    
  89.     A[a] <- C[c]
  90.     c <- c + 1
  91.     a <- a + 1
  92. endwhile
  93.  
  94. while (d<Dlength)
  95.    
  96.     A[a] <- D[d]
  97.     d <- d + 1
  98.     a <- a + 1
  99. endwhile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement