고쳤다능 ㅠ.ㅠ
소스코드보기
| #include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
template <class BidirectionalIterator>
void duplex_selection_sort(BidirectionalIterator first, BidirectionalIterator last);
int main(int argc, char *argv[]) {
using namespace std;
vector<int> array0, array1, array2;
ostream_iterator<int> output(cout, " ");
for(int i = 1; i <= 10; i++) {
array0.push_back(i);
array1.push_back(i);
array2.insert(array2.begin(), i);
}
random_shuffle(array0.begin(), array0.end());
cout << "before sort" << endl;
copy(array0.begin(), array0.end(), output);
cout << endl;
copy(array1.begin(), array1.end(), output);
cout << endl;
copy(array2.begin(), array2.end(), output);
cout << endl;
duplex_selection_sort(array0.begin(), array0.end());
duplex_selection_sort(array1.begin(), array1.end());
duplex_selection_sort(array2.begin(), array2.end());
cout << "\nafter sort" << endl;
copy(array0.begin(), array0.end(), output);
cout << endl;
copy(array1.begin(), array1.end(), output);
cout << endl;
copy(array2.begin(), array2.end(), output);
cout << endl;
return EXIT_SUCCESS;
}
template <class BidirectionalIterator>
void duplex_selection_sort(BidirectionalIterator first, BidirectionalIterator last) {
BidirectionalIterator min, max;
while(last > first) {
min = max = first;
for(BidirectionalIterator i = first; i != last; i++) {
if(*min > *i)
min = i;
if(*max < *i)
max = i;
}
if( (max == first) && (min == last - 1) ) {
std::iter_swap(first, last - 1);
}
else if(max == first) {
std::iter_swap(max, last - 1);
std::iter_swap(first, min);
}
else {
std::iter_swap(min, first);
std::iter_swap(max, last - 1);
}
first++;
last--;
}
}
|
Output: | before sort
5 4 8 9 1 6 3 2 7 10
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
after sort
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
|
댓글을 달아 주세요
버그있어요,
정렬된 배열은 제대로 처리가 안되요.
헐..그런듯
내일 고쳐봐야겠심
9 10 8 7 6 5 4 3 2 1 Fail!
나 왜이러지..요즘 술을 너무 많이 먹나
뻔이 틀린걸 그냥 올려놨네 -_-;;