Skip to content

JavaScript 排序 #47

Open
Open

Description

@fakefish

comb sort

分个组分别冒泡

Array.prototype.combSort = function () {
  var factor = 0.8
  var gap = this.length
  var swapped = true
  var i
  var temp
  while (gap > 1 || swapped) {
    if (gap > 1) {
      gap = Math.floor(gap * factor)
    }
    swapped = false
    for (i = 0; gap + i < this.length; i++) {
      if (this[i] > this[i + gap]) {
        temp = this[i]
        this[i] = this[i + gap]
        this[i + gap] = temp;
        swapped = true
      }
    }
  }
  return this
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions