In this tutorial we will be looking at working with Arrays in C#. In my time as a .Net developer I have been asked many times, and seen many questions in online programming forums, how to accomplish certain tasks when working with arrays, such as a bubble sort, selection sort and many searching questions, that is the reason I decided to write this tutorial. For some, especially new programmers, working with and manipulating arrays can seem like a daunting task. I will cover several functions used for working with arrays, functions like:
- Linear Search of an array
- Binary search of an array
- Selection sort on an array
- Bubble sort of an array
- Find the smallest & largest value in an array
The code I am providing in this tutorial is tested and tried, and it is also very well commented to make it easier for the newer programmers to read it and see what is going on. Working with arrays, like I stated before, can seem like a very daunting task for new programmers. That really couldn't be further from the truth, yes it can take some time to get the hang of, but all it takes is practice, and understanding how looping structures in an array work. That is what I will be demonstrating in this tutorial, so lets get to some code.
The code offered here was originally written for a console application, but can easily be ported over to a Windows application, the logic for each method is the same. As with any .Net application we need to make sure we have references to the proper Namespaces so we have access to what we need. For this particular console application we only need 3 Namespaces
So lets bring in our references
csharp
Search An ArrayFor these examples we will be using integer arrays, but the logic is the same for strings and other data types. First we will look at a couple methods for searching for values in an array. The first method is doing a
Linear Search. Here we will prompt the user for the size they want their array. From there we will ask them for the values in their array, with each value provided we will loop the size of the array, checking for numeric values.
Once all their values are in the array we will then ask them what value to search for, also checking to make sure they provided a numeric value. From there we will check each value of the array, in the order it was provided, looking for a match. If we find a match we let them know the location in the array where the match was found (the array index), otherwise we let them know the value wasn't found:
csharp
The next search type we will utilize is the
Binary Search method. As with the linear search, we will follow the first few steps exactly when it comes to getting the array size and the array values. It is when we start the search that things get radically different. With the binary search we first find the highest, lowest and middle values in our array. We do this to help narrow down the location of the requested value.
On each iteration of the loop, the lowest, highest and middle values are altered based on the current value. We do this until we either find a match, or reach the end of the array, we then act accordingly:
csharp
As far as search algorithms those are the 2 most common, so we will not go further into search algorithms. We will now switch our focus to sorting an array.
Sorting An ArrayWhen it comes to sorting an array, or any kind of list really, there are many different sorting algorithms. In this tutorial we will look at 3
- Selection Sort
- Bubble Sort
- Insertion Sort
Selection SortA
Selection Sort works by selecting the smallest unsorted item remaining in the list, and then swapping it with the item in the next position to be filled. A selection sort is a fast and easy sorting algorithm to implement, but isn't really efficient when it comes to sorting a large list. So lets look at how we would perform a selection fort on an integer array:
csharp
Bubble SortThough the
Bubble Sort is the oldest and easiest sorting algorithm to use, it is also the slowest, making it unappealing when it comes to large lists and arrays.
The bubble sort compares each item in the list with the next item (the one next to it), and swapping them if required. The algorithm repeats this process until it makes a pass all the way through the list without swapping any items (in other words, all items are in the correct order). This causes larger values to "bubble" to the end of the list while smaller values "sink" towards the beginning of the list. Here is how a bubble sort in C# would be implemented:
csharp
Insertion SortThe
Insertion Sort works like its name implies, it inserts each item into its proper place in the sorted array. The insertion sort algorithm is almost twice as fast, thus far more efficient, than the bubble sort. The most common implementation, as we will be seeing shortly, is to use an in place sorting system, where in each iteration the value is inserted into it's right place, until the entire array has been sorted. This is how an insertion sort would be implemented in C#:
csharp
The final item we will look at, and one there are many questions about, is finding the lowest and highest value in an integer array. Here we will, as with the sort and search methods we've looked at, will prompt the user for the size of their array and the values they want in their array. We will them loop through the array, examining each value. If the current value is higher than the previous value then the highest number variable is updated with that value, same goes with the current value being lower than the previous value.
We continue this format until we have reached the end of our array. Once we have reached the end of our array we will have the values for the highest and the lowest values in the array. Here is how that process would look like:
csharp
There you have it, that is how you work with arrays, well as far as searching and sorting arrays. There are many other search and sorting algorithms out there, some more efficient, some less efficient, but this tutorial should at least give you a good foundation for going further on this topic. I hope you found this tutorial useful and informative, and I thank you for reading.
Happy Coding!
This post has been edited by PsychoCoder: 17 Mar, 2008 - 06:24 PM