/*****************************************************************//** * \file SortingAlgorithms.h * \brief 排序 * \ IDE vs 2022 C++ 20 * \author geovindu * \date September 28 2023 *********************************************************************/ #pragma once #ifndef SORTHINALGORITHMS_H #define SORTHINALGORITHMS_H #include <iostream> #include <vector> using namespace std; namespace GeovinDu { /** * 排序. */ class SortingAlgorithms { private: public: /** * . * * \param a * \param b */ //void swap(int* a, int* b); /** * 1.Bubble Sort冒泡排序法 也可以用int array[] * * \param array int数组 * \param size 数组长度 */ void BubbleSort(int array[],int size); /** * . * * \param array */ void printArray(int array[], int size); /** * 1.Bubble Sort冒泡排序法. * , int size * \param arr * \param n */ void BubbleSort2(int arr[]); /** * 1.Bubble Sort冒泡排序法. * * \param a */ void BubbleSortVect(vector<int>& a); /** * . * * \param va */ void printVector(vector<int> va); /** * 2. Selection Sort选择排序. * * \param array * \param size */ void SelectionSort(int array[], int size); /** *3. Insertion Sort插入排序 . * * \param array * \param size */ void InsertionSort(int array[], int size); }; } #endif
/*****************************************************************//** * \file SortingAlgorithms.cpp * \brief 排序 * * \ IDE vs 2022 C++ 20 * \author geovindu * \date September 28 2023 *********************************************************************/ #include <iostream> #include<stdio.h> #include<stdlib.h> #include<stdbool.h> #include <vector> #include "SortingAlgorithms.h" using namespace std; namespace GeovinDu { /** * . * * \param a * \param b */ /*void SortingAlgorithms::swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; }*/ /** * 1.Bubble Sort冒泡排序法 也可以用int array[] * 用法有问题 * \param array int数组 * \param size 数组长度 */ void SortingAlgorithms::BubbleSort(int array[],int size) { //int size = sizeof(array) / sizeof(array[0]); // loop to access each array element for (int step = 0; step < size; ++step) { // loop to compare array elements for (int i = 0; i < size - step; ++i) { // compare two adjacent elements // change > to < to sort in descending order if (array[i] > array[i + 1]) { // swapping elements if elements // are not in the intended order int temp = array[i]; array[i] = array[i + 1]; array[i + 1] = temp; } } } } /** * Bubble Sort冒泡排序法 . * , int size * \param array * \param size */ void SortingAlgorithms::BubbleSort2(int array[]) { int i, j; int size = sizeof(array) / sizeof(array[0]); bool swapped; for (i = 0; i < size - 1; i++) { swapped = false; for (j = 0; j < size - i - 1; j++) { if (array[j] > array[j + 1]) { swap(array[j], array[j + 1]); swapped = true; } } // If no two elements were swapped // by inner loop, then break if (swapped == false) break; } } /** * Bubble Sort冒泡排序法 . * * \param a */ void SortingAlgorithms::BubbleSortVect(vector<int>& a) { bool swap = true; while (swap) { swap = false; for (size_t i = 0; i < a.size() - 1; i++) { if (a[i] > a[i + 1]) { a[i] += a[i + 1]; a[i + 1] = a[i] - a[i + 1]; a[i] -= a[i + 1]; swap = true; } } } } /** * . * * \param array */ void SortingAlgorithms::printArray(int array[], int size) { // int size = sizeof(array) / sizeof(array[0]); // printf("%d",size); for (int i = 0; i < size; ++i) { cout << " " << array[i]; } cout << "\n"; } /** * . * * \param va */ void SortingAlgorithms::printVector(vector<int> va) { for (size_t i = 0; i < va.size(); i++) { cout << va[i] << " "; } cout << endl; } /** * 2. Selection Sort选择排序. * * \param array * \param size */ void SortingAlgorithms::SelectionSort(int array[], int size) { /* bool swapped; int i; for (int step = 0; step < size - 1; step++) { int min_idx = step; swapped = false; for (i = step + 1; i < size; i++) { // To sort in descending order, change > to < in this line. // Select the minimum element in each loop. if (array[i] < array[min_idx]) min_idx = i; } // put min at the correct position if (min_idx != i) swap(&array[min_idx], &array[step]); } */ int i, j, min_idx; // One by one move boundary of // unsorted subarray for (i = 0; i < size - 1; i++) { // Find the minimum element in // unsorted array min_idx = i; for (j = i + 1; j < size; j++) { if (array[j] < array[min_idx]) min_idx = j; } // Swap the found minimum element // with the first element if (min_idx != i) swap(array[min_idx], array[i]); } } /** * 3.Insertion Sort插入排序. * * \param array * \param size */ void SortingAlgorithms::InsertionSort(int array[], int size) { for (int step = 1; step < size; step++) { int key = array[step]; int j = step - 1; // Compare key with each element on the left of it until an element smaller than // it is found. // For descending order, change key<array[j] to key>array[j]. while (key < array[j] && j >= 0) { array[j + 1] = array[j]; --j; } array[j + 1] = key; } } }
/*****************************************************************//** * \file geovin.h * \brief 排序 * \ IDE vs 2022 C++ 20 * \author geovindu * \date September 28 2023 *********************************************************************/ #pragma once #ifndef GEOVIN_H #define GEOVIN_H #include <iostream> #include "SortingAlgorithms.h" using namespace std; namespace GeovinDu { /** * 排序实例 */ class geovin { private: public: /** * 1.Bubble Sort冒泡排序法. * */ void bubble(); /** * 2. Selection Sort选择排序.. * */ void Selection(); /** *3.Insertion Sort插入排序. * */ void Insertion(); }; } #endif
/*****************************************************************//** * \file geovin.cpp * \brief 排序 * \ IDE vs 2022 C++ 20 * \author geovindu * \date September 28 2023 *********************************************************************/ #include "geovin.h" #include <iostream> #include<stdio.h> #include<stdlib.h> #include<stdbool.h> #include <vector> #include "SortingAlgorithms.h" namespace GeovinDu { /** * 1.Bubble Sort冒泡排序法. * */ void geovin::bubble() { //非静态 SortingAlgorithms sort; int data[] = { -2, 45, 0, 11, -9 }; // find array's length int size = sizeof(data) / sizeof(data[0]); printf("%d", size); sort.BubbleSort2(data); cout << "1.冒泡排序法 Bubble Sorted Array in Ascending Order:\n"; sort.printArray(data,size); cout << "1.冒泡排序法 用vector:\n"; vector<int> va{ 2,9,0,4,5,1,8,7 }; sort.printVector(va); } /** * 2. Selection Sort选择排序.. * */ void geovin::Selection() { SortingAlgorithms sort; int geovindu[] = { 20, 12, 10, 15, 2 }; int size = sizeof(geovindu) / sizeof(geovindu[0]); sort.SelectionSort(geovindu, size); cout << "2.Selection Sorted array in Acsending Order:\n"; sort.printArray(geovindu, size); } /** * 3.Insertion Sort插入排序. * */ void geovin::Insertion() { SortingAlgorithms sort; int geovindu[] = { 9, 5, 1, 4, 3 }; int size = sizeof(geovindu) / sizeof(geovindu[0]); sort.InsertionSort(geovindu, size); cout << "3.Insertion Sorted array in ascending order:\n"; sort.printArray(geovindu, size); } }
调用:
/*****************************************************************//** * \file EssentialAlgorithms.cpp * \brief * IDE vs 2022 C++ 20 * \author geovindu * \date September 28 2023 *********************************************************************/ #define UNICODE #include <iostream> #include "geovin.h" using namespace std; using namespace GeovinDu; int main() { std::cout << "Hello World!涂聚文 Geovin Du,geovindu,学习C++\n"; geovin du; //1. du.bubble(); //2. du.Selection(); //3. du.Insertion(); } // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单 // 调试程序: F5 或调试 >“开始调试”菜单 // 入门使用技巧: // 1. 使用解决方案资源管理器窗口添加/管理文件 // 2. 使用团队资源管理器窗口连接到源代码管理 // 3. 使用输出窗口查看生成输出和其他消息 // 4. 使用错误列表窗口查看错误 // 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目 // 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件 #define _UNICODE
标签:Sorting,int,void,param,Algorithms,cpp,array,include,size From: https://www.cnblogs.com/geovindu/p/17735823.html