Guest User

Untitled

a guest
Oct 18th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Sorting {
  5. /* Merge sorting algorithm */
  6. static class MergeSort {
  7. private static List<int> Merge(ref List<int> a, ref List<int> b) {
  8. /* Merge the two sorted lists */
  9. List<int> merged = new List<int>();
  10. while((a.Count > 0) && (b.Count > 0)) {
  11. /* We always compare the first two elements of the lists against each other.
  12. * The smaller element gets added to the sorted list, and removed from its original list. */
  13. if (a[0] < b[0]) {
  14. /* The element on list a is smaller than on b.
  15. * We add it to the merged list and remove it from "a". */
  16. merged.Add(a[0]);
  17. a.RemoveAt(0);
  18. }
  19. else {
  20. /* This time b is the smaller element. It goes first. */
  21. merged.Add(b[0]);
  22. b.RemoveAt(0);
  23. }
  24. }
  25. if (a.Count == 0) {
  26. /* a is empty. Add the rest of b */
  27. merged.AddRange(b);
  28. }
  29. else {
  30. /* b is empty. Add the rest of a */
  31. merged.AddRange(a);
  32. }
  33. /* Return the merged list */
  34. return merged;
  35. }
  36.  
  37. public static List<int> Sort(List<int> elements) {
  38. /* Here we split the big list "elements" into two smaller lists
  39. * using recursion until the list only has one element in it.
  40. * A one-element list is sorted by default.
  41. * After we get a list with one element, we start merging upwards until
  42. * we get one big sorted list. */
  43. if (elements.Count > 1) {
  44. int cutoff = elements.Count / 2;
  45. // List.GetRange(int start, int amount); Get <amount> elements from <start>
  46. List<int> a = Sort(elements.GetRange(0, cutoff));
  47. List<int> b = Sort(elements.GetRange(cutoff, elements.Count - cutoff));
  48. return Merge(ref a, ref b);
  49. }
  50. else {
  51. /* Return the list */
  52. return elements;
  53. }
  54. }
  55. public static string Sort(string line) {
  56. /* Overloaded method for Sort().
  57. * Convert the string to a List<int> based on the Unicode values for each character,
  58. * then pass that to the List<int> Sort() method. */
  59. List<int> unicodeValues = new List<int>();
  60. foreach(char letter in line) {
  61. // Cast to int and add to the elements list
  62. unicodeValues.Add((int)letter);
  63. }
  64. // Call the "original" Sort() method
  65. List<int> sortedUnicodeValues = Sort(unicodeValues);
  66.  
  67. // Create a new char array of sortedUnicodeValues.Count length
  68. char[] sortedChars = new char[sortedUnicodeValues.Count];
  69.  
  70. for(int i = 0; i < sortedUnicodeValues.Count; i++) {
  71. // Cast all unicode values (ints) to char
  72. sortedChars[i] = (char)sortedUnicodeValues[i];
  73. }
  74. /* Convert the sortedChars char array to a single string */
  75. string sortedString = new string(sortedChars);
  76. return sortedString;
  77. }
  78. }
  79.  
  80. /* Insertion sorting algorithm */
  81. static class InsertionSort {
  82. public static List<int> Sort(List<int> elements) {
  83. if (elements.Count < 2) {
  84. /* Less than 2 elements. Sorted by default */
  85. return elements;
  86. }
  87. /* Take each element on the list and
  88. * repeatedly compare it against all elements before it,
  89. * until the element being compared against it is larger than, or equals it. */
  90. int x, finalPosition;
  91. for(int i = 1; i < elements.Count; i++) {
  92. // i = 1 because we need to start at the second index.
  93. // It makes no sense to compare the element at index 0 against itself ;)
  94. int element = elements[i];
  95. finalPosition = i;
  96. for(x = i-1; x >= 0 && element < elements[x]; x--) {
  97. /* Start comparing the element at index i against the first one before it (i-1).
  98. * As long as x is larger than or equals 0, and the element at index i
  99. * is smaller than the one at index x, we set the final insertion position to x's current position,
  100. * and continue to loop backwards (x--) */
  101. finalPosition = x;
  102. }
  103. /* After the nested loop, x is the final index on which to insert the element. */
  104. elements.RemoveAt(i);
  105. elements.Insert(finalPosition, element);
  106. }
  107. return elements;
  108. }
  109. }
  110. }
Add Comment
Please, Sign In to add comment