How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the Maple programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the Maple programming language

Table of Contents

Problem Statement

Sort an array of integers (of any convenient size) into ascending order using Pancake sorting. In short, instead of individual elements being sorted, the only operation allowed is to "flip" one end of the list, like so: Only one end of the list can be flipped; this should be the low end, but the high end is okay if it's easier to code or works better, but it must be the same end for the entire solution. (The end flipped can't be arbitrarily changed.) Show both the initial, unsorted list and the final sorted list. (Intermediate steps during sorting are optional.) Optimizations are optional (but recommended).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the Maple programming language

Source code in the maple programming language

flip := proc(arr, i)
	local start, temp, icopy;
	temp, start, icopy := 0,1,i:
	while (start < icopy) do
		arr[start], arr[icopy] := arr[icopy], arr[start]:
		start:=start+1:
		icopy:=icopy-1:
	end do:
end proc:
findMax := proc(arr, i)
	local Max, j:
	Max := 1:
	for j from 1 to i do
		if arr[j] > arr[Max] then Max := j: end if:
	end do:
	return Max:
end proc:
pancakesort := proc(arr)
	local len,i,Max;
	len := numelems(arr):
	for i from len to 2 by -1 do
		print(arr):
		Max := findMax(arr, i):
		if (not Max = i) then
			flip(arr, Max):
			flip(arr, i):
		end if:
	end do:
	print(arr);
end proc:

  

You may also check:How to resolve the algorithm 15 puzzle game step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the LabVIEW programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the APL programming language
You may also check:How to resolve the algorithm Sorting algorithms/Quicksort step by step in the Logtalk programming language
You may also check:How to resolve the algorithm SHA-1 step by step in the NetRexx programming language