上傳檔案到「260202」
This commit is contained in:
24
260202/快速排序.cpp
Normal file
24
260202/快速排序.cpp
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#include<iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
int n;
|
||||||
|
int a[n];
|
||||||
|
cin >> n;
|
||||||
|
for (int i=0;i<n;i++)
|
||||||
|
cin >> a[i];
|
||||||
|
// 选择排序:给第 i 个位置找最合适的(最小的)数
|
||||||
|
for (int i = 0; i < n - 1; i++) {
|
||||||
|
int minidx = i; // 1. 先记下当前位置
|
||||||
|
for (int j = i + 1; j < n; j++) {
|
||||||
|
if (a[j] < a[minidx]) { // 2. 发现更小的了
|
||||||
|
minidx = j; // 3. 只是记下它的位置,不交换
|
||||||
|
}
|
||||||
|
}
|
||||||
|
swap(a[i], a[minidx]); // 4. 全看完后,只换这一次
|
||||||
|
}
|
||||||
|
for(int i=0;i<n;i++)
|
||||||
|
cout<<a[i]<<" ";
|
||||||
|
cout<<endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user