Skip to main content
Code Review

Return to Question

put transscribed book code in block quote; attribute
Source Link
greybeard
  • 7.4k
  • 3
  • 21
  • 55

odd-even-merge sort for 32 inputs odd-even-merge sort for 32 inputs (clickable)

TheFor context the code from the book translated into C:

#include <stdlib.h>
#include <stdio.h>
void sort(int l, int r)
{ int n = r-l+1;
 for (int p=1; p<n; p+=p)
 for (int k=p; k>0; k/=2)
 for (int j=k%p; j+k<n; j+=(k+k))
 for (int i=0; i<n-j-k; i++)
 if ((j+i)/(p+p) == (j+i+k)/(p+p))
 printf("%2i cmp %2i\n", l+j+i, l+j+i+k);
}
int main(char* argv, int args)
{ const int COUNT = 8;
 sort(0, COUNT-1);
}
#include <stdlib.h>
#include <stdio.h>
void sort(int l, int r)
/* transscribed from Program 11.4 batchersortNR() in
 * Robert Sedgewick's 2002
 * "Algorithms in Java", Parts 1-4 (3rd Edition) */
{ int n = r-l+1;
 for (int p=1; p<n; p+=p)
 for (int k=p; k>0; k/=2)
 for (int j=k%p; j+k<n; j+=(k+k))
 for (int i=0; i<n-j-k; i++)
 if ((j+i)/(p+p) == (j+i+k)/(p+p))
 printf("%2i cmp %2i", l+j+i, l+j+i+k);
}
int main(char* argv, int args)
{ const int COUNT = 8;
 sort(0, COUNT-1);
}

odd-even-merge sort for 32 inputs (clickable)

The code from the book translated into C:

#include <stdlib.h>
#include <stdio.h>
void sort(int l, int r)
{ int n = r-l+1;
 for (int p=1; p<n; p+=p)
 for (int k=p; k>0; k/=2)
 for (int j=k%p; j+k<n; j+=(k+k))
 for (int i=0; i<n-j-k; i++)
 if ((j+i)/(p+p) == (j+i+k)/(p+p))
 printf("%2i cmp %2i\n", l+j+i, l+j+i+k);
}
int main(char* argv, int args)
{ const int COUNT = 8;
 sort(0, COUNT-1);
}

odd-even-merge sort for 32 inputs (clickable)

For context the code from the book translated into C:

#include <stdlib.h>
#include <stdio.h>
void sort(int l, int r)
/* transscribed from Program 11.4 batchersortNR() in
 * Robert Sedgewick's 2002
 * "Algorithms in Java", Parts 1-4 (3rd Edition) */
{ int n = r-l+1;
 for (int p=1; p<n; p+=p)
 for (int k=p; k>0; k/=2)
 for (int j=k%p; j+k<n; j+=(k+k))
 for (int i=0; i<n-j-k; i++)
 if ((j+i)/(p+p) == (j+i+k)/(p+p))
 printf("%2i cmp %2i", l+j+i, l+j+i+k);
}
int main(char* argv, int args)
{ const int COUNT = 8;
 sort(0, COUNT-1);
}
"Must not" isn't the opposite of "must"
Source Link
Toby Speight
  • 87.3k
  • 14
  • 104
  • 322

odd-even-merge sort for 32 inputs odd-even-merge sort for 32 inputs (clickable)

See my StackOverflow question StackOverflow question for more details and a diagram for the falsly generated network.

  • Is it correct?
  • Can it be improved? E.g. just 4 loops.
  • It mustneed not be very fast, because it's a hardware generation and check routine.

odd-even-merge sort for 32 inputs (clickable)

See my StackOverflow question for more details and a diagram for the falsly generated network.

  • Is it correct?
  • Can it be improved? E.g. just 4 loops.
  • It must not be very fast, because it's a hardware generation and check routine.

odd-even-merge sort for 32 inputs (clickable)

See my StackOverflow question for more details and a diagram for the falsly generated network.

  • Is it correct?
  • Can it be improved? E.g. just 4 loops.
  • It need not be very fast, because it's a hardware generation and check routine.
broken image fixed (click 'rendered output' to see the difference); for more info, see https://gist.github.com/Glorfindel83/9d954d34385d2ac2597bbe864466259f
Source Link
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
Loading
added standard implementation for a log2 function.
Source Link
Paebbels
  • 597
  • 6
  • 19
Loading
added 137 characters in body
Source Link
Paebbels
  • 597
  • 6
  • 19
Loading
added 1153 characters in body
Source Link
Paebbels
  • 597
  • 6
  • 19
Loading
Source Link
Paebbels
  • 597
  • 6
  • 19
Loading
lang-c

AltStyle によって変換されたページ (->オリジナル) /