In the Odd-Even sort, or Brick sort, you take every second element and swap it with the next one if they are not sorted. Then you take the same elements and swap it with the previous element if they are not ordered. You continue until you are done. You can formulate the algorithm a bit … Continue reading “💡 107. Odd-even sort in Perl 6”
Tag: sorting algorithms
💡 106. Gnome sort in Perl 6
Our today’s topic is the Gnome sort, which is also referred to as Stupid sort. To sort an array using this method, you scan the data from left to right and check the two adjacent items to see if they are ordered properly. If they are, you go forward. If not, you swap the elements … Continue reading “💡 106. Gnome sort in Perl 6”
💡 105. Pancake sort in Perl 6
The Pancake sort is an interesting method of sorting data, as unlike more traditional sorting algorithms, it operates with piles of data on each step. You have to imagine data as a pile of pancakes, the values corresponding to the size of pancakes. The only allowed operation is flipping a pile of ‘pancakes.’ It can … Continue reading “💡 105. Pancake sort in Perl 6”
💡 104. Stooge sort in Perl 6
Have you ever heard of the Stooge sort algorithm? If not, it is quite interesting to learn it. The idea is clear, while you maybe need some time to see if it really works. So, take a list of numbers and swap the first and the last elements if they are not sorted properly (that … Continue reading “💡 104. Stooge sort in Perl 6”
💡 103. Merge sort in Perl 6
Welcome to another sorting episode, this time we’ll talk about Merge sort in Perl 6. In Merge sort, you first split the data into halves until the pieces become atomic (in the original meaning of the word), that is either each piece contains a single element, or, after the current split, the second part contains … Continue reading “💡 103. Merge sort in Perl 6”
💡 102. Insertion sort in Perl 6
Today, we are investigating the Insertion sort algorithm and its possible implementation in Perl 6. The algorithm’s complexity is O(n2), but it is a good candidate to practice some Perl 6. The idea is simple. You find the minimum value in the array and put it to the first position. Then you scan the data … Continue reading “💡 102. Insertion sort in Perl 6”
💡 101. Quick sort in Perl 6
Today, let’s look at another, and presumably the most well known method of sorting data, Quick sort. The algorithm requires you to select the so-called pivot, one of the element from the data, and split the rest in two parts: the elements less than the pivot, and the elements more or equals to the pivot. … Continue reading “💡 101. Quick sort in Perl 6”
💡 100. Bubble sort in Perl 6
Hey everyone, let’s implement some algorithms in Perl 6. The first one will be the classical Bubble sort. In essence, you scan the data from left to right and swap the two adjacent items if they are not ordered properly yet. Repeat until the whole data list is ordered. Here’s the initial straightforward implementation: sub … Continue reading “💡 100. Bubble sort in Perl 6”