Topic Q - Simple Algorithms
Swapping
The simplest of all algorithms is the swap, where you basically have two variables or objects whose values you want to switch. The key requirement for a swap, of course, is that the data types of the two variables/objects are the same.
For example, if you have two strings, varA and varB, with the values of “Matt” and “Mallory” respectively, and you want to swap the two values, you would need to introduce an additional variable to temporarily hold one of the values.
Code Samples
Basic Swapping Principles
The following is a method demonstrating a simple swap of two items within a single method.
1 private static void SimpleSwap()
2 {
3 // A simple swap
4 string varA = "Matt";
5 string varB = "Mallory";
6
7 Console.WriteLine($"Attempting to swap {varA} and {varB}.");
8
9 // The following will NOT work
10 varA = varB;
11 varB = varA;
12 Console.WriteLine($"Results: {varA} and {varB}");
13
14 // Reset & try again
15 varA = "Matt";
16 varB = "Mallory";
17
18 Console.WriteLine($"Second attempt to swap {varA} and {varB}.");
19
20 // A temporary variable is required.
21 string temp;
22 temp = varA;
23 varA = varB;
24 varB = temp;
25 Console.WriteLine($"Results: {varA} and {varB}");
26 }
Swapping With Reference variables
The following is a demo of trying to use a method to swap two variables. For a method swap to work, you need to pass in the variables by reference (the ref keyword).
1 private static void DemoSwapMethods()
2 {
3 // Variable setup
4 string varA = "Matt";
5 string varB = "Mallory";
6
7 Console.WriteLine($"Attempting to swap {varA} and {varB}.");
8 BadSwap(varA, varB);
9 Console.WriteLine($"Results of BadSwap method: {varA} and {varB}");
10 GoodSwap(ref varA, ref varB);
11 Console.WriteLine($"Results of GoodSwap method: {varA} and {varB}");
12 }
13
14 private static void BadSwap(string a, string b)
15 {
16 // DON'T use this method...
17 string temp;
18 temp = a;
19 a = b;
20 b = temp;
21 }
22
23 private static void GoodSwap(ref string a, ref string b)
24 {
25 string temp;
26 temp = a;
27 a = b;
28 b = temp;
29 }
This is true not only for built-in data types (int, string, etc.), but for classes as well. Take, for example, this simple class called Person.
1 public class Person
2 {
3 public string Name { get; set; }
4 public Person(string name)
5 {
6 Name = name;
7 }
8 public override string ToString()
9 {
10 return "My name is " + Name;
11 }
12 }
When swapping two whole objects in a separate method, we would need to pass those objects by reference.
1 private static void DemoObjectSwapMethods()
2 {
3 // Variable setup
4 Person varA = new Person("Matt");
5 Person varB = new Person("Mallory");
6
7 Console.WriteLine($"Attempting to swap {varA} and {varB}.");
8 BadObjectSwap(varA, varB);
9 Console.WriteLine($"Results of BadSwap method: {varA} and {varB}");
10 GoodObjectSwap(ref varA, ref varB);
11 Console.WriteLine($"Results of GoodSwap method: {varA} and {varB}");
12 }
13
14 private static void BadObjectSwap(Person a, Person b)
15 {
16 // Only the local variables are swapped
17 Person temp;
18 temp = a;
19 a = b;
20 b = temp;
21 }
22
23 private static void GoodObjectSwap(ref Person a, ref Person b)
24 {
25 Person temp;
26 temp = a;
27 a = b;
28 b = temp;
29 }
Array Element Swapping
The following is a demo of swapping two elements in an array by using a method. Note that there is no need for a ref keyword in the parameters; that is because an array variable is a reference type, and any changes to that reference variable’s members (the elements in the array) will affect the variable passed in. Therefore, when you pass an array to a method you are effectively sending in the whole array.
1 private static void DemoArrayElementSwap()
2 {
3 string[] studioC_CastMembers = { "Whitney Call", "Mallory Everton", "Jason G\
4 ray", "Matt Meese", "Adam Berg", "Stacey Harkey", "Natalie Madsen", "Stephen Mee\
5 k", "James Perry", "Jeremy Warner" };
6 Console.WriteLine($"Going to swap {studioC_CastMembers[0]} and {studioC_Cast\
7 Members[1]}");
8 SwapElements(studioC_CastMembers, 0, 1);
9 Console.WriteLine($"Results of array swap: {studioC_CastMembers[0]} and {stu\
10 dioC_CastMembers[1]}");
11 }
12
13 private static void SwapElements(string[] theArray, int indexA, int indexB)
14 {
15 string temp;
16 temp = theArray[indexA];
17 theArray[indexA] = theArray[indexB];
18 theArray[indexB] = temp;
19 }
Sorting