The pyramids of Egypt are some of the most fascinating structures in the world. To honor the great pyramids we'll be introducing pyramid sort. Pyramid sort works so that the highest numbers are in the center of the array and the lowest are on the edges. When comparing 2 numbers the smaller number goes on the left. So if the array contains 1,2,3 then 1 goes on the left with 2 on the far right and 3 in the middle. Here are 2 more examples:
> psort([1,2,3,4,5]) [1,3,5,4,2] > psort([1,3,5,7,9,11]) [1,5,9,11,7,3]
Comments:
Karl T - 10 years, 8 months ago
Here's my attempt. It might be a little cheating because C# has an Array.Sort function if using Linq, but it was still hard to get the logic for alternating the array index right.
reply permalink
Tom - 10 years, 8 months ago
Here is my Java solution.
/** * PyramidSort.java */
import java.util.Scanner; import java.util.ArrayList; import java.util.Arrays;
public class PyramidSort{ private static int [] nums;
}
reply permalink
Tom - 10 years, 8 months ago
apologys for the layout there, I don't know how to post my code properly on this site.
reply permalink
Max Burstein - 10 years, 8 months ago
The site uses Github flavored markdown so this is how you can format your code https://help.github.com/articles/github-flavored-markdown#fenced-code-blocks
Thanks for the submission!
reply permalink
Zigo - 10 years, 8 months ago
Here's something in Python. I think it could be simpler, but after using C for so long wrapping my head around this language is impossible.
reply permalink
Heroka - 10 years, 8 months ago
My attempt in R:
reply permalink
Anonymous - 10 years, 8 months ago
In J the solution is quite short:
And to show that it works (
NB.
is a comment in J):reply permalink
Anonymous - 10 years, 8 months ago
By the way, Max, I'm not sure the problem description is correct for an even number of integers. In your second example,
11 is at the top, 9 left of it, 7 right of it. But shouldn't 9 be on the right hand side?
reply permalink
Max Burstein - 10 years, 8 months ago
9 vs 11 is how it would break down for the even case. Since 9 is the smaller number it goes to the left.
reply permalink
Anonymous - 10 years, 8 months ago
Ah, right, I figured the pyramid should always have a single 'peak'. Thanks.
reply permalink
Anonymous - 10 years, 8 months ago
Fixed.
And the result:
Thanks for the challenge!
reply permalink
Pearce Keesling - 10 years, 8 months ago
Sometimes it amazes me just how long it takes to write things in C, I could probably have written this in a few lines in Ruby, but just the formatting alone takes a collective 20 lines.
If anyone has any advice on how to do formatting more efficiently, I'm all ears :)
reply permalink
tl300 - 10 years, 8 months ago
Possible, quick solution in Python...
reply permalink
Hueho - 10 years, 8 months ago
Easy mode: pre-sorting the array in crescent order, and swapping consecutive elements until it reached the mirrored equivalente to the original position.
Essentially bubblesort, but dumber.
reply permalink
Anonymous - 10 years, 8 months ago
Simple (but inefficient) Python:
reply permalink
Zach - 10 years, 8 months ago
I took some time to make it output in a really nice pyramidal shape.
reply permalink
Bill Bejeck - 10 years, 8 months ago
Here's a solution in Java. /** * Assumes input is already sorted in ascending order and * a ',' delimited String */ public class PyramidSort {
}
reply permalink
Mre64 - 10 years, 6 months ago
/* Mre64 6/11/14
/ import java.util.;
public class PryamidSort {
}
reply permalink