C# exercises and solutions--sort records


C# structure: student records application

Sort records

Step11: Defining the sort(student[] st, int itemcount) function to sort the records in ascending order by total scores. For the sort technique we use bubble sort algorithm.

static void bubblesort(student[] dataset, int n)
{
int i, j;
for (i = 0; i < n; i++)
for (j = n - 1; j > i; j--)
if (dataset[j].total < dataset[j - 1].total )
{
  student temp = dataset[j];
  dataset[j] = dataset[j - 1];
  dataset[j - 1] = temp;
  }

}

If you want to sort the records in descending order, you need to modify the C# code above to:

static void bubblesort(student[] dataset, int n)
{
int i, j;
for (i = 0; i < n; i++)
for (j = n - 1; j > i; j--)
if (dataset[j].total > dataset[j - 1].total )
{
student temp = dataset[j];
dataset[j] = dataset[j - 1];
dataset[j - 1] = temp;
}

}




Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.