Bubble sort algoritmasına tek döngülü bir algoritma
Bubble sort algoritmasında tüm diziyi tekrar tekrar dolaşmanın önüne geçerek, sıralamada performans artışı sağlayacak bir kod.
int[] arr = { 16, 12, 24, 21, 13, 9, 17, 8, 2, 14, 30, 26, 4, 19, 10, 15, 3, 27, 1, 22, 11, 6, 5, 18, 28, 7, 29, 20, 25, 23 };
int n = arr.Length;
int temp = 0;
for (int i = 0; i < n - 1; i++)
{
if (arr[i] > arr[i + 1])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
i-=2;
i = ((i < -1) ? -1 : i);
}
}
var arr = [16, 12, 24, 21, 13, 9, 17, 8, 2, 14, 30, 26, 4, 19, 10, 15, 3, 27, 1, 22, 11, 6, 5, 18, 28, 7, 29, 20, 25, 23];
var n = arr.length;
var temp = 0;
for (var i = 0; i < n - 1; i++)
{
if (arr[i] > arr[i + 1])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
i-=2;
}
}
arr = [16, 12, 24, 21, 13, 9, 17, 8, 2, 14, 30, 26, 4, 19, 10, 15, 3, 27, 1, 22, 11, 6, 5, 18, 28, 7, 29, 20, 25, 23]
n = len(arr)
i = 0
while i < n - 1:
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i+1], arr[i]
i -= 2
i = -1 if i < -1 else i
i += 1