Avoiding Extra slice() in Bubble Sort While Keeping Functional Immutability

I’m working with TypeScript that follows functional programming principles, meaning I want to keep immutability and avoid mutating any of the input arrays.

I’m using bubble sort, and I have an issue where I need to return a new sorted array without modifying the original one.

However, my current implementation seems to be adding unnecessary overhead by adding a .slice() at the end to create a new array.

export default function bubbleSort<T>(array: ReadonlyArray<T>, compare: (a: T, b: T) => number): ReadonlyArray<T> {
  // Helper function to swap elements in an array
  const swap = (arr: ReadonlyArray<T>, i: number, j: number): ReadonlyArray<T> => {
    const newArr = [...arr]
    ;[newArr[i], newArr[j]] = [newArr[j], newArr[i]]
    return newArr
  }

  // Function to perform one full pass of bubble sort, bubbling the largest element to the end
  const bubblePass = (arr: ReadonlyArray<T>, n: number): [ReadonlyArray<T>, boolean] => {
    // Slice the array to ignore already sorted elements
    return arr.slice(1, n).reduce(
      ([accArray, swapped], _, i) => {
        if (compare(accArray[i], accArray[i + 1]) > 0) {
          return [swap(accArray, i, i + 1), true] // Swap if necessary and mark as swapped
        }
        return [accArray, swapped]
      },
      [arr, false] as [ReadonlyArray<T>, boolean],
    )
  }

  // Recursive function to apply the bubble sort until no more swaps are needed
  const sort = (arr: ReadonlyArray<T>, n: number): ReadonlyArray<T> => {
    if (n <= 1) return arr // Base case: if the array length is 1 or less, return the array
    const [newArr, swapped] = bubblePass(arr, n)
    return swapped ? sort(newArr, n - 1) : newArr // Recur with the size reduced by 1 if swapped, else return sorted array
  }

  return sort([...array], array.length)
}

Then, the errors happen:

const arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
const sorted = bubbleSort(arr, (a, b) => a - b)
sorted.push(123) // Property 'push' does not exist on type 'readonly number[]'.

It works with slice method at the end

export default function bubbleSort<T>(array: ReadonlyArray<T>, compare: (a: T, b: T) => number): T[]  {
  // Helper function to swap elements in an array
  const swap = (arr: ReadonlyArray<T>, i: number, j: number): ReadonlyArray<T> => {
    const newArr = [...arr]
    ;[newArr[i], newArr[j]] = [newArr[j], newArr[i]]
    return newArr
  }

  // Function to perform one full pass of bubble sort, bubbling the largest element to the end
  const bubblePass = (arr: ReadonlyArray<T>, n: number): [ReadonlyArray<T>, boolean] => {
    // Slice the array to ignore already sorted elements
    return arr.slice(1, n).reduce(
      ([accArray, swapped], _, i) => {
        if (compare(accArray[i], accArray[i + 1]) > 0) {
          return [swap(accArray, i, i + 1), true] // Swap if necessary and mark as swapped
        }
        return [accArray, swapped]
      },
      [arr, false] as [ReadonlyArray<T>, boolean],
    )
  }

  // Recursive function to apply the bubble sort until no more swaps are needed
  const sort = (arr: ReadonlyArray<T>, n: number): ReadonlyArray<T> => {
    if (n <= 1) return arr // Base case: if the array length is 1 or less, return the array
    const [newArr, swapped] = bubblePass(arr, n)
    return swapped ? sort(newArr, n - 1) : newArr // Recur with the size reduced by 1 if swapped, else return sorted array
  }

  return sort([...array], array.length).slice()
}

The Issue: The .slice() at the end solves the immutability issue, but it seems to be a waste of performance, especially since the array has already been copied during the sorting process.

2

For the types to work correctly, we have to get rid of ReadonlyArray. Instead of .slice, you can use the i < n - 1 check in .reduce.

Here is my sample code:

export default function bubbleSort<T>(array: ReadonlyArray<T>, compare: (a: T, b: T) => number): T[] {
  // Helper function to swap elements in an array
  const swap = (arr: T[], i: number, j: number): T[] => {
    const newArr = [...arr];
    [newArr[i], newArr[j]] = [newArr[j], newArr[i]];
    return newArr
  }

  // Function to perform one full pass of bubble sort, bubbling the largest element to the end
  const bubblePass = (arr: T[], n: number): [T[], boolean] => {
    // Slice the array to ignore already sorted elements
    return arr.reduce(
      ([accArray, swapped], _, i) => {
        if (i < n - 1 && compare(accArray[i], accArray[i + 1]) > 0) {
          return [swap(accArray, i, i + 1), true] // Swap if necessary and mark as swapped
        }
        return [accArray, swapped]
      },
      [arr, false] as [T[], boolean],
    )
  }

  // Recursive function to apply the bubble sort until no more swaps are needed
  const sort = (arr: T[], n: number): T[] => {
    if (n <= 1) return arr // Base case: if the array length is 1 or less, return the array
    const [newArr, swapped] = bubblePass(arr, n)
    return swapped ? sort(newArr, n - 1) : newArr // Recur with the size reduced by 1 if swapped, else return sorted array
  }

  return sort([...array], array.length)
}

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật