public static string BubbleSort(){int[] arr = { 78, 55, 45, 98, 13 };int temp;for (int j = 0; j <= arr.Length - 2; j++){for (int i = 0; i <= arr.Length - 2; i++){if (arr[i] > arr[i + 1]){temp = arr[i + 1];arr[i + 1] = arr[i];arr[i] = temp;}}}Console.WriteLine("Sorted:");foreach (int p in arr)Console.Write(p + " ");Console.Read();}
Interviews2 answers
Sort the given array using the bubble sort? int[] a = { 8, 3, 11, 34, 43, 1, 67, 35, 98 };
2 Answers
Dinuka HettiarachchiPosted Nov 18, 2021, 7:11 AM EST
public int[] min(int[] a)
{
int temp, swaps;
for (int i = 0; i < a.Length; i++)
{
swaps = 0;
for (int j=0;j a[j + 1])
{
temp = a[j];
a[j] = a[j+1];
a[j + 1] = temp;
swaps++;
}
}
if (swaps == 0)
{
break;
}
}
return a;
}
Ritesh SinghPosted Aug 13, 2016, 2:51 AM EDT
