Created
September 8, 2018 07:19
-
-
Save penut85420/c346d9fdc54368c8ab2a05c6e1218685 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() { | |
int arr[10] = {9, 5, 7, 3, 1, 2, 8, 6, 4, 0}; | |
int i, j, t, m; //, p; // 如何不使用 p 這個變數完成選擇演算法? | |
for (i = 0; i < 10; i++) { | |
m = arr[0]; // 紀錄陣列中最大值為多少 | |
p = 0; // 記錄此最大值位於陣列的第幾個元素 | |
for (j = 1; j < 10 - i; j++) { | |
if (arr[j] > m) { | |
m = arr[j]; | |
p = j; | |
} | |
} | |
t = arr[10-i-1]; | |
arr[10-i-1] = m; | |
arr[p] = t; | |
} | |
for (i = 0; i < 10; i++) | |
printf("%d ", i); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment