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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the Swift 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 Swift programming language

Source code in the swift programming language

import Foundation

struct PancakeSort {
    var arr:[Int]
    
    mutating func flip(n:Int) {
        for i in 0 ..< (n + 1) / 2 {
            swap(&arr[n - i], &arr[i])
        }
        println("flip(0.. \(n)): \(arr)")
    }
    
    func minmax(n:Int) -> [Int] {
        var xm = arr[0]
        var xM = arr[0]
        var posm = 0
        var posM = 0
        
        for i in 1..<n {
            if (arr[i] < xm) {
                xm = arr[i]
                posm = i
            } else if (arr[i] > xM) {
                xM = arr[i]
                posM = i
            }
        }
        
        return [posm, posM]
    }
    
    mutating func sort(var n:Int, var dir:Int) {
        if n == 0 {
            return
        }
        
        let mM = minmax(n)
        let bestXPos = mM[dir]
        let altXPos = mM[1 - dir]
        var flipped = false
        
        if bestXPos == n - 1 {
            n--
        } else if bestXPos == 0 {
            flip(n - 1)
            n--
        } else if altXPos == n - 1 {
            dir = 1 - dir
            n--
            flipped = true
        } else {
            flip(bestXPos)
        }
        
        sort(n, dir: dir)
        
        if flipped {
            flip(n)
        }
    }
}

let arr = [2, 3, 6, 1, 4, 5, 10, 8, 7, 9]
var a = PancakeSort(arr: arr)
a.sort(arr.count, dir: 1)
println(a.arr)


  

You may also check:How to resolve the algorithm Pascal's triangle step by step in the BQN programming language
You may also check:How to resolve the algorithm Magic 8-ball step by step in the uBasic/4tH programming language
You may also check:How to resolve the algorithm Averages/Median step by step in the K programming language
You may also check:How to resolve the algorithm Make directory path step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Imaginary base numbers step by step in the Perl programming language