0% found this document useful (0 votes)
11 views8 pages

BubbleSort-converted

The document provides an overview of the Bubble Sort algorithm, including its implementation in C# and a detailed explanation of how it works. It describes the process of comparing and swapping adjacent elements to sort an array, along with a complexity analysis indicating that its time complexity is O(n^2) and space complexity is O(1). Additionally, it introduces an optimization using a flag to improve efficiency by breaking the loop when no swaps occur.

Uploaded by

Harindra Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

BubbleSort-converted

The document provides an overview of the Bubble Sort algorithm, including its implementation in C# and a detailed explanation of how it works. It describes the process of comparing and swapping adjacent elements to sort an array, along with a complexity analysis indicating that its time complexity is O(n^2) and space complexity is O(1). Additionally, it introduces an optimization using a flag to improve efficiency by breaking the loop when no swaps occur.

Uploaded by

Harindra Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Bubble Sort

1)

Console.WriteLine("Enter the size");

int n = Convert.ToInt32(Console.ReadLine());

int[] mynum = new int[n];

Console.WriteLine("Enter the Numbers");

for (int p = 0; p < n; p++)

mynum[p] = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("The number are");

foreach (int p in mynum)

Console.WriteLine(p);

for (int i = 0; i < n; i++)

for (int j = i + 1; j < n; j++)

if (mynum[i] > mynum[j])

int x = mynum[j];

mynum[j] = mynum[i];

mynum[i] = x;

Console.WriteLine("Sortrd data is-");

foreach (int p in mynum)


{

Console.WriteLine(p);

Console.ReadLine();

2)

How bubble sort works?


We take an unsorted array for our example. Bubble sort take Ο(n2) time so
we're keeping short and precise.

Bubble sort starts with very first two elements, comparing them to check
which one is greater.

In this case, value 33 is greater than 14, so it is already in sorted locations.


Next, we compare 33 with 27.

We find that 27 is smaller than 33 and these two values must be swapped.

The new array should look like this −


Next we compare 33 and 35. We find that both are in already sorted
positions.

Then we move to next two values, 35 and 10.

We know than 10 is smaller 35. Hence they are not sorted.

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.

Now we should look into some practical aspects of bubble sort.

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)

for all elements of list

if list[i] > list[i+1]

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.

Pseudocode of BubbleSort algorithm can be written as given below −

procedure bubbleSort( list : array of items )

loop = list.count;

for i = 0 to loop-1 do:

swapped = false

for j = 0 to loop-1 do:

/* compare the adjacent elements */

if list[j] > list[j+1] then

/* swap them */
swap( list[j], list[j+1] )

swapped = true

end if

end for

/*if no number was swapped that means

array is sorted now, break the loop.*/

if(not swapped) then

break

end if

end for

end procedure return list

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;

for(i=0; i<6; i++)

for(j=0; j<6-i-1; j++)

if( a[j] > a[j+1])

temp = a[j];

a[j] = a[j+1];

a[j+1] = temp;

//now you can print the sorted array after this

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;

for(i=0; i<6; i++)

int flag = 0; //taking a flag variable

for(j=0; j<6-i-1; j++)

if( a[j] > a[j+1])

temp = a[j];

a[j] = a[j+1];

a[j+1] = temp;

flag = 1; //setting flag as 1, if swapping occurs

if(!flag) //breaking out of for loop if no swapping takes place

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.

Complexity Analysis of Bubble Sorting


In Bubble Sort, n-1 comparisons will be done in 1st pass, n-2 in 2nd pass, n-3 in 3rd pass
and so on. So the total number of comparisons will be
(n-1)+(n-2)+(n-3)+.....+3+2+1

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();
}
}
}

You might also like