cpp: Sorting Algorithms
Programming in C++ (mheducation.com)
https://highered.mheducation.com/sites/0072424125/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | /*****************************************************************/ /** * \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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | /*****************************************************************/ /** * \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; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | /*****************************************************************/ /** * \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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | /*****************************************************************/ /** * \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); } } |
调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | /*****************************************************************/ /** * \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 |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
Cpp programming
标签:
算法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2022-09-28 CSharp: Command Pattern
2022-09-28 Java: Memento Pattern
2018-09-28 MySQL5.7: Paging using Mysql Stored Proc