Many C# books start with the "Hello World" program. Reading through several chapters of these books, users still do not find enough information to implement mathematically oriented programs. In our first C# example, we start with implementation of two commonly used sorting algorithms: bubble-sort and quick-sort. Either of these two algorithms will sort the given number of floating-point numbers in ascending order. By going through the code, we hope you will learn
Our sorting code implementation is listed below:
using System;
using System.IO;
using System.Diagnostics;
namespace Sorting
{
class Program
{
static void Main(string[] args)
{
int i, j, n = 0, num_loops = 1000;
double duration;
double[] dValues, dSortedValues;
Stopwatch stopWatch = new Stopwatch();
string line = string.Empty;
TextReader infile = new StreamReader("sort_data.d1");
if (infile != null)
{
// Read n floating-point numbers from file
line = infile.ReadLine();
while (line.Length == 0)
{
line = infile.ReadLine();
}
if (line.Length != 0)
{
n = int.Parse(line);
}
dValues = new double[n];
dSortedValues = new double[n];
for (i = 0; i < n; i++)
{
line = infile.ReadLine();
while (line.Length == 0)
{
line = infile.ReadLine();
}
dValues[i] = double.Parse(line);
}
infile.Close();
// Test speed of bubble sort by repeatedly calling BubbleSort
stopWatch.Start();
for (i = 0; i < num_loops; i++)
{
for (j = 0; j < n; j++)
{
// Reset values to call BubbleSort
dSortedValues[j] = dValues[j];
}
BubbleSort(n, dSortedValues);
}
stopWatch.Stop();
duration = (double)stopWatch.ElapsedMilliseconds;
Console.WriteLine("Bubble sorting CPU: {0:F4}", duration / 1000.0);
// Test speed of quick sort by repeatedly calling QuickSort
stopWatch.Reset();
stopWatch.Start();
for (i = 0; i < num_loops; i++)
{
for (j = 0; j < n; j++)
{
// Reset values to call QuickSort
dSortedValues[j] = dValues[j];
}
QuickSort(n, dSortedValues);
}
stopWatch.Stop();
duration = (double)stopWatch.ElapsedMilliseconds;
Console.WriteLine("Quick sorting CPU: {0:F4}", duration / 1000.0);
// Write sorted data to a file
TextWriter tw = new StreamWriter("sort_data.o1");
if (tw != null)
{
for (i = 0; i < n; i++)
{
tw.WriteLine("{0:F8} or {0:E15}", dSortedValues[i]);
}
tw.Close();
}
}
}
static void BubbleSort(int n, double[] dValues)
{
int i, j;
double temp;
for (i=0; i dValues[j])
{
temp = dValues[i];
dValues[i] = dValues[j];
dValues[j] = temp;
}
}
}
}
static void QuickSort(int n, double[] dValues)
{
RecursiveQSort(dValues, 0, n - 1);
}
static void RecursiveQSort(double[] a, int n_left, int n_right)
{
int i, j;
double pivot;
if (n_left >= n_right)
return;
i = n_left;
j = n_right;
// Choose the last element as pivot
pivot = a[j];
while (i < j)
{
while (i < j && a[i] <= pivot) i++;
while (i < j && a[j] >= pivot) j--;
if (i < j)
{
Swap(ref a[i], ref a[j]);
}
}
if (i != n_right)
{
Swap(ref a[i], ref a[n_right]);
}
// sort sub-array recursively
RecursiveQSort(a, n_left, i - 1);
RecursiveQSort(a, i + 1, n_right);
}
static void Swap(ref double x, ref double y)
{
double temp = y;
y = x;
x = temp;
}
}
}
You can copy the above code list and save it to a file named CSharp_1.cs in your working folder. You then type the following at the DOS command line:
Working_folder>csc /o CSharp_1.cs
After completion of compiling, you will see Sorting.exe in the same folder. By providing data to be sorted in a ASCII (text) file sort_data.d1, you can run the program and obtained the sorted data in sort_data.o1.
Based on the above code list, we try to answer the questions asked at the beginning of this document.
10.0
35.0
2.0
.
.
.
In C# we have not found a method equivalent to C fscanf that reads in formatted data. Therefore, our
implementation of reading in data has to take into consideration of newline between each data in a
while loop.