Q: Write a bubble sort method, then apply it to sort the array and display it.
static void Main(string[] args)
{
int[] A = { 3, -12, 6, 9, -7, -13, 19, 35, -8, -11, 15, 27, -1 };
SortArray(A);
DisplayArray(A);
Console.ReadLine();
} //=================================================================
//bubble sort method
static int[] SortArray(int[] array)
{
int temp = array[0];
for (int i = 0; i < array.Length; i++)
{
for (int j = i+1; j < array.Length; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
return array;
}
//method to display array
static void DisplayArray(int[]A)
{
Console.WriteLine("\n============Sorted Buble Array================");
for (int i = 0; i < A.Length; i++)
{
Console.Write("{0}, ", A[i]);
}
Console.WriteLine("\n============Sorted Buble Array================");
}
}
}
No comments:
Post a Comment