-
交換排序
鎖定
所謂交換,就是根據
序列中兩個記錄鍵值的比較結果來
對換這兩個記錄在序列中的位置,交換排序的特點是:將
鍵值較大的記錄向序列的尾部移動,鍵值較小的記錄向序列的前部移動。
- 中文名
-
交換排序
- 外文名
-
Swap Sort
- 根 據
-
根據序列中兩個記錄
- 特 點
-
鍵值較小的記錄向序列的前部移動
在
程序設計中,交換排序是基本排序方法的一種,下邊用
c語言實現一個交換排序的函數:
void swapsort(int a[],int length)
{
for(int i=0; i<length-1; i++){
for(intj=i+1; j<length; j++){
if(a[i]>a[j]){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
下面是
java語言實現一個交換排序的函數:
public static void swapSort(int[] array){
for(int i=0; i<array.length-1; i++){
for(int j=i+1; j<array.length; j++){
if(array[i]>array[j]){
int temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
}
go語言實現一個交換排序的函數:
package main
import "fmt"
func swap(n1 *int ,n2 *int) {
t :=*n1
*n1 = *n2
*n2 = t
}
func main() {
a :=10
b :=20
swap(&a,&b)
fmt.Printf("a=%v,b=%v",a,b) //a=20,b=10
}