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;
}
}
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;
}
}
No comments:
Post a Comment