原文為幾段不完整的代碼片段,經整理補全如下:
void quicksort(int a[], int low, int high) {
if (low >= high) return;
int pivot = a[low], i = low, j = high;
while (i < j) {
while (i < j && a[j] >= pivot) j--;
if (i < j) a[i++] = a[j];
while (i < j && a[i] <= pivot) i++;
if (i < j) a[j--] = a[i];
}
a[i] = pivot;
quicksort(a, low, i-1);
quicksort(a, i+1, high);
}
#include <stdlib.h>
#include <time.h>
#define N 20
int main() {
int a[N];
srand(time(NULL));
for (int i=0; i
// 交換
void swap(int *a, int *b) { int t=*a; *a=*b; *b=t; }
// 打印
void printArray(int a[], int n) {
for (int i=0; i
rand() 未 srand() → 每次產生相同序列。qsort。這些片段涵蓋了 C 語言陣列操作、排序、隨機數的基礎模式,適合初學者練習指標、遞歸與標準庫用法。