classSolution { public: voidsort( conststd::string& input_file_name, conststd::string& output_file_name){ std::vector<int> arr; int number = 0; std::ifstream fin(input_file_name); while(fin >> number) { arr.push_back(number); } qsort(&arr, 0, arr.size() - 1); std::ofstream fout(output_file_name); for (int i = 0; i < arr.size(); ++i) { fout << arr[i] << std::endl; } }
private: voidqsort(std::vector<int>* arr, int p, int r){ if (p >= r) { return; } int q = partition(arr, p, r); qsort(arr, p, q - 1); qsort(arr, q + 1, r); }
intpartition(std::vector<int>* arr, int p, int r){ int i = p; int j = i - 1; for (; i < r; ++i) { if (arr->at(i) < arr->at(r)) { ++j; std::swap(arr->at(i) , arr->at(j)); } } ++j; std::swap(arr->at(j) , arr->at(r)); return j; } };
import sys import random defswap(list, i, j): tmp = list[i] list[i] = list[j] list[j] = tmp
defgenerate(): number_limit = int(sys.argv[1]) n = int(sys.argv[2]) list = [] for i inrange(number_limit): list.append(i) f = open('numbers_input.txt', 'w') for i inrange(n): random_index = random.randint(i, number_limit - 1) swap(list, i, random_index) f.write(str(list[i]) + '\n') f.close() generate()