Bubble - Merg - Sort

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Bubble Sort

In this sorting technique, elements are sorted in ascending order by repeatedly


swapping the larger element with the smaller element. In case of descending
order, sorting is performed by repeatedly swapping the smaller element with
the larger element.

To clearly understand the bubble sorting technique, let's go through its


algorithm step by step with an example.

Bubble Sort Illustration

Suppose we have seven numbers stored in an array as


shown below.

Take the 1st number 12 and compare it with the 2nd number
9. 12 is greater than 9 so swap both the numbers.

Now take the 2nd number 12 and compare it with the 3rd
number 37. 12 is not greater than 37 so leave it.
Now take the 3rd number 37 and compare it with the 4th
number 86. 37 is not greater than 86 so leave it.

Now take the 4th number 86 and compare it with the 5th
number 2. 86 is greater than 2 so swap both the numbers.

Now take the 5th number 86 and compare it with the 6th
number 17. 86 is greater than 17 so swap both the numbers.

Now take the 6th number 86 and compare it with the 7th
number 5. 86 is greater than 5 so swap both the numbers.

So we can see that the largest number has shifted towards


the end of the array. To arrange the other numbers also, we
have to repeat the above process five times more from index
0 to index 4.

If there are ten number in an array, we have to repeat the


above process nine times to sort the entire array.

Algorithm for Bubble Sort

 Define an array to store N numbers for bubble sort.


Suppose we have defined an array with the name num.
 Run an outer loop i from 0 to N-1 to repeat the
process of bubble sort.
 Run an inner loop j inside the body of the outer
loop i for bubble sort from 0 to N-1-i.
 Inside the body of the inner loop j, check if the value
at num[j] is greater than the value at num[j+1].
 If the value at num[j] is greater than value
at num[j+1] then swap the numbers.
 Now print the sorted array on the screen outside the
body of the outer loop i.

Implementation of Bubble sort

#include<stdio.h>
void print(int a[], int n) //function to print array elements
{
int i;
for(i = 0; i < n; i++)
{
printf("%d ",a[i]);
}
}
void bubble(int a[], int n) // function to implement bubble sort
{
int i, j, temp;
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
void main ()
{
int i, j,temp;
int a[5] = { 9, 7, 8, 3, 6};
int n = sizeof(a)/sizeof(a[0]);
printf("Before sorting array elements are - \n");
print(a, n);
bubble(a, n);
printf("\nAfter sorting array elements are - \n");
print(a, n);
}
Merge sort
Merge sort is similar to the quick sort algorithm as it uses the divide and
conquer approach to sort the elements. It is one of the most popular and
efficient sorting algorithm. It divides the given list into two equal halves, calls
itself for the two halves and then merges the two sorted halves.

The merge function works as follows:

1. Create copies of the subarrays L <- A[p..q] and R <- A[q+1..r].

2. Create three pointers i, j and k

a. i maintains current index of L, starting at 0

b. j maintains current index of R, starting at 0

c. k maintains the current index of A[p..q], starting at p.

3. Until we reach the end of either L or R, pick the larger among the elements
from L and R and place them in the correct position at A[p..q]
4. When we run out of elements in either L or R, pick up the remaining elements
and put in A[p..q]

Merge Sort Visualization:

Merge_sort(0,5)

9 7 8 3 2 1
mid=(0+5)/2=2

Merge_sort(0,2) Merge_sort(3,5)

9 7 8 3 2 1

mid=(0+2)/2=1 mid=(3+5)/2=4

Merge_sort(0,1) Merge_sort(2,2) Merge_sort(3,4) Merge_sort(5,5)

9 7 8 3 2 1
mid=(0+1)/2=0 No further call mid=(3+4)/2=3 No further call

Merge_sort(0,0) Merge_sort(1,1) Merge_sort(3,3) Merge_sort(4,4)

9 7 3
2

No further call No further call No further call

Merge(0,0,1)

7 9 2 3
Merge(3,3,4)

1 2 3
9 7 8
Merge(3,5)
Merge(0,2)
Merge_sort(0,5)

1 2 3 7 8 9
//by MK Patel Sir
// Merge sort Implementation Using C

#include <stdio.h>

// Merge two subarrays L and R into arr

void merge(int arr[], int p, int q, int r) {

// Create L ← A[p..q] and R ← A[q+1..r]

int n1 = q - p + 1;

int n2 = r - q;

int L[n1], R[n2];

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

L[i] = arr[p + i];

for (int j = 0; j < n2; j++)

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

// Maintain current index of sub-arrays and main array

int i, j, k;

i = 0;

j = 0;

k = p;

// Until we reach either end of either L or R, pick larger among

// elements L and R and place them in the correct position at A[p..r]

while (i < n1 && j < n2) {

if (L[i] <= R[j]) {

arr[k] = L[i];

i++;
} else {

arr[k] = R[j];

j++;

k++;

// When we run out of elements in either L or R,

// pick up the remaining elements and put in A[p..r]

while (i < n1) {

arr[k] = L[i];

i++;

k++;

while (j < n2) {

arr[k] = R[j];

j++;

k++;

// Divide the array into two subarrays, sort them and merge them

void mergeSort(int arr[], int l, int r) {

if (l < r) {
// m is the point where the array is divided into two subarrays

int m = l + (r - l) / 2;

mergeSort(arr, l, m);

mergeSort(arr, m + 1, r);

// Merge the sorted subarrays

merge(arr, l, m, r);

// Print the array

void printArray(int arr[], int size) {

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

printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[] = {9, 7, 8, 3, 2, 1};

int size = sizeof(arr) / sizeof(arr[0]);

mergeSort(arr, 0, size - 1);

printf("Sorted array: \n");

printArray(arr, size);

You might also like