For faster navigation, this Iframe is preloading the Wikiwand page for 奇偶排序.

奇偶排序

奇偶排序
使用奇偶排序法对一列随机数字进行排序的过程
概况
類別排序算法
資料結構数组
复杂度
最坏时间复杂度
最优时间复杂度
最佳解
相关变量的定义

奇偶排序(英語:Odd–even sort),或奇偶换位排序砖排序[1],是一种相对简单的排序算法,最初发明用于有本地互连的并行计算。这是与冒泡排序特点类似的一种比较排序

该算法中,通过比较数组中相邻的(奇-偶)位置数字对,如果该奇偶对是错误的顺序(第一个大于第二个),则交换。下一步重复该操作,但针对所有的(偶-奇)位置数字对。如此交替进行下去。

处理器数组的排序

并行计算排序中,每个处理器对应处理一个值,并仅有与左右邻居的本地互连。所有处理器可同时与邻居进行比较、交换操作,交替以奇-偶、偶-奇的顺序。该算法由Habermann在1972年最初发表并展现了在并行处理上的效率。[2]

该算法可以有效地延伸到每个处理器拥有多个值的情况。在Baudet–Stevenson奇偶合并分割算法中,每个处理器在每一步对自己所拥有的子数组进行排序,然后与邻居执行合并分割或换位合并。[3]

Batcher奇偶归并排序

Batcher奇偶归并排序是一种相关但更有效率的排序算法,采用比较-交换和完美-洗牌操作。[4]

Batcher的方法在拥有广泛互连的并行计算处理器上效率不错。[5]

程式代碼

C语言

void odd_even_sort(int arr[], int len) {
	int odd_even, i;
	int temp;
	int sorted = 0;
	while (!sorted) {
		sorted = 1;
		for (odd_even = 0; odd_even < 2; odd_even++)
			for (i = odd_even; i < len - 1; i += 2)
				if (arr[i] > arr[i + 1]) {
					temp = arr[i];
					arr[i] = arr[i + 1];
					arr[i + 1] = temp;
					sorted = 0;
				}
	}
}

C++

template<typename T> //整數或浮點數皆可使用,若要使用物件(class)時必須設定大於(>)的運算子功能
void odd_even_sort(T arr[], int len) {
	int odd_even, i;
	bool sorted = false;
	while (!sorted) {
		sorted = true;
		for (odd_even = 0; odd_even < 2; odd_even++)
			for (i = odd_even; i < len - 1; i += 2)
				if (arr[i] > arr[i + 1]) {
					swap(arr[i], arr[i + 1]);
					sorted = false;
				}
	}
}

Python

# 假设已有列表a等待排序
while True:
    sorted = True
    # 处理奇-偶对
    for i in xrange(1, len(a)-1, 2):
        if a[i] > a[i+1]:
           a[i], a[i+1] = a[i+1], a[i] # 交换
           sorted = False
    # 处理偶-奇对
    for i in xrange(0, len(a)-1, 2):
        if a[i] > a[i+1]:
           a[i], a[i+1] = a[i+1], a[i] # 交换
           sorted = False
    if sorted:
        break

JavaScript

Array.prototype.odd_even_sort = function() {
	var odd_even, i;
	var temp;
	var sorted = 0;
	while (!sorted) {
		sorted = 1;
		for ( odd_even = 0; odd_even < 2; odd_even++)
			for ( i = odd_even; i < this.length - 1; i += 2)
				if (this[i] > this[i + 1]) {
					temp = this[i];
					this[i] = this[i + 1];
					this[i + 1] = temp;
					sorted = 0;
				}
	}
	return this;
};

PHP

function swap(&$x, &$y) {
	$t = $x;
	$x = $y;
	$y = $t;
}
function odd_even_sort(&$arr) {//php的陣列視為基本型別,所以必須用傳參考才能修改原陣列
	$sorted = 0;
	while (!$sorted) {
		$sorted = 1;
		for ($odd_even = 0; $odd_even < 2; $odd_even++)
			for ($i = $odd_even; $i < count($arr) - 1; $i += 2)
				if ($arr[$i] > $arr[$i + 1]) {
					swap($arr[$i], $arr[$i + 1]);
					$sorted = 0;
				}
	}
}

参考文献

  1. ^ Phillips, Malcolm. Array Sorting. [3 August 2011]. (Sort Techniques 原始内容 请检查|url=值 (帮助)存档于2011年10月28日). 
  2. ^ N. Haberman (1972) "Parallel Neighbor Sort (or the Glory of the Induction Principle)," CMU Computer Science Report (available as Technical report AD-759 248, National Technical Information Service, US Department of Commerce, 5285 Port Royal Rd Sprigfield VA 22151).
  3. ^ S. Lakshmivarahan, S. K. Dhall, and L. L. Miller, Franz L. Alt and Marshall C. Yovits , 编, Parallel Sorting Algorithms, Advances in computers (Academic Press), 1984, 23: 295–351, ISBN 9780120121236 
  4. ^ Robert Sedgewick. Algorithms in Java, Parts 1-4 3rd. Addison-Wesley Professional. 2003: 454–464. ISBN 9780201361209. 
  5. ^ Allen Kent and James G. Williams. Encyclopedia of Computer Science and Technology: Supplement 14. CRC Press. 1993: 33–38. ISBN 9780824722821. 

外部連結

{{bottomLinkPreText}} {{bottomLinkText}}
奇偶排序
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?