Sorting
is nothing but arranging the data in ascending or descending order. The term

**sorting**came into picture, as humans realised the importance of searching quickly.**sorting**allows everyone to arrange data in an order, hence making it easier to search.**Bubble Sort Algorithm**

Bubble Sort is a simple
algorithm which is used to sort a given set of n elements provided in
form of an array with n number of elements. Bubble Sort compares all
the element one by one and sort them based on their values.

If the given array has to be
sorted in ascending order, then bubble sort will start by comparing the first
element of the array with the second element, if the first element is greater
than the second element, it will swap both the elements, and then
move on to compare the second and the third element, and so on.

If we have total n elements,
then we need to repeat this process for n-1 times. But it is noted
that when the array is sorted , comparing adjacent element does not produce any
swap and we can stop the process without repeating the next step.

Bubble sort, sometimes referred
to as

**sinking sort**, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller elements "bubble" to the top of the list.
void bubblesort(int
arr[], int n)

{

int i, j, temp;

for(i = 0; i < n; i++) // steps

{

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

{

if( arr[j] > arr[j+1])
//comparing adjacent element

{

// swap the adjacent elements
if they are out of order

temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

}

}

}

Hence, in
the inner for loop, we check whether swapping of elements is taking
place or not, every time.

If for a
particular iteration, no swapping took place, it means the array has been
sorted and we can jump out of the for loop, instead of executing all
the iterations.

void bubbleSort(int
arr[], int n)

{

int i, j, temp,flag;

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

{

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

{

// introducing a flag to monitor
swapping

flag = 0;

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

{

// swap the elements

temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

// if swapping happens update
flag to 1

flag = 1;

}

}

// if value of flag is zero after all
the iterations of inner loop

//
then break out

if(!flag)

{

break;

}

}

**Selection Sort**

The selection sort algorithm
sorts an array by repeatedly finding the minimum element (considering ascending
order) from unsorted part and putting it at the beginning. The algorithm
maintains two subarrays in a given array.

1) The sub array which is already sorted.

2) Remaining sub array which is unsorted.

2) Remaining sub array which is unsorted.

In every iteration of selection sort, the minimum element
(considering ascending order) from the unsorted sub array is picked and moved
to the sorted sub array.

void selectionsort(int arr[],
int n)

{

int
i, j, minidx,temp;

//
One by one move boundary of unsorted subarray

for
(i = 0; i < n-1; i++)

{

//
Find the minimum element in unsorted array

minidx
= i;

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

if
(arr[j] < arr[minidx])

minidx
= j;

//
Swap the found minimum element with the first element

if(minidx!=i)

{

temp=arr[minidx];

arr[minidx]=arr[i];

arr[i]=temp;

}

}

}

very nice..post.

ReplyDeletehttps://shayarihindishayari.com/

your blog is nice

ReplyDeleteweb programming tutorial

welookups.com