GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Array-sorting/sorting/sorting/SortingSmart.cs at main · Mika-dot/Array-sorting Expand file tree/ Copy pathSortingSmart.csMore file actions 980 lines (863 loc) · 30.4 KB / Copy pathSortingSmart.csFile metadata and controls980 lines (863 loc) · 30.4 KB 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 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 using System; using System.Collections.Generic; using System.Linq; namespace sorting { class TreeNode //простая реализация бинарного дерева { public TreeNode(int data) { Data = data; } public int Data { get; set; } //данные public TreeNode Left { get; set; } //левая ветка дерева public TreeNode Right { get; set; } //правая ветка дерева public void Insert(TreeNode node) //рекурсивное добавление узла в дерево { if (node.Data < Data) { if (Left == null) { Left = node; } else { Left.Insert(node); } } else { if (Right == null) { Right = node; } else { Right.Insert(node); } } } public int[] Transform(List<int> elements = null) //преобразование дерева в отсортированный массив { if (elements == null) { elements = new List<int>(); } if (Left != null) { Left.Transform(elements); } elements.Add(Data); if (Right != null) { Right.Transform(elements); } return elements.ToArray(); } } public class SortingSmart { static void Merge(int[] array, int lowIndex, int middleIndex, int highIndex) //метод для слияния массивов { var left = lowIndex; var right = middleIndex + 1; var tempArray = new int[highIndex - lowIndex + 1]; var index = 0; while ((left <= middleIndex) && (right <= highIndex)) { if (array[left] < array[right]) { tempArray[index] = array[left]; left++; } else { tempArray[index] = array[right]; right++; } index++; } for (var i = left; i <= middleIndex; i++) { tempArray[index] = array[i]; index++; } for (var i = right; i <= highIndex; i++) { tempArray[index] = array[i]; index++; } for (var i = 0; i < tempArray.Length; i++) { array[lowIndex + i] = tempArray[i]; } } static int[] MergeSort(int[] array, int lowIndex, int highIndex) //сортировка слиянием { if (lowIndex < highIndex) { var middleIndex = (lowIndex + highIndex) / 2; MergeSort(array, lowIndex, middleIndex); MergeSort(array, middleIndex + 1, highIndex); Merge(array, lowIndex, middleIndex, highIndex); } return array; } static int IndexOfMax(int[] array, int n)//метод для получения индекса максимального элемента подмассива { int result = 0; for (var i = 1; i <= n; ++i) { if (array[i] > array[result]) { result = i; } } return result; } static int IndexOfMin(int[] array, int n) //метод поиска позиции минимального элемента подмассива, начиная с позиции n { int result = n; for (var i = n; i < array.Length; ++i) { if (array[i] < array[result]) { result = i; } } return result; } static void Flip(int[] array, int end)//метод для переворота массива { for (var start = 0; start < end; start++, end--) { var temp = array[start]; array[start] = array[end]; array[end] = temp; } } static void Swap(ref int e1, ref int e2) //метод обмена элементов { var temp = e1; e1 = e2; e2 = temp; } static int Partition(int[] array, int minIndex, int maxIndex) //метод возвращающий индекс опорного элемента { var pivot = minIndex - 1; for (var i = minIndex; i < maxIndex; i++) { if (array[i] < array[maxIndex]) { pivot++; Swap(ref array[pivot], ref array[i]); } } pivot++; Swap(ref array[pivot], ref array[maxIndex]); return pivot; } static int[] QuickSort(int[] array, int minIndex, int maxIndex) //быстрая сортировка { if (minIndex >= maxIndex) { return array; } var pivotIndex = Partition(array, minIndex, maxIndex); QuickSort(array, minIndex, pivotIndex - 1); QuickSort(array, pivotIndex + 1, maxIndex); return array; } static int GetNextStep(int s) //метод для генерации следующего шага { s = s * 1000 / 1247; return s > 1 ? s : 1; } static void Heapify(int[] arr, int heapSize, int heapRoot) { int max = heapRoot; int leftChildIndex = 2 * heapRoot + 1; int rightChildIndex = 2 * heapRoot + 2; if (leftChildIndex < heapSize && arr[leftChildIndex] > arr[max]) max = leftChildIndex; if (rightChildIndex < heapSize && arr[rightChildIndex] > arr[max]) max = rightChildIndex; if (max != heapRoot) { int swap = arr[heapRoot]; arr[heapRoot] = arr[max]; arr[max] = swap; Heapify(arr, heapSize, max); } } private static void SwapBogo(ref int[] arr, int u, int v) { int temp = arr[u]; arr[u] = arr[v]; arr[v] = temp; } private static bool sorted(int[] arr) { bool ret = true; for (int i = 0; i < arr.Length - 1; i++) { if (arr[i] > arr[i + 1]) { ret = false; break; } } return ret; } private static void insertionSort(int[] arr, int left, int right) { for (int i = left + 1; i <= right; i++) { int temp = arr[i]; int j = i - 1; while (j >= left && arr[j] > temp) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = temp; } } private static void merge(int[] arr, int l, int m, int r) { // original array is broken in two parts // left and right array int len1 = m - l + 1, len2 = r - m; int[] left = new int[len1]; int[] right = new int[len2]; for (int x = 0; x < len1; x++) left[x] = arr[l + x]; for (int x = 0; x < len2; x++) right[x] = arr[m + 1 + x]; int i = 0; int j = 0; int k = l; // after comparing, we merge those two array // in larger sub array while (i < len1 && j < len2) { if (left[i] <= right[j]) { arr[k] = left[i]; i++; } else { arr[k] = right[j]; j++; } k++; } // copy remaining elements of left, if any while (i < len1) { arr[k] = left[i]; k++; i++; } // copy remaining element of right, if any while (j < len2) { arr[k] = right[j]; k++; j++; } } private static T[] InitializeArray<T>(int length) where T : new() { T[] array = new T[length]; for (int i = 0; i < length; ++i) { array[i] = new T(); } return array; } private static int[] MSDRadix(int[] array, int l, int r, int d, int[] temp) { if (l >= r) { return new int[0]; } const int k = 256; var count = new int[k + 2]; for (var i = l; i <= r; i++) { var j = Key(array[i]); count[j + 2]++; } for (var i = 1; i < count.Length; i++) { count[i] += count[i - 1]; } for (var i = l; i <= r; i++) { var j = Key(array[i]); temp[count[j + 1]++] = array[i]; } for (var i = l; i <= r; i++) { array[i] = temp[i - l]; } for (var i = 0; i < k; i++) { MSDRadix(array, l + count[i], l + count[i + 1] - 1, d + 1, temp); } int Key(int s) => d >= s ? -1 : s; return array; } // ----------------------------------------------------------------- public static void Bubble(ref int[] array) //сортировка пузырьком { var len = array.Length; for (var i = 1; i < len; i++) { for (var j = 0; j < len - i; j++) { if (array[j] > array[j + 1]) { Swap(ref array[j], ref array[j + 1]); } } } } public static void Shaker(ref int[] array) //сортировка перемешиванием { for (var i = 0; i < array.Length / 2; i++) { var swapFlag = false; //проход слева направо for (var j = i; j < array.Length - i - 1; j++) { if (array[j] > array[j + 1]) { Swap(ref array[j], ref array[j + 1]); swapFlag = true; } } //проход справа налево for (var j = array.Length - 2 - i; j > i; j--) { if (array[j - 1] > array[j]) { Swap(ref array[j - 1], ref array[j]); swapFlag = true; } } //если обменов не было выходим if (!swapFlag) { break; } } } public static void Insertion(ref int[] array) //сортировка вставками { int n = array.Length; for (int i = 1; i < n; ++i) { int key = array[i]; int j = i - 1; // Move elements of arr[0..i-1], // that are greater than key, // to one position ahead of // their current position while (j >= 0 && array[j] > key) { array[j + 1] = array[j]; j = j - 1; } array[j + 1] = key; } } public static void Stooge(ref int[] array, int startIndex, int endIndex) //сортировка по частям { if (array[startIndex] > array[endIndex]) { Swap(ref array[startIndex], ref array[endIndex]); } if (endIndex - startIndex > 1) { var len = (endIndex - startIndex + 1) / 3; Stooge(ref array, startIndex, endIndex - len); Stooge(ref array, startIndex + len, endIndex); Stooge(ref array, startIndex, endIndex - len); } } public static void Stooge(ref int[] array) //сортировка по частям { Stooge(ref array, 0, array.Length - 1); } public static void Pancake(ref int[] array) //блинная сортировка { for (var subArrayLength = array.Length - 1; subArrayLength >= 0; subArrayLength--) { //получаем позицию максимального элемента подмассива var indexOfMax = IndexOfMax(array, subArrayLength); if (indexOfMax != subArrayLength) { //переворот массива до индекса максимального элемента //максимальный элемент подмассива расположится вначале Flip(array, indexOfMax); //переворот всего подмассива Flip(array, subArrayLength); } } } public static void Shell(ref int[] array) //сортировки Шелла { //расстояние между элементами, которые сравниваются var d = array.Length / 2; while (d >= 1) { for (var i = d; i < array.Length; i++) { var j = i; while ((j >= d) && (array[j - d] > array[j])) { Swap(ref array[j], ref array[j - d]); j = j - d; } } d = d / 2; } } public static void Merge(ref int[] array) //сортировка слиянием { MergeSort(array, 0, array.Length - 1); } public static void Selection(ref int[] array, int currentIndex = 0) //сортировка выбором { if (currentIndex == array.Length) return; var index = IndexOfMin(array, currentIndex); if (index != currentIndex) { Swap(ref array[index], ref array[currentIndex]); } Selection(ref array, currentIndex + 1); } public static void Quick(ref int[] array) //сортировка Хоара { QuickSort(array, 0, array.Length - 1); } public static void Gnome(ref int[] unsortedArray) //Гномья сортировка { var index = 1; var nextIndex = index + 1; while (index < unsortedArray.Length) { if (unsortedArray[index - 1] < unsortedArray[index]) { index = nextIndex; nextIndex++; } else { Swap(ref unsortedArray[index - 1], ref unsortedArray[index]); index--; if (index == 0) { index = nextIndex; nextIndex++; } } } } public static void Tree(ref int[] array) //метод для сортировки с помощью двоичного дерева { var treeNode = new TreeNode(array[0]); for (int i = 1; i < array.Length; i++) { treeNode.Insert(new TreeNode(array[i])); } array = treeNode.Transform(); } public static void Comb(ref int[] array) //Сортировка расческой { var arrayLength = array.Length; var currentStep = arrayLength - 1; while (currentStep > 1) { for (int i = 0; i + currentStep < array.Length; i++) { if (array[i] > array[i + currentStep]) { Swap(ref array[i], ref array[i + currentStep]); } } currentStep = GetNextStep(currentStep); } //сортировка пузырьком for (var i = 1; i < arrayLength; i++) { var swapFlag = false; for (var j = 0; j < arrayLength - i; j++) { if (array[j] > array[j + 1]) { Swap(ref array[j], ref array[j + 1]); swapFlag = true; } } if (!swapFlag) { break; } } } public static void BasicCounting(ref int[] array) //простой вариант сортировки подсчетом { int n = array.Length; int max = 0; for (int i = 0; i < n; i++) { if (max < array[i]) { max = array[i]; } } int[] freq = new int[max + 1]; for (int i = 0; i < max + 1; i++) { freq[i] = 0; } for (int i = 0; i < n; i++) { freq[array[i]]++; } for (int i = 0, j = 0; i <= max; i++) { while (freq[i] > 0) { array[j] = i; j++; freq[i]--; } } } public static void CombinedBubble(ref int[] array) { int length = array.Length; int temp = array[0]; for (int i = 0; i < length; i++) { for (int j = i + 1; j < length; j++) { if (array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } public static void Heapify(ref int[] array) // Heapify { for (int i = array.Length / 2 - 1; i >= 0; i--) Heapify(array, array.Length, i); for (int i = array.Length - 1; i >= 0; i--) { int temp = array[0]; array[0] = array[i]; array[i] = temp; Heapify(array, i, 0); } } public static void Cocktail(ref int[] array) // cocktail { int start = 0; int end = array.Length - 1; int temp; bool swapped = true; while (swapped) { swapped = false; for (int i = 0; i < end; i++) { if (array[i] > array[i + 1]) { temp = array[i]; array[i] = array[i + 1]; array[i + 1] = temp; swapped = true; } } if (!swapped) { break; } swapped = false; end -= 1; for (int i = end; i >= start; i--) { if (array[i] > array[i + 1]) { temp = array[i]; array[i] = array[i + 1]; array[i + 1] = temp; swapped = true; } } start += 1; } } public static void OddEven(ref int[] array) { //Initialization int Flag = 0; int temp = 0; //Initialize flag =0 or f!=1 while (Flag == 0) { /*Initialize Flag is 1 When both if condiotion is false so the flag remain 1 and the while loop is terminate*/ Flag = 1; //use Even Loop for comparing even idexes of an array for (int i = 0; i < array.Length - 1; i += 2) { /* Use if conditon for comparing adjacents elements if they are in wrong order than swap*/ if (array[i] > array[i + 1]) { temp = array[i]; array[i] = array[i + 1]; array[i + 1] = temp; //This Flag variable is always remain 0 when if condition is true Flag = 0; } } //use Odd Loop for comparing odd idexes of an array for (int i = 1; i < array.Length - 1; i += 2) { /* Use if conditon for comparing adjacents elements if they are in wrong order than swap*/ if (array[i] > array[i + 1]) { //This Flag variable is always remain 0 when if condition is true temp = array[i]; array[i] = array[i + 1]; array[i + 1] = temp; Flag = 0; } } } //return sorted array } // OddEven public static void BinaryInsertion(ref int[] array) { int count = 0; for (int i = 0; i < array.Length; i++) { int tmp = array[i]; int left = 0; int right = i - 1; while (left <= right) { int m = (left + right) / 2; //определение индекса среднего элемента if (tmp < array[m]) right = m - 1; // сдвиг правой else left = m + 1; //или левой границы count++; } for (int j = i - 1; j >= left; j--) { array[j + 1] = array[j]; // сдвиг элементов // count++; } array[left] = tmp; // вставка элемента на нужное место } } // BinaryInsertion public static void Bogo(ref int[] array) { while (!sorted(array)) { Random rdm = new Random(); for (int i = 0; i < array.Length; i++) { { for (int u = 0; u < array.Length; u++) { SwapBogo(ref array, u, rdm.Next(0, array.Length - 1)); } } } } } // Bogo public static void Cycle(ref int[] array) { for (int i = 0; i < array.Length - 1; i++) { int item = array[i]; int pos = i; for (int j = i + 1; j < array.Length; j++) { if (array[j] < item) { pos++; } } if (pos == i) { continue; } while (item == array[pos]) { pos++; } int var = item; item = array[pos]; array[pos] = var; while (pos != i) { pos = i; for (int j = i + 1; j < array.Length; j++) { if (array[j] < item) { pos++; } } while (item == array[pos]) { pos++; } int temp = item; item = array[pos]; array[pos] = temp; } } } // Cycle public static void Exchange(ref int[] array) { for (int i = 0; i < array.Length; i++) { for (int j = i; j < array.Length; j++) { if (array[j] < array[i]) { int container = array[j]; array[j] = array[i]; array[i] = container; } } } } // Exchange public static void Heap(ref int[] array) { int n = array.Length; for (int i = n / 2 - 1; i >= 0; i--) Heapify(array, n, i); for (int i = n - 1; i > 0; i--) { int temp = array[0]; array[0] = array[i]; array[i] = temp; Heapify(array, i, 0); } } // Heap public static void Tim(ref int[] array) { int RUN = 32; for (int i = 0; i < array.Length; i += RUN) insertionSort(array, i, Math.Min((i + 31), (array.Length - 1))); for (int size = RUN; size < array.Length; size = 2 * size) { for (int left = 0; left < array.Length; left += 2 * size) { int mid = left + size - 1; int right = Math.Min((left + 2 * size - 1), (array.Length - 1)); merge(array, left, mid, right); } } } // Tim public static void Counting(ref int[] array) { int min = 0; int max = 0; for (int i = 0; i < array.Length; i++) { if (array[i] < min) min = array[i]; if (array[i] > max) max = array[i]; } int[] count = new int[max - min + 1]; int z = 0; for (int i = 0; i < count.Length; i++) count[i] = 0; for (int i = 0; i < array.Length; i++) count[array[i] - min]++; for (int i = min; i <= max; i++) { while (count[i - min]-- > 0) { array[z] = i; ++z; } } } // Counting public static void Bucket(ref int[] array) { int minValue = array[0]; int maxValue = array[0]; for (int i = 1; i < array.Length; i++) { if (array[i] > maxValue) maxValue = array[i]; if (array[i] < minValue) minValue = array[i]; } List<int>[] bucket = new List<int>[maxValue - minValue + 1]; for (int i = 0; i < bucket.Length; i++) { bucket[i] = new List<int>(); } for (int i = 0; i < array.Length; i++) { bucket[array[i] - minValue].Add(array[i]); } int k = 0; for (int i = 0; i < bucket.Length; i++) { if (bucket[i].Count > 0) { for (int j = 0; j < bucket[i].Count; j++) { array[k] = bucket[i][j]; k++; } } } } // Bucket public static void Radix(ref int[] array) { var numbers = new Queue<int>(array); var buckets = InitializeArray<Queue<int>>(10); int m = 10; int n = 1; for (int i = numbers.Max().ToString().Length; i > 0; i--) { while (numbers.Count > 0) { buckets[numbers.Peek() % m / n].Enqueue(numbers.Dequeue()); } foreach (Queue<int> bucket in buckets) { while (bucket.Count > 0) { numbers.Enqueue(bucket.Dequeue()); } } m *= 10; n *= 10; } array = numbers.ToArray(); } // Radix public static void MSDRadix(ref int[] array) => MSDRadix(array, 0, array.Length - 1, 0, new int[array.Length]); // MSDRadix } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |