# 排序方法

雖然現在有許多的程式語言都有內建排序的函式可以輕易地把一連串的資料快速地排列出順序，但是排序方法的演算流程，仍然是給初學者動動腦筋很有趣的素材，在這一小節中，就來瞭解2個簡單的排序方法的思維過程吧。

### 交換兩個變數中的內容

假設我們有2個變數，分別是 a=100, b=200，在傳統的程式語言中，想要把它們的內容進行交換，需要使用以下的指令：

```
#include <stdio.h>

int main()
{
    int a, b, temp;
    a = 100;
    b = 200;
    temp = a;
    a = b;
    b = temp;
    printf("%d, %d\n", a, b);

    return 0;
}
```

&#x20;但是如果是Python語言的話，因為它支援多變數同時設定值的功能，因此在交換變數內容的地方就簡單多了：

```
a, b = 100, 200
a, b = b, a
```

&#x20;但是，如果要交換的資料內容是在串列中的值，則在操作上會有一點變化，傳統的程式語言操作如下（C語言）：

```
#include <stdio.h>

int main()
{
    int a[2], temp;
    a[0] = 100;
    a[1] = 200;
    temp = a[0];
    a[0] = a[1];
    a[1] = temp;
    printf("%d, %d\n", a[0], a[1]);

    return 0;
}
```

Python語言的操作方式如下：

```
a = [100, 200]
a[0], a[1] = a[1], a[0]
```

### 取得清單中的最大及最小值

同樣地，在Python及許多程式語言中也有使用一個函式直接取出清單或串列中所有元素的最大值、最小值、平均值等功能。但是，如果使用積木的話，該如何做呢？請參考以下的積木程式：

![](https://3758851535-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LMo8PFR2UvTkWwSK8m0%2F-MaQoKE6UjDpA8LwLdbw%2F-MaQwI2t4_LX8xn3XEdc%2Fimage.png?alt=media\&token=cb48aca1-0b2d-43a0-b9cf-720c3908d921)

### 選擇排序法 Selection Sort

假設我們要把一串數字從大排到小，則選擇排序法會每次都會固定住一個數字，然後找出其餘數字中最大的和它比較，如果該數字比它大就進行交換，否則就不換。請參考以下的說明：

![](https://3758851535-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LMo8PFR2UvTkWwSK8m0%2F-MaQmRNKYDqN1_X0vk0h%2F-MaQnkk6uI1iLdBsfLPk%2Fimage.png?alt=media\&token=4e5c2063-ac79-4900-bdce-5036937af69a)

### 氣泡排序法 Bubble Sort

以下是氣泡排序法的演算過程：

![](https://3758851535-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LMo8PFR2UvTkWwSK8m0%2F-MaQmRNKYDqN1_X0vk0h%2F-MaQo1fRejnh0hQ4JYnX%2Fimage.png?alt=media\&token=3dcca0da-a1d3-47a0-a8f1-b7e4a75ce7c6)

![](https://3758851535-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LMo8PFR2UvTkWwSK8m0%2F-MaQmRNKYDqN1_X0vk0h%2F-MaQo73o_BoL-G5bWjqE%2Fimage.png?alt=media\&token=5136f25e-2108-4065-9031-47c364c808ab)

![](https://3758851535-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LMo8PFR2UvTkWwSK8m0%2F-MaQmRNKYDqN1_X0vk0h%2F-MaQoCF3Jd-R_HMybQ9_%2Fimage.png?alt=media\&token=c9c7e2b2-0e15-4a1a-a1ca-297da76ea1f5)

![](https://3758851535-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LMo8PFR2UvTkWwSK8m0%2F-MaQmRNKYDqN1_X0vk0h%2F-MaQoGXEWvYI0fFz0BPr%2Fimage.png?alt=media\&token=13f2b15f-3bf5-410d-ad22-1ff90405c615)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nkust.gitbook.io/csx/pai-xu-fang-fa.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
