BubbleSort-converted
BubbleSort-converted
1)
int n = Convert.ToInt32(Console.ReadLine());
mynum[p] = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(p);
int x = mynum[j];
mynum[j] = mynum[i];
mynum[i] = x;
Console.WriteLine(p);
Console.ReadLine();
2)
Bubble sort starts with very first two elements, comparing them to check
which one is greater.
We find that 27 is smaller than 33 and these two values must be swapped.
We swap these values. We find that we reach at the end of the array. After
one iteration the array should look like this −
To be precise, we are now showing that how array should look like after
each iteration. After second iteration, it should look like this −
Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sorts learns that array is
completely sorted.
Algorithm
We assume list is an array of n elements. We further assume
that swapfunction, swaps the values of given array elements.
begin BubbleSort(list)
swap(list[i], list[i+1])
end if
end for
return list
end BubbleSort
Pseudocode
We observe in algorithm that Bubble Sort compares each pair of array
element unless the whole array is completely sorted ascending. This may
cause few complexity issues like what if the array needs no more swapping
as all the elements are already ascending.
To ease-out the issue, we use one flag variable swapped which will help us
to see if any swap is happened or not. If no swap is occurred, i.e. the array
requires no more processing to be sorted, it will come out of the loop.
loop = list.count;
swapped = false
/* swap them */
swap( list[j], list[j+1] )
swapped = true
end if
end for
break
end if
end for
3)
Bubble Sorting
Bubble Sort is an algorithm which is used to sort N elements that are given in a memory
for eg: an Array withN number of elements. Bubble Sort compares all the element one by
one and sort them based on their values.
It is called Bubble sort, because with each iteration the smaller element in the list bubbles
up towards the first place, just like a water bubble rises up to the water surface.
Sorting takes place by stepping through all the data items one-by-one in pairs and
comparing adjacent data items and swapping each pair that is out of order.
Sorting using Bubble Sort Algorithm
Let's consider an array with values {5, 1, 6, 2, 4, 3}
int a[6] = {5, 1, 6, 2, 4, 3};
int i, j, temp;
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
Above is the algorithm, to sort an array using Bubble Sort. Although the above logic will
sort and unsorted array, still the above algorithm isn't efficient and can be enhanced
further. Because as per the above logic, the for loop will keep going for six iterations even
if the array gets sorted after the second iteration.
Hence we can insert a flag and can keep checking whether swapping of elements is
taking place or not. If no swapping is taking place that means the array is sorted and wew
can jump out of the for loop.
int a[6] = {5, 1, 6, 2, 4, 3};
int i, j, temp;
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
break;
In the above code, if in a complete single cycle of j iteration(inner for loop), no swapping
takes place, and flag remains 0, then we will break out of the for loops, because the array
has already been sorted.
Sum = n(n-1)/2
i.e O(n2)
Hence the complexity of Bubble Sort is O(n2).
The main advantage of Bubble Sort is the simplicity of the algorithm.Space complexity for
Bubble Sort is O(1), because only single additional memory space is required
for temp variable
Best-case Time Complexity will be O(n), it is when the list is already sorted.
4)
using System;
namespace BubbleSortInCSharp
{
class bubblesort
{
static void Main(string[] args)
{
int[] a = { 3, 2, 5, 4, 1 }; // ing numbers through array
int t;
for (int p = 0; p <= a.Length - 2; p++)
{
for (int i = 0; i <= a.Length - 2; i++)
{
if (a[i] > a[i + 1])
{
t = a[i + 1];
a[i + 1] = a[i];
a[i] = t;
}
}
}
Console.WriteLine("This Application Created by vithal wadje for C# corner");
Console.WriteLine("The Sorted array");
foreach (int aa in a) //writting array
Console.Write(aa + " ");
Console.Read();
}
}
}