What sorting algorithm is this? I thought o开发者_如何学编程f this method last night and quickly drew up some code, and to my surprise worked perfectly. I've been looking through the Wikipedia article on sorting algorithms and have searched Stack Overflow but have been unable to find this or similar algorithm.
This is the algorithm's written process:
[3, 1, 0, 4]
^ ^ Check outermost items, swap if necessary
----------------
[3, 1, 0, 4]
^ ^ Check first pair of second farthest apart items, swap if necessary
[0, 1, 3, 4]
----------------
[0, 1, 3, 4]
^ ^ Check second pair of second farthest apart items, swap if necessary
----------------
[0, 1, 3, 4]
^ ^ Check first pair of third farthest apart items, swap if necessary
----------------
[0, 1, 3, 4]
^ ^ Check second pair of third farthest apart items, swap if necessary
----------------
[0, 1, 3, 4]
^ ^ Check third pair of third farthest apart items, swap if necessary
----------------
[0, 1, 3, 4]
Cannot compare closer items (adjacent), done.
This is the algorithm in JavaScript:
var unsorted = [4, 9, 10, 2, 9, 4, 0];
var sorted = ianSort(unsorted);
function ianSort(array) {
for(var j = array.length; j > 1; j--) {
for(var i = 0; i < array.length - j + 1; i++) {
if(array[i] > array[i + j - 1]) {
var temp = array[i];
array[i] = array[i + j - 1];
array[i + j - 1] = temp;
}
}
}
return array;
}
(I've called the function IanSort here in the unlikely event that I'm the first person to think this up. ^_^)
I believe that what you've devised is related to comb sort, which is a generalization of bubble sort that initially starts with a large step size and then decays the step size over time. Traditionally, comb sort works by starting with a large size and then decaying the interval size by a constant factor on each iteration. Your algorithm essentially does this by choosing a constant such that the step size shrinks by one on each iteration.
Looks like a Comb sort
I'm not quite sure, but it looks like the bubble sort algorithim to me.
This appears to be a form of the Comb Sort algorithm. I also thought I was the first to discover it several years ago and made up several variations to test against the quick sort and other sorting algorithms. You can read about my research here.
It's a fast algorithm that can compete with the quick sort, even beating it under some conditions.
精彩评论