int KthMin::findKthMin(IDoubleList03 * data, int k) { IDoubleNode03* head = data->getHead(); std::vector<int> vec; while (head) { vec.push_back(head->getValue()); head = head->getNext(); } //for (size_t i = 0; i < vec.size(); i++) { // cout << i << "th, num is " << vec[i] << endl; //} HeapSort sorter; sorter.sort(vec); return vec[k]; int index = 0; int min = -1; k += 1; for (size_t i = 0; i < k; ++i) { if (vec.empty()) return min; min = vec[0]; for (size_t j = 0; j < vec.size(); ++j) { if (vec[j] < min) { min = vec[j]; index = j; } } vec.erase(vec.begin() + index); } return min; }
int main() { //待排数据输入方式: /*int N = 0; cout << "排序数据个数:\n"; cin >> N; int* A = new int[N]; cout << "请输入待排序的数据:\n"; for (int i = 0; i < N; i++) { cin >> A[i]; }*/ //数据直接给定 int B[N] = { 1, 6, 3, 5, 2, 4 }; int C[13] = { 54, 35, 48, 36, 27, 12, 44, 44, 8, 14, 26, 17, 2 }; int* A = C; //从文件中读取,大量数据,计算时间复杂度 printResult("待排原始数据:", C, N); BubbleSort bubble; bubble.bubbleSort(A,N); printResult("bubbleSort", A, N); SelectionSort select; select.selectionSort(A, N); printResult("selectSort", A, N); InsertionSort insert; insert.insertionSort(A, N); printResult("InsetSort", A, N); MergeSort merge; merge.mergeSort(A, N); printResult("MergeSort", A, N); QuickSort qucik; qucik.quickSort(A, N); printResult("QucikSort",A,N); QuickSort2 qucik2; qucik2.quickSort(A, N); printResult("QucikSort2", A, N); HeapSort heap; heap.heapSort(A, N); printResult("heapSort", A, N); HeapSort2 heap2; heap2.heapSort(A, N); printResult("heapSort2", A, N); ShellSort shell; shell.shellSort(A,N); printResult("shellSort", A, N); return 0; }
int main() { HeapSort heap; heap.insertItem(2); heap.insertItem(5); heap.insertItem(7); heap.insertItem(16); heap.displayHeap(); return 0; }
int main() { int array[] = {4, 8, 23, 1, 5, 9, 12, 0}; HeapSort<int> h = HeapSort<int>(array, 8); h.heapSort(); for (int i=0; i<8; i++) { cout << array[i] << " "; } cout << endl; return 0; }
int main(){ int N,number; cin>>N; HeapSort hs; for (int i = 0; i < N; i++) { cin>>number; hs.insert(number); } hs.sort(); for (int i = 0; i < N; i++) { if(i){ cout<<' '; } cout<<hs[i]; } cout<<endl; return 0; }
int main() { srand(time(NULL)); int a[50] = {14,7,15,2,8,8,12,12,10,17}; for (int i = 0; i < 20; ++i) { a[i] = rand() % 20; printf("%d ", a[i]); } HeapSort<int> hs; if(0 != hs.build(a, 20)) { printf("fail to build heap"); return -1; } printf("\n"); printf("max is:%d\n", *(hs.max())); printf("min is:%d\n", *(hs.min())); return 0; }
BinaryTreeChromosome* TradingSystem::PerformAnalysis( const std::vector<Candlestick>& candlesticks, const std::vector<BaseIndicator *>& indicators, unsigned populationCount, unsigned generationCount, unsigned selectionAmount, double leafValueMutationProbability, double leafSignMutationProbability, double logicalNodeMutationProbability, double leafIndicatorMutationProbability, double crossoverProbability, BinaryTreeChromosome* chromosomeToStartWith, std::function<void(double fitness, BinaryTreeChromosome * chromosome, int generation)> update ) { srand(static_cast<unsigned int>(time(nullptr))); std::vector<IndicatorTuple> dataSet = EvaluateCandlesticks(candlesticks, indicators); // Initialization std::vector<BinaryTreeChromosome *> front_buffer = std::vector<BinaryTreeChromosome *>(); std::vector<BinaryTreeChromosome *> back_buffer = std::vector<BinaryTreeChromosome *>(); BinaryTreeFitness fitness(&(EvaluateFitness), &dataSet); BinaryTreeGeneticAlgo selection = BinaryTreeGeneticAlgo( selectionAmount, leafValueMutationProbability, leafSignMutationProbability, logicalNodeMutationProbability, leafIndicatorMutationProbability, crossoverProbability ); for (unsigned y = 0; y < populationCount; y++) { front_buffer.push_back(new BinaryTreeChromosome()); back_buffer.push_back(new BinaryTreeChromosome()); } for (unsigned i = 0; i < populationCount; i++) { front_buffer[i]->GenerateTree(3, indicators); back_buffer[i]->GenerateTree(3, indicators); front_buffer[i]->setFitness(0); back_buffer[i]->setFitness(0); } if (chromosomeToStartWith != nullptr) { for (unsigned i = 0; i < populationCount; i++) { chromosomeToStartWith->copyTo(front_buffer[i]); chromosomeToStartWith->copyTo(back_buffer[i]); if (i != 0) { front_buffer[i]->Mutate( leafValueMutationProbability, leafSignMutationProbability, logicalNodeMutationProbability, crossoverProbability, leafIndicatorMutationProbability ); back_buffer[i]->Mutate( leafValueMutationProbability, leafSignMutationProbability, logicalNodeMutationProbability, crossoverProbability, leafIndicatorMutationProbability ); } } } HeapSort heapSort; std::vector<BinaryTreeChromosome*>* p_front_buffer = &front_buffer; std::vector<BinaryTreeChromosome*>* p_back_buffer = &back_buffer; std::vector<BinaryTreeChromosome*>* tmp2; for (unsigned y = 0; y < generationCount; y++) { fitness.CalculateFitness(p_front_buffer); tmp2 = p_front_buffer; p_front_buffer = p_back_buffer; p_back_buffer = tmp2; heapSort.Sort(p_back_buffer, populationCount); update(p_back_buffer->at(populationCount - 1)->getFitness(), p_back_buffer->at(populationCount - 1), y + 1 /* Start with 1 */); // Selection selection.Select(p_front_buffer, p_back_buffer, populationCount); } fitness.CalculateFitness(p_front_buffer); heapSort.Sort(p_front_buffer, populationCount); BinaryTreeChromosome* bestFit = new BinaryTreeChromosome(p_front_buffer->at(populationCount - 1)); for (unsigned i = 0; i < populationCount; i++) { delete front_buffer[i]; delete back_buffer[i]; } return bestFit; } // TradingSystem::PerformAnalysis
void RunHeapSort() { HeapSort heapSort; heapSort.AddData(19); heapSort.AddData(8); heapSort.AddData(2); heapSort.AddData(1); heapSort.AddData(24); heapSort.AddData(29); heapSort.AddData(3); heapSort.AddData(23); heapSort.AddData(234); heapSort.AddData(32); heapSort.Sort(); heapSort.PrintResult(); }
void TestSortingAlgos() { try { BubbleSort testBObj; int arrB[ARRAY_SIZE] = { 15,3,12,10,1,9,6,11,5,4 }; testBObj.LoadData(arrB, ARRAY_SIZE); testBObj.Print(); testBObj.Sort(); cout << "Bubble Sort Output:"; testBObj.Print(); SelectionSort testSObj; int arrS[ARRAY_SIZE] = { 15,3,12,10,1,9,6,11,5,4 }; testSObj.LoadData(arrS, ARRAY_SIZE); testSObj.Print(); testSObj.Sort(); cout << "Selection Sort Output:"; testSObj.Print(); InsertionSort testIObj; int arrI[ARRAY_SIZE] = { 15,3,12,10,1,9,6,11,5,4 }; testIObj.LoadData(arrI, ARRAY_SIZE); testIObj.Print(); testIObj.Sort(); cout << "Insertion Sort Output:"; testIObj.Print(); MergeSort testMObj; int arrM[ARRAY_SIZE] = { 15,3,12,10,1,9,6,11,5,4 }; testMObj.LoadData(arrM, ARRAY_SIZE); testMObj.Print(); testMObj.Sort(); cout << "Insertion Sort Output:"; testMObj.Print(); QuickSort testQObj; int arrQ[] = { 15,3,12,10,1,9,6,11,5,4, 12, 8,1, -23, 87,45, 12, 423 }; testQObj.LoadData(arrQ, sizeof(arrQ)/sizeof(int)); testQObj.Print(); testQObj.Sort(); cout << "Quick Sort Output:"; testQObj.Print(); HeapSort testHObj; int arrH[] = { 15,3,12,10,1,9,6,11,5,4, 12, 8,1, -23, 87,45, 12, 423 }; testHObj.LoadData(arrH, sizeof(arrH) / sizeof(int)); testHObj.Print(); testHObj.Sort(); cout << "Heap Sort Output:"; testHObj.Print(); QuickSortRandomized testQRObj; int arrQR[] = { 15,3,12,10,1,9,6,11,5,4, 12, 8,1, -23, 87,45, 12, 423 }; testQRObj.LoadData(arrQR, sizeof(arrQR) / sizeof(int)); testQRObj.Print(); testQRObj.Sort(); cout << "Heap Sort Output:"; testQRObj.Print(); HeapSortRevised testHSRObj; int arrHSR[] = { 15,3,12,10,1,9,6,11,5,4, 12, 8,1, -23, 87,45, 12, 423 }; testHSRObj.LoadData(arrHSR, sizeof(arrHSR) / sizeof(int)); testHSRObj.Print(); testHSRObj.Sort(); cout << "Heap Sort Output:"; testHSRObj.Print(); } catch (const std::exception& E) { cerr << "Caught exception \"" << E.what() << "\"\n"; } catch (...) { cerr << "Caught unknown exception"; } }