vanessa lemme

vanessa lemme

  • NA
  • 7
  • 807

basic bubble sort

Jun 10 2017 7:52 PM
I have an array of 10 elements which makes nine comparisons but i want it to make eight comparisons on the second pass and seven on the third pass etc. Can anyone help please?
 
 private static int[] arr = {17, 3, 9, 15, 7, 32, 21, 99, 10, 5};
{
static void Main(string[] args)
{
Console.WriteLine("Before Sorting: \n");
foreach (int i in arr)
{
Console.Write(i + " ");
}
Console.WriteLine();
BubbleSort();
Console.ReadKey();
}
public static void BubbleSort()
{
int temp;
Console.WriteLine("\nAfter Bubble Sorting: ");
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];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
Console.WriteLine();
foreach (int q in arr)
{
Console.Write(q + " ");
}
}
}
}
}
 
 

Answers (2)