Pancake Sorting

Pancake Sorting :

Author(s) : Jacob Eli Goodman Date : 1970s

Pancake sort, despite its intriguing name, operates on a more theoretical level. It envisions an array of elements like pancakes on a griddle. The algorithm involves 'flipping' subsequences within the array to bring the largest element to the end. This process is repeated, progressively reducing the unsorted portion of the array. Pancake sort has a worst-case time complexity of O(n²) but finds application in some theoretical computer science problems. However, due to its flipping operation being impractical for most real-world implementations, it's not commonly used for practical sorting tasks.

Time Complexity O(n²)
Best Case O(n)
Worst Case O(n²)
Space Complexity O(1)
Stable No

Code Integration:

* the code might contain some bugs or may not work correctly

Python JavaScript Java Go C
def pancakesort(arr):
	n = len(arr)
	while n > 1:
		# Find index of the maximum element in the unsorted part
		max_index = arr.index(max(arr[:n]))
		# Flip elements from the beginning to the index of the maximum element
		arr = arr[:max_index + 1][::-1] + arr[max_index + 1:]
		# Flip elements from the beginning to the new end (excluding the maximum element)
		arr = arr[:n][::-1] + arr[n:]
		n -= 1  # Reduce the unsorted part

# Test array
arr = [3, 2, 4, 1]
pancakesort(arr)
print('Sorted array is:', arr)
function pancakesort(arr) {
	let n = arr.length;
	while (n > 1) {
		// Find index of the maximum element in the unsorted part
		let maxIndex = arr.indexOf(Math.max(...arr.slice(0, n)));
		// Flip elements from the beginning to the index of the maximum element
		arr.splice(0, maxIndex + 1).reverse();
		// Flip elements from the beginning to the new end (excluding the maximum element)
		arr.splice(0, n).reverse();
		n--;  // Reduce the unsorted part
	}

	// Test array
	let arr = [3, 2, 4, 1];
	pancakesort(arr);
	console.log('Sorted array is:', arr);
}
public static void pancakesort(int[] arr) {
	int n = arr.length;
	while (n > 1) {
		// Find index of the maximum element in the unsorted part
		int maxIndex = 0;
		for (int i = 0; i < n; i++) {
			if (arr[i] > arr[maxIndex]) {
				maxIndex = i;
			}
		}
		// Flip elements from the beginning to the index of the maximum element
		reverse(arr, 0, maxIndex);
		// Flip elements from the beginning to the new end (excluding the maximum element)
		reverse(arr, 0, n - 1);
		n--;  // Reduce the unsorted part
	}

	// Helper function for reversing a subarray
	private static void reverse(int[] arr, int start, int end) {
		while (start < end) {
			int temp = arr[start];
			arr[start] = arr[end];
			arr[end] = temp;
			start++;
			end--;
		}
	}

	// Test array
	int[] arr = {3, 2, 4, 1};
	pancakesort(arr);
	System.out.println("Sorted array is:" + java.util.Arrays.toString(arr));
}
func pancakesort(arr []int) {
	n := len(arr)
	for n > 1 {
		// Find index of the maximum element in the unsorted part
		maxIndex := 0
		for i := 0; i < n; i++ {
			if arr[i] > arr[maxIndex] {
				maxIndex = i
			}
		}
		// Flip elements from the beginning to the index of the maximum element
		reverse(arr[:maxIndex+1])
		// Flip elements from the beginning to the new end (excluding the maximum element)
		reverse(arr[:n])
		n--  // Reduce the unsorted part
	}

	// Helper function for reversing a slice
	func reverse(slice []int) {
		for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 {
			slice[i], slice[j] = slice[j], slice[i]
		}
	}

	// Test array
	arr := []int{3, 2, 4, 1}
	pancakesort(arr)
	fmt.Println("Sorted array is:", arr)
}
void pancakesort(int arr[], int n) {
	while (n > 1) {
		// Find index of the maximum element in the unsorted part
		int maxIndex = 0;
		for (int i = 0; i < n; i++) {
			if (arr[i] > arr[maxIndex]) {
				maxIndex = i;
			}
		}
		// Flip elements from the beginning to the index of the maximum element
		flip(arr, 0, maxIndex);
		// Flip elements from the beginning to the new end (excluding the maximum element)
		flip(arr, 0, n - 1);
		n--;  // Reduce the unsorted part
	}

	// Helper function for reversing a subarray
	void flip(int arr[], int start, int end) {
		while (start < end) {
			int temp = arr[start];
			arr[start] = arr[end];
			arr[end] = temp;
			start++;
			end--;
		}
	}

	// Test array
	int arr[] = {3, 2, 4, 1};
	int n = sizeof(arr) / sizeof(arr[0]);
	pancakesort(arr, n);
	printf("Sorted array is: ");
	for (int i = 0; i < n; i++) {
		printf("%d ", arr[i]);
	}
}
light dark