cheapnawer.blogg.se

Free alternative to pixel sorter
Free alternative to pixel sorter







Each column of blue and green boxes takes N sorted sequences and concatenates them in pairs to form N/2 bitonic sequences, which are then sorted by the boxes in that column to form N/2 sorted sequences. The output of each green or blue box will be a sorted sequence, so the output of each pair of adjacent lists will be bitonic, because the top one is blue and the bottom one is green. For any arbitrary sequence of inputs, it will sort them correctly, with the largest at the bottom. The green and blue boxes combine to form the entire sorting network. Therefore, the output of the green or blue box will be completely sorted. This continues until it is sorted into exactly the correct position. It will then pass through a smaller red box that sorts it into the correct quarter of the list within that half. If a number enters the blue or green box, then the first red box will sort it into the correct half of the list. If the input to this box happens to be bitonic, then the output will be completely sorted in increasing order (blue) or decreasing order (green). This structure is known as a butterfly network. All arrows point down (blue) or all point up (green). Every such box has the same structure: a red box is applied to the entire input sequence, then to each half of the result, then to each half of each of those results, and so on. The red boxes combine to form blue and green boxes. This theorem is not obvious, but can be verified by carefully considering all the cases of how the various inputs might compare, using the zero-one principle. The top half of the output will be bitonic, and the bottom half will be bitonic, with every element of the top half less than or equal to every element of the bottom half (for dark red) or vice versa (for light red). If the inputs happen to form a bitonic sequence, then the output will form two bitonic sequences. The colored boxes are just for illustration and have no effect on the algorithm.Įvery red box has the same structure: each input in the top half is compared to the corresponding input in the bottom half, with all arrows pointing down (dark red) or all up (light red). If they are out of order, they are swapped. Whenever two numbers reach the two ends of an arrow, they are compared to ensure that the arrow points toward the larger number. The network is designed to sort the elements, with the largest number at the bottom. The 16 numbers enter at the inputs at the left end, slide along each of the 16 horizontal wires, and exit at the outputs at the right end. The following is a bitonic sorting network with 16 inputs:









Free alternative to pixel sorter