C# exercise solutions-C# Arrays: sort


C# Arrays exercise: sort

Exercise 1:By using the bubble sort algorithm, write C# code to sort an integer array of 10 elements in ascending.

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[10] { 23, 2, 3, 34, 6,1,24,45,78,8}; //unsorted data set
bubblesort(arr, 10); //sorting process using bubble sort
int i;
for (i = 0; i < 10; i++)
Console.Write(arr[i] + "\t"); //after sorting in ascending order

Console.ReadLine();

}
///bubble sort

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

    }
   }
}

Exercise 2: Modify the C# code in exercise 1 in order to sort the array in descending order.

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[10] { 23, 2, 3, 34, 6,1,24,45,78,8}; //unsorted data set
bubblesort(arr, 10); //sorting process using bubble sort
int i;
for (i = 0; i < 10; i++)
Console.Write(arr[i] + "\t"); //after sorting in descending order

Console.ReadLine();

}
///bubble sort

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

    }
  }
}


Comments

Jamisco comment

 Jamisco

6 months later I was able to do this in less than 5 minutes

int[] ten = new int[] { 5, 3, 1, 6, 8, 10, 4, 9, 2, 7 };

int i;
for ( i = 0; i <= ten.Length - 1; i++)
{
for (int j = i + 1; j <= ten.Length - 1; j++)
{
int a = ten[i]; // val 10
int b = ten[j]; // val 4

if ( a < b)
{
ten[i] = b; // index 0 = 1
ten[j] = a; // index 1 = 5
}
}
Console.Write(ten[i] + " ");
}
Console.ReadLine();


2017-06-20
shrill(This program has both a comment

 shrill(This program has both a

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace bubblesort
{
public class Program
{
public void Main(string[] args)
{
int[] list = new int[] { 2, 13, 4, 34, 54, 45, 76, 8, 0 };
string choice = "y";
string option;
while (choice == "y")
{
Console.WriteLine("Do you want 1. ascending or 2. descending order(1/2) ?");
option = Console.ReadLine().ToString();
switch (option)
{
case "1":
bubblesortascending(list);
break;
case"2":
bubblesortdesending(list);
break;
default: Console.WriteLine("Wrong option");
break;

}
Console.WriteLine("Do you want to try again?(y/n)");
choice = Console.ReadLine().ToString();
if (choice == "y")
continue;

else
break;

}
}
public void bubblesortascending(int[] list)
{
for (int i = 0; i < list.Length; i++)
{
for (int j = 0; j < list.Length - 1; j++)
{
if (list[j] > list[j + 1])
{
int temp = list[j];
list[j ] = list[j+1];
list[j+1] = temp;
}

}

}
Console.WriteLine("Bubble Sort Ascending Order:");
for (int i = 0; i < list.Length; i++)
{
Console.Write(list[i]+" ");

}
}
public void bubblesortdesending(int[] list)
{
for (int i = 0; i < list.Length; i++)
{
for (int j = 0; j < list.Length - 1; j++)
{
if (list[j] < list[j + 1])
{
int temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}

}

}
Console.WriteLine("Bubble sort Descending Order: ");
for (int i = 0; i < list.Length; i++)
{
Console.Write(list[i] + " ");

}
}

}
}


2017-01-29
Jhong comment

 Jhong

Hey Ahmed,

In his example, bubblesort is a method, you should make a method first before using the word. Just like what he did:

static void bubblesort(int[] dataset, int n)


But, I have my own version wherein process are in one method:

int[] arr = { 10, 2, 4, 9, 1, 6, 3, 5, 7, 8 };
int temp = 0;

for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}

foreach (var item in arr)
{
Console.WriteLine(item);
}

Console.ReadLine();


2017-01-27
ahmed comment

 ahmed

bubblesort(arr, 10);

Error 1 The name 'bubblesort' does not exist in the current context c:\users\hp\documents\visual studio 2013\Projects\bubble sort algorithm\bubble sort algorithm\Program.cs 14 13 bubble sort algorithm


plz help


2017-01-17
jamisco comment

 jamisco

Array.sort people.
Its a fcking life saver
instead of the whole loops. Like for a complete noob like me the first question was just impossible to think out


2017-01-02
SAD comment

 SAD

For ascending order it iterates for 15 loops and for descending order 30 loops. Is it possible equal number of loops for both?


2014-11-04
Dara comment

 Dara

It is the number of array elements.


2014-09-11
Phu comment

 Phu

What is input n in the subroutine?


2014-09-06



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.