Brick Sort

package sort

/**
 * This method implements the Generic Brick Sort
 *
 * @param array The array to be sorted
 * Sorts the array in increasing order
 *
 * Worst-case performance	O(n^2)
 * Best-case performance	O(n)
 * Average performance	O(n^2)
 * Worst-case space complexity	O(1)
 **/

fun <T : Comparable<T>>  oddEvenSort(array: Array<T>) {
    var isSorted = false
    while (!isSorted) {
        isSorted = true
        var temp : Comparable<T>


        var i = 1
        while (i <= array.size - 2) {
            if (array[i] > array[i + 1]) {
                temp = array[i]
                array[i] = array[i + 1]
                array[i + 1] = temp
                isSorted = false
            }
            i += 2
        }
        var j = 0
        while (j <= array.size - 2) {
            if (array[j] > array[j + 1]) {
                temp = array[j]
                array[j] = array[j + 1]
                array[j + 1] = temp
                isSorted = false
            }
            j += 2
        }
    }
    return
}
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.