c: Sorting Algorithms
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 | /*****************************************************************/ /** * \file SortAlgorithm.h * \brief 业务操作方法 * IDE: VSCODE c11 https://github.com/hustcc/JS-Sorting-Algorithm/blob/master/2.selectionSort.md * https://www.programiz.com/dsa/counting-sort * https://www.geeksforgeeks.org/sorting-algorithms/ * \author geovindu,Geovin Du 涂聚文 * \date 2023-09-19 ***********************************************************************/ #ifndef SORTALGORITHM_H #define SORTALGORITHM_H #include <stdio.h> #include <stdlib.h> /** * @brief 1。Bubble Sort冒泡排序法 * @param data INT 数组 * @param lensize 数组长度 * * @return 排序好的数组 */ int * BubbleSort( int * data, int lensize); /** * @brief 2 C Program for Selection Sort 选择排序 * @param arr INT 数组 * @param size 数组长度 * * @return 返回说明 */ void SelectionSort( int array[], int size); /** * @brief 3 Insertion Sort插入排序 * @param array INT 数组 * @param size 数组长度 * * @return 返回说明 */ void InsertionSort( int array[], int size); /** * @brief 4 Quick Sort 快速排序 * @param array INT 数组 * @param start 数组长度开始值 * @param end 数组长度结束值 * @return 返回说明 */ void QuickSort( int array[], int start, int end); /** * @brief 5 Merge Sort 合并排序 * @param array INT 数组 * @param start 数组长度开始值 * @param end 数组长度结束值 * @return 返回说明 */ void MergeSort( int array[], int const begin, int const end); /** * @brief 6 Counting Sort 计数排序 * @param array INT 数组 * @param size 数组长度 * @return 返回说明 */ void CountingSort( int array[], int size); /** * @brief 7 Radix Sort 基数排序 * @param arr INT 数组 * @param size 数组长度 * @return 返回说明 */ void Radixsort( int array[], int size); /** * @brief 8 Bucket Sort 桶排序 * @param array INT 数组 * @return 返回说明 */ void BucketSort( int array[]); /** * @brief 9 Heap Sort 堆排序 * @param array INT 数组 * @param nsize 数组长度 * @return 返回说明 */ void HeapSort( int array[], int nsize); /** * @brief 10 Heap Sort 希尔排序 * @param array INT 数组 * @param nsize 数组长度 * @return 返回说明 */ void ShellSort( int array[], int nsize); /** * @brief 11 Linear Search 顺序查找(Linear/Sequential Search),也称为线性查找 * @param array INT 数组 * @param nsize 数组长度 * @param key 搜索的关键数字 * @return 返回数组的索引值 */ int LinearSearch( int array[], int nsize, int key); /** * @brief 12 Binary Search 二分搜索 * @param array INT 数组 * @param key 搜索的关键数字 * @param low 最小的长度的起始值 * @param high 最大的长度的结束值 * * @return 返回数组的索引值 */ int BinarySearch( int array[], int key, int low, int high); #endif //SORTALGORITHM_H |
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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 | /*****************************************************************/ /** * \file SortAlgorithm.c * \brief c Sorting Algorithms 业务操作方法 * \IDE VSCODE c11 https://stackoverflow.com/questions/71924077/configuring-task-json-and-launch-json-for-c-in-vs-code * https://www.programiz.com/dsa/counting-sort * https://www.geeksforgeeks.org/sorting-algorithms/ * 安装插件“Doxygen Documentation Generator”,用来生成注释。 * 安装插件”C/C++ Snippets”,用来生成文件头、代码块分割线等。 * \author geovindu,Geovin Du * \date 2023-09-19 ***********************************************************************/ #include <stdio.h> #include <stdlib.h> #define MAXSIZE 100 /** * @brief 1。Bubble Sort冒泡排序法 * @param data INT 数组 * @param lensize 数组长度 * @return 排序好的数组 */ int * BubbleSort( int * data, int lensize) { int i,j,tmp; int * newdate; /* 原始数据 */ //int lensize=sizeof(data) / sizeof(data [0]);//sizeof(data); //sizeof(data) / sizeof(data[0]);// printf ( "2共 長度是:%d " ,lensize); printf ( "冒泡排序法:\n原始数据为:" ); for (i=0;i<lensize;i++) printf ( "%3d" ,data[i]); printf ( "\n" ); for (i=(lensize-1);i>=0;i--) /* 扫描次数 */ { for (j=0;j<i;j++) /*比较、交换次数*/ { if (data[j]>data[j+1]) /* 比较相邻两数,如第一个数较大则交换 */ { tmp=data[j]; data[j]=data[j+1]; data[j+1]=tmp; } } printf ( "第 %d 次排序后的结果是:" ,lensize-i); /*把各次扫描后的结果打印出来*/ for (j=0;j<lensize;j++) printf ( "%3d" ,data[j]); printf ( "\n" ); } //printf("最终排序的结果为:"); for (i=0;i<lensize;i++) //newdate[i]=data[i]; printf ( "%3d" ,data[i]); printf ( "\n" ); return data; } void swap( int *a, int *b) //交換兩個變數 { int temp = *a; *a = *b; *b = temp; } /** * @brief 2 C Program for Selection Sort 选择排序 * @param array INT 数组 * @param size 数组长度 * * @return 返回说明 */ void SelectionSort( int array[], int len) { int i,j; for (i = 0 ; i < len - 1 ; i++) { int min = i; for (j = i + 1; j < len; j++) //走訪未排序的元素 if (array[j] < array[min]) //找到目前最小值 min = j; //紀錄最小值 swap(&array[min], &array[i]); //做交換 } } /** * @brief 3 Insertion Sort插入排序 * @param array INT 数组 * @param size 数组长度 * * @return 返回说明 */ void InsertionSort( int array[], int size){ // defining some iterables and variables int i, temp, j; // using the for-loop for (i = 1; i < size; i++){ // initializing the temp variable as value at index i from array temp = array[i]; // initializing another iterable value j = i - 1; // using the while loop for j >= 0 and arr[j] > temp while (j >= 0 && array[j] > temp){ // swapping the elements array[j + 1] = array[j]; j = j - 1; } array[j + 1] = temp; } } void qswap( int * a, int * b) { int t = *a; *a = *b; *b = t; } /** * @brief 4 Quick Sort 快速排序 * @param array INT 数组 * @param low 数组长度初始值 * @param high 数组长度长度值 * @return 返回说明 */ int partition( int array[], int low, int high) { // Choosing the pivot int pivot = array[high]; // Index of smaller element and indicates // the right position of pivot found so far int qi = (low - 1); for ( int j = low; j <= high - 1; j++) { // If current element is smaller than the pivot if (array[j] < pivot) { qi++; qswap(&array[qi], &array[j]); } } qswap(&array[qi + 1], &array[high]); return (qi + 1); } /** * @brief 4 Quick Sort 快速排序 * @param array INT 数组 * @param low 数组长度开始值 * @param high 数组长度结束值 * @return 返回说明 */ void QuickSort( int array[], int low, int high){ if (low < high) { // pi is partitioning index, arr[p] // is now at right place int pi = partition(array, low, high); // Separately sort elements before // partition and after partition QuickSort(array, low, pi - 1); QuickSort(array, pi + 1, high); } } void merge( int array[], int const left, int const mid, int const right) { int const subArrayOne = mid - left + 1; int const subArrayTwo = right - mid; // Create temp arrays int * leftArray =( int *) malloc (subArrayOne); // int [subArrayOne]; int * rightArray = ( int *) malloc (subArrayTwo); //int [subArrayTwo]; // Copy data to temp arrays leftArray[] and rightArray[] for ( int i = 0; i < subArrayOne; i++) leftArray[i] = array[left + i]; for ( int j = 0; j < subArrayTwo; j++) rightArray[j] = array[mid + 1 + j]; int indexOfSubArrayOne = 0; int indexOfSubArrayTwo = 0; int indexOfMergedArray = left; // Merge the temp arrays back into array[left..right] while (indexOfSubArrayOne < subArrayOne && indexOfSubArrayTwo < subArrayTwo) { if (leftArray[indexOfSubArrayOne] <= rightArray[indexOfSubArrayTwo]) { array[indexOfMergedArray] = leftArray[indexOfSubArrayOne]; indexOfSubArrayOne++; } else { array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo]; indexOfSubArrayTwo++; } indexOfMergedArray++; } // Copy the remaining elements of // left[], if there are any while (indexOfSubArrayOne < subArrayOne) { array[indexOfMergedArray] = leftArray[indexOfSubArrayOne]; indexOfSubArrayOne++; indexOfMergedArray++; } // Copy the remaining elements of // right[], if there are any while (indexOfSubArrayTwo < subArrayTwo) { array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo]; indexOfSubArrayTwo++; indexOfMergedArray++; } //delete[] leftArray; //delete[] rightArray; } /** * @brief 5 Merge Sort 合并/归并排序 * @param array INT 数组 * @param start 数组长度开始值 * @param end 数组长度结束值 * @return 返回说明 */ void MergeSort( int array[], int const begin, int const end) { if (begin >= end) return ; int mid = begin + (end - begin) / 2; MergeSort(array, begin, mid); MergeSort(array, mid + 1, end); merge(array, begin, mid, end); } /** * @brief 6 Counting Sort 计数排序 * @param array INT 数组 * @param size 数组长度 * @return 返回说明 */ void CountingSort( int array[], int size) { int output[10]; // Find the largest element of the array int max = array[0]; for ( int i = 1; i < size; i++) { if (array[i] > max) max = array[i]; } // The size of count must be at least (max+1) but // we cannot declare it as int count(max+1) in C as // it does not support dynamic memory allocation. // So, its size is provided statically. int count[10]; // Initialize count array with all zeros. for ( int i = 0; i <= max; ++i) { count[i] = 0; } // Store the count of each element for ( int i = 0; i < size; i++) { count[array[i]]++; } // Store the cummulative count of each array for ( int i = 1; i <= max; i++) { count[i] += count[i - 1]; } // Find the index of each element of the original array in count array, and // place the elements in output array for ( int i = size - 1; i >= 0; i--) { output[count[array[i]] - 1] = array[i]; count[array[i]]--; } // Copy the sorted elements into original array for ( int i = 0; i < size; i++) { array[i] = output[i]; } } int getMax( int array[], int n) { int max = array[0]; for ( int i = 1; i < n; i++) if (array[i] > max) max = array[i]; return max; } void radcountingSort( int array[], int size, int place) { int output[size + 1]; int max = (array[0] / place) % 10; for ( int i = 1; i < size; i++) { if (((array[i] / place) % 10) > max) max = array[i]; } int count[max + 1]; for ( int i = 0; i < max; ++i) count[i] = 0; // Calculate count of elements for ( int i = 0; i < size; i++) count[(array[i] / place) % 10]++; // Calculate cumulative count for ( int i = 1; i < 10; i++) count[i] += count[i - 1]; // Place the elements in sorted order for ( int i = size - 1; i >= 0; i--) { output[count[(array[i] / place) % 10] - 1] = array[i]; count[(array[i] / place) % 10]--; } for ( int i = 0; i < size; i++) array[i] = output[i]; } /** * @brief 7 Radix Sort 基数排序 * @param arr INT 数组 * @param size 数组长度 * @return 返回说明 */ void Radixsort( int array[], int size) { // Get maximum element int max = getMax(array, size); // Apply counting sort to sort elements based on place value. for ( int place = 1; max / place > 0; place *= 10) radcountingSort(array, size, place); } //这个数是有规则的 #define NARRAY 9 // Array size 7 #define NBUCKET 8 // Number of buckets 6 #define INTERVAL 12 // Each bucket capacity 10 struct Node { int data; struct Node *next; }; struct Node *BucketInsertionSort( struct Node *list); //void printBuckets(struct Node *list); int getBucketIndex( int value); /** * @brief 8 Radix Sort 基数排序 * @param array INT 数组 * @return 返回说明 */ void BucketSort( int array[]) { int i, j; struct Node **buckets; // Create buckets and allocate memory size buckets = ( struct Node **) malloc ( sizeof ( struct Node *) * NBUCKET); // Initialize empty buckets for (i = 0; i < NBUCKET; ++i) { buckets[i] = NULL; } // Fill the buckets with respective elements for (i = 0; i < NARRAY; ++i) { struct Node *current; int pos = getBucketIndex(array[i]); current = ( struct Node *) malloc ( sizeof ( struct Node)); current->data = array[i]; current->next = buckets[pos]; buckets[pos] = current; } // Print the buckets along with their elements //for (i = 0; i < NBUCKET; i++) { //printf("Bucket[%d]: ", i); //printBuckets(buckets[i]); //printf("\n"); //} // Sort the elements of each bucket for (i = 0; i < NBUCKET; ++i) { buckets[i] = BucketInsertionSort(buckets[i]); } //printf("-------------\n"); //printf("Bucktets after sorting\n"); // for (i = 0; i < NBUCKET; i++) { //printf("Bucket[%d]: ", i); //printBuckets(buckets[i]); //printf("\n"); // } // Put sorted elements on arr for (j = 0, i = 0; i < NBUCKET; ++i) { struct Node *node; node = buckets[i]; while (node) { array[j++] = node->data; node = node->next; } } return ; } struct Node* BucketInsertionSort( struct Node *list) { struct Node *k, *nodeList; if (list == 0 || list->next == 0) { return list; } nodeList = list; k = list->next; nodeList->next = 0; while (k != 0) { struct Node *ptr; if (nodeList->data > k->data) { struct Node *tmp; tmp = k; k = k->next; tmp->next = nodeList; nodeList = tmp; continue ; } for (ptr = nodeList; ptr->next != 0; ptr = ptr->next) { if (ptr->next->data > k->data) break ; } if (ptr->next != 0) { struct Node *tmp; tmp = k; k = k->next; tmp->next = ptr->next; ptr->next = tmp; continue ; } else { ptr->next = k; k = k->next; ptr->next->next = 0; continue ; } } return nodeList; } int getBucketIndex( int value) { return value / INTERVAL; } void printBuckets( struct Node *list) { struct Node *cur = list; while (cur) { printf ( "%d " , cur->data); cur = cur->next; } } void heapify( int arr[], int n, int i) { // Find largest among root, left child and right child int largest = i; int left = 2 * i + 1; int right = 2 * i + 2; if (left < n && arr[left] > arr[largest]) largest = left; if (right < n && arr[right] > arr[largest]) largest = right; // Swap and continue heapifying if root is not largest if (largest != i) { swap(&arr[i], &arr[largest]); heapify(arr, n, largest); } } /** * @brief 9 Heap Sort 堆排序 * @param array INT 数组 * @param nsize 数组长度 * @return 返回说明 */ void HeapSort( int array[], int nsize) { // Build max heap for ( int i = nsize / 2 - 1; i >= 0; i--) heapify(array,nsize, i); // Heap sort for ( int i = nsize - 1; i >= 0; i--) { swap(&array[0], &array[i]); // Heapify root element to get highest element at root again heapify(array, i, 0); } } /** * @brief 10 Heap Sort 希尔排序 * @param array INT 数组 * @param nsize 数组长度 * @return 返回说明 */ void ShellSort( int array[], int nsize) { // Rearrange elements at each n/2, n/4, n/8, ... intervals for ( int gap = nsize/2; gap > 0; gap /= 2) { // Do a gapped insertion sort for this gap size. // The first gap elements a[0..gap-1] are already in gapped order // keep adding one more element until the entire array is // gap sorted for ( int i = gap; i < nsize; i += 1) { // add a[i] to the elements that have been gap sorted // save a[i] in temp and make a hole at position i int temp = array[i]; // shift earlier gap-sorted elements up until the correct // location for a[i] is found int j; for (j = i; j >= gap && array[j - gap] > temp; j -= gap) array[j] = array[j - gap]; // put temp (the original a[i]) in its correct location array[j] = temp; } } /* for (int interval = n / 2; interval > 0; interval /= 2) { for (int i = interval; i < n; i += 1) { int temp = array[i]; int j; for (j = i; j >= interval && array[j - interval] > temp; j -= interval) { array[j] = array[j - interval]; } array[j] = temp; } } */ } /** * @brief 11 Linear Search 顺序查找(Linear/Sequential Search),也称为线性查找 * @param array INT 数组 * @param nsize 数组长度 * @param key 搜索的关键数字 * @return 返回数组的索引值 */ int LinearSearch( int array[], int nsize, int key) { // Going through array sequencially for ( int i = 0; i < nsize; i++) if (array[i] == key) return i; return -1; } /** * @brief 12 Binary Search 二分搜索 * @param array INT 数组 * @param key 搜索的关键数字 * @param low 最小的长度的起始值 * @param high 最大的长度的结束值 * * @return 返回数组的索引值 */ int BinarySearch( int array[], int key, int low, int high) { // Repeat until the pointers low and high meet each other while (low <= high) { int mid = low + (high - low) / 2; if (array[mid] == key) return mid; if (array[mid] < key) low = mid + 1; else high = mid - 1; } return -1; } |
调用:
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 | /* * @Author: 涂聚文 geovindu,Geovin Du * @Date: 2023-09-11 14:07:29 * @LastEditors: * @LastEditTime: 2023-09-20 14:35:49 * @FilePath: \testcpp\helloword.c * @Description: */ /*****************************************************************/ /** * \file helloworld.C * \brief 业务操作方法 * \IDE: VSCODE c11 安装插件“Doxygen Documentation Generator”,用来生成注释。 安装插件”C/C++ Snippets”,用来生成文件头、代码块分割线等。KoroFileHeader C/C++ Snippets插件设置 https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-support-arriving-in-msvc/ * \author geovindu,Geovin Du * \date 2023-09-19 * \copyright * \namespace * ***********************************************************************/ #include<string.h> #include<stdio.h> #include<stdlib.h> #include "include/SortAlgorithm.h" int main() { printf ( "hello world, c \n" ); printf ( "你好,中国\n" ); printf ( "文件名:%s" __FILE__); printf ( "\n当前行号:%d" ,__LINE__); printf ( "\n日期:%s" ,__DATE__); printf ( "\n时间:%s\n" ,__TIME__); int i; int *p; char str[20]; //1冒泡排序 int data[12]={60,50,39,27,12,8,45,63,20,2,10,88}; /* 原始数据 */ int lensize= sizeof (data) / sizeof (data [0]); //sizeof(data); p=BubbleSort(data,lensize); itoa(lensize, str, 10); printf ( "\n1共長度是 %d " ,lensize); printf ( "\n1冒泡排序的结果为:" ); for (i=0;i<lensize;i++) printf ( "%3d" ,p[i]); printf ( "\n" ); //2选择排序 int arr[] = { 64, 25, 12, 22, 11,88,28,100 }; int n = sizeof (arr) / sizeof (arr[0]); SelectionSort(arr, n); int ii; printf ( "2选择排序结果为:" ); for (ii = 0; ii < n; ii++) printf ( "%d " , arr[ii]); printf ( "\n" ); //3插入排序 int inarr[] = {25, 23, 28, 16, 18,100,8,99}; // calculating the size of array int size = sizeof (inarr) / sizeof (inarr[0]); printf ( "3插入排序结果为:" ); InsertionSort(inarr, size); for (ii = 0; ii < n; ii++) printf ( "%d " , inarr[ii]); printf ( "\n" ); //4快速排序 // defining and initializing an array int qsarr[] = {100,25, 23, 28, 16, 18,8,99,3,20}; printf ( "4快速排序结果为:" ); // calculating the size of array size = sizeof (qsarr) / sizeof (qsarr[0]); QuickSort(qsarr, 0, size - 1); for ( int i = 0; i < size; i++) printf ( "%d " , qsarr[i]); printf ( "\n" ); //5 合并排序 printf ( "5合并排序结果为:" ); int mearr[] = { 12, 11, 23, 55, 6, 57,3,100,9 }; int arr_size = sizeof (mearr) / sizeof (mearr[0]); MergeSort(mearr, 0, arr_size - 1); for ( int i = 0; i < arr_size; i++) printf ( "%d " , mearr[i]); printf ( "\n" ); //6 计数排序 printf ( "6计数排序结果为:" ); int carray[] = {4, 2, 2, 8, 3, 3, 1}; int cn = sizeof (carray) / sizeof (carray[0]); CountingSort(carray, cn); for ( int i = 0; i < cn; i++) printf ( "%d " , carray[i]); printf ( "\n" ); //7. 基数排序 printf ( "7基数排序结果为:" ); int rarray[] = {121, 432, 564, 23, 1, 45, 788}; int rn = sizeof (rarray) / sizeof (rarray[0]); Radixsort(rarray, rn); for ( int i = 0; i < rn; i++) printf ( "%d " , rarray[i]); printf ( "\n" ); //8 Bucket Sort 桶排序 printf ( "8桶排序结果为:" ); int barray[] = {42, 32, 33, 5,52, 37,100, 47, 51}; BucketSort(barray); int bn = sizeof (barray) / sizeof (barray[0]); for ( int i = 0; i < bn; i++) printf ( "%d " , barray[i]); printf ( "\n" ); //9堆排序 printf ( "9堆排序结果为:" ); int harr[] = {1, 12, 9, 5, 6, 10}; int hn = sizeof (harr) / sizeof (harr[0]); HeapSort(harr, hn); for ( int i = 0; i < hn; i++) printf ( "%d " , harr[i]); printf ( "\n" ); //10.希尔排序 printf ( "10.希尔排序结果为:" ); int sdata[] = {9, 8, 3, 7, 25, 6, 4, 11,38}; int ssize = sizeof (sdata) / sizeof (sdata[0]); ShellSort(sdata, ssize); for ( int i = 0; i < ssize; i++) printf ( "%d " , sdata[i]); printf ( "\n" ); //11 顺序查找(Linear/Sequential Search),也称为线性查找 printf ( "11.顺序查找结果为:" ); int lsdata[] = {9, 8, 3, 7, 25, 6, 4, 11,38}; int key=25; //要查找的数字 int lsize = sizeof (lsdata) / sizeof (lsdata[0]); int result = LinearSearch(lsdata, lsize,key); (result == -1) ? printf ( "\nElement not found" ) : printf ( "\nElement found at index(数组中的索引号是:): %d\n" , result); //12 Binary Search 二分搜索 printf ( "\n12.二分搜索结果为:\n" ); int bsarray[] = {3, 4, 5, 6, 7, 8, 9}; int bsize = sizeof (bsarray) / sizeof (bsarray[0]); int xkey = 8; int bresult = BinarySearch(bsarray, xkey, 0, bsize - 1); if (bresult == -1) printf ( "Not found" ); else printf ( "Element is found at index(数组中的索引号是:) %d\n" , bresult); system ( "pause" ); // linux 无效 ,只win 下有效 return 0; } |
https://www.programiz.com/dsa/counting-sort
https://www.geeksforgeeks.org/sorting-algorithms/
https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-support-arriving-in-msvc/
https://codedocs.org/what-is/c17-c-standard-revision
http://www.open-std.org/jtc1/sc22/wg14/www/docs/
http://www.open-std.org/jtc1/sc22/wg14/www/
http://www.open-std.org/jtc1/sc22/wg14/
https://learn.microsoft.com/en-us/cpp/overview/install-c17-support?view=msvc-170
https://en.cppreference.com/w/c/17
https://en.cppreference.com/w/c/11
cpp-docs/docs/c-language/alignment-c.md at main · MicrosoftDocs/cpp-docs (github.com)
https://github.com/MicrosoftDocs/cpp-docs/blob/main/docs/c-language/alignment-c.md
https://iso-9899.info/wiki/The_Standard
https://en.cppreference.com/w/c/23
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2022-09-20 java: Bridge Pattern
2022-09-20 CSharp: Adapter Patterns