Files
BasicsOfComputerSoftwareEng…/OOP/12/Excercise01.cpp
2023-05-17 09:48:55 +08:00

58 lines
1.3 KiB
C++

#include <iostream>
using std::cout;
using std::endl;
template <class T>
void swap(T list[], const int left, const int right) {
T tmp = list[left];
list[left] = list[right];
list[right] = tmp;
}
template <class T>
T *sort(T originList[], const int left, const int right) {
int l = left, r = right;
int pivot = left;
if (left >= right) {
return originList;
}
while (l < r) {
while (originList[r] >= originList[pivot] && r > l) {
r--;
}
swap(originList, pivot, r);
pivot = r;
while (originList[l] <= originList[pivot] && l < r) {
l++;
}
swap(originList, pivot, l);
pivot = l;
}
sort(originList, left, pivot - 1);
sort(originList, pivot + 1, right);
return originList;
}
int main() {
int intList[6] = {1, 6, 4, 5, 2, 3};
char charList[14] = "Muelsyse.com!";
for (int i = 0; i < 6; i++) {
cout << intList[i] << ' ';
}
cout << endl;
for (int i = 0; i < 13; i++) {
cout << charList[i] << ' ';
}
cout << endl;
sort(intList, 0, 5);
sort(charList, 0, 9);
for (int i = 0; i < 6; i++) {
cout << intList[i] << ' ';
}
cout << endl;
for (int i = 0; i < 13; i++) {
cout << charList[i] << ' ';
}
cout << endl;
return 0;
}