CzB404 / gaza-sort

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GazaSort

GazaSort is a lossy and non-deterministic sorting algorithm that sorts an array of numbers by randomly removing elements until the remaining array is sorted. Although it does return a sorted array, the algorithm focuses on randomly eliminating values rather than organizing them. Data loss is not seen as a flaw but as a feature. It mutates the original array with complete disregard for data integrity.

Contributing

We’re accepting help from anyone! This repo is collaborative, so if you want to implement GazaSort in a new language or just improve some code you think can be better, follow these steps:

  • Fork the repository.
  • Add your code to the language folder in the main branch. If there isn’t a language folder, create one and add your code there.
  • We currently allow only one solution per language. If one already exists, you can improve the existing code.
  • Follow the best practices in your chosen language.
  • Submit a pull request for review.

Sample Implementation in TypeScript

/**
 * GazaSort: The only method for sorting an array while embracing necessary data sacrifices.
 * @param {number[]} arr - The array of numbers to be sorted.
 * @returns {number[]} The sorted array after unavoidable sacrifices.
 */
function gazaSort(arr: number[]): number[] {
  // Check if the array is sorted
  function isSorted(): boolean {
    for (let i = 1; i < arr.length; i++) {
      if (arr[i - 1] > arr[i]) return false;
    }
    return true;
  }

  // Remove elements until the array is sorted
  while (!isSorted()) {
    const randomTarget = Math.floor(Math.random() * arr.length);
    arr.splice(randomTarget, 1);
  }

  // Return sorted array
  return arr;
}

Give this Project a Star ⭐

If you liked working on this project, please share this project as much as you can and star this project to help as many people in open source as you can.

About

License:MIT License


Languages

Language:TypeScript 12.1%Language:Python 11.2%Language:Java 9.7%Language:C# 8.5%Language:Go 7.1%Language:C++ 6.2%Language:Haskell 5.8%Language:Hack 5.8%Language:Swift 5.6%Language:Perl 5.4%Language:Rust 5.2%Language:Scala 5.2%Language:Kotlin 4.7%Language:Lua 4.5%Language:Ruby 3.0%