Latest

6/recent/ticker-posts

COMPARISON BETWEEN DIFFERENT SORTING TECHNIQUES

 BUBBLE SORT:

To check the graphical comparison between the time complexity of different techniques click

 https://www.cuplex2b.com/2020/09/compare-and-plot-graph-of-cpu-time-for.html

 Bubble sort is a basic sorting technique that works with repeatedly swapping adjacent elements. This algorithm works as follows. Lets take an array of size 6

            a[6]={ 3,7,1,5,2,9}

Now, we will sort this array in increasing order using bubble sort. First, it compares each adjacent elements and swap if they are not in order. The process is repeated until the array get sorted. The pictorial representation is shown below:

we can see that in every step we get the relatively large number and the number of comparison decreases by one for every value of i=0 to i=n-1

We are performing n(n-1)/2 comparisons. So, the time complexity will be n^2 in general.

Code can be implemented as follows: 

CODE: 

#include <iostream>

using namespace std;


int main()

{

    int a[6]={3,7,1,5,2,9};

    int n=6;

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

    {

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

        {

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

            swap(a[j],a[j+1]);  //swap is a built in function used to swap two numbers

        }

    }

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

        cout<<a[i]<<" ";

    return 0;

}

OUTPUT: 

1 2 3 5 7 9



TIME COMPLEXITY AND PERFORMANCE:
1. Time complexity in worst case: O(n^2)
2. Time complexity in best case : O(n). (When the array is already sorted)
3. Space complexity: O(1).
4. This is an in place sorting technique and is stable.

MODIFIED BUBBLE SORT:

CODE:

#include <iostream>

using namespace std;

int main()
{
    int a[6]={3,7,1,5,2,9};
    int n=6;
    for(int i=0;i<n-1;i++)
    {   int flag=0;
        for(int j=0;j<n-i-1;j++)
        {
            if(a[j]>a[j+1])
            {
             swap(a[j],a[j+1]); //swap is a built in function used to swap two numbers
             flag=1;
            }
        }
        if(flag==0)
            break;
    }
    for(int i=0;i<n;i++)
        cout<<a[i]<<" ";
    return 0;
}

In this case if no elements are swapped in the inner loop, then the array is already sorted and no need to perform any further operation. This is more efficient than normal bubble sort.

SELECTION SORT:

In this sorting technique, the minimum element is selected from the unsorted part and is put at the beginning.

To understand the technique, let's take an array of size 6
a[6]={5,8,1,9,10,3}

Now, we will sort this array in increasing order using selection sort. The pictorial diagram is shown below for better understanding: 



We can see that in every step, we are picking minimum element of the unsorted part and putting it on the ith position. Here, swapping is taking place in every step. That's why selection sort is unstable.

CODE: 

#include <iostream>

using namespace std;

int main()
{
    int a[6]={5,8,1,9,10,3};
    int n=6;
     int i, j, mn;

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

        mn= i;
        for (j = i+1; j < n; j++)
        if (a[j] < a[mn])
            mn = j;


        swap(a[mn],a[i]);
    }
    for(int i=0;i<n;i++)
        cout<<a[i]<<" ";
    return 0;
}

OUTPUT: 1 3 5 8 9 10



TIME COMPLEXITY AND PERFORMANCE: 

1. Time complexity for all cases is O(n^2).

2.  Space complexity is O(1).

3. The technique is unstable.

INSERTION SORT:

This is one of the efficient sorting technique as compared to bubble sort and selection sort. In this technique, in every step, we compare an element with it's previous element. It is a greedy method of sorting.

 Insertion sort works the way many people sort a hand of playing cards. We start with an empty left hand and start with one card at a time. We pick up one card at a time and place it in the correct position in the left hand. To find out the correct position, we compare it with all the previously placed card on the left hand.

let's take an array 

a[6]={ 5,8,1,9,10,3}

 

 

5

8

1

9

10

3

 

i=1

j=0

5

8

1

9

10

3

key=8

i=2

j=1

5

1

8

9

10

3

key=1

i=2

j=0

1

5

8

9

10

3

key=1

 

 

1

5

8

9

10

3

 

i=3

j=2

1

5

8

9

10

3

key=9

 

 

1

5

8

9

10

3

 

i=4

j=3

1

5

8

9

10

3

key=10

 

 

1

5

8

9

10

3

 

i=5

j=4

1

5

8

9

3

10

key=3

i=5

j=3

1

5

8

3

9

10

key=3

i=5

j=2

1

5

3

8

9

10

key=3

i=5

j=1

1

3

5

8

9

10

key=3

 

 

1

3

5

8

9

10

 

 CODE:

#include <iostream>

using namespace std;

int main()

{

    int a[6]={5,8,1,9,10,3};

    int n=6;

     int i, key, j;

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

    {

        key = a[i];

        j = i - 1;

    while (j >= 0 && a[j] > key)

        {

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

            j = j - 1;

        }

        a[j + 1] = key;

    }

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

    {

        cout<<a[i]<<" ";

    }

}

OUTPUT: 1 3 5 8 9 10





TIME COMPLEXITY AND PERFORMANCE:

Time complexity depends on the number of inputs. Insertion sort is efficient in sorting smaller number of inputs 

1. The time complexity is O(n^2) in worst case.
2. Time complexity in best case is O(n).
3. Space complexity is O(1).
4. This technique is stable and in place sorting technique.

QUICK SORT:

This is a divide and conquer sorting technique. In divide the array in small small partitions and sort the small parts and at last combine them to get the sorted final array. 

First we take a pivot element. let us take the first element as pivot element and now we will traverse from left right to find out an element which is greater than the pivot element and will stop there. Again, we will traverse from right to left to find out an element lesser than or equal to the pivot element and will stop there. If we the traversal don't cross each other, then we will swap the two element that we found out. If we cross each other, then we will swap pivot element will the smaller element.

CODE: 

#include <bits/stdc++.h>

using namespace std;


int partition (int a[], int left, int right)

{

    int pivot = a[right];

    int mn = (left - 1);


    for (int i= left;i<right;i++)

    {

        if (a[i] < pivot)             // check if there is a element which is greater than pivot

        {

            mn++;

            swap(a[mn],a[i]);

        }

    }

    swap(a[mn+ 1],a[right]);         //since the loop gives the element greater than pivot

    return (mn + 1);

}



void quickSort(int a[], int left, int right)

{

    if (left< right)

    {


        int pp = partition(a, left, right);

        quickSort(a, left, pp- 1);

        quickSort(a, pp + 1,right);

    }

}


int main()

{

    int a[7] = {30,40,10,20,5,80,50};

    int n=7;

    quickSort(a,0,n-1);

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

        cout<<a[i]<<" ";

    return 0;

}

OUTPUT:    5    10    20    30    40    50    80


The code is explained step by step below with pictorial representation. Try to understand it taking the code as reference.



TIME COMPLEXITY AND PERFORMANCE:

1. Time complexity in best case is O(n log n).

2. Time complexity in worst case is O(n^2).

3. Time complexity in average case is O(n log n).

4. This sorting technique is not stable in general.

5. It is in place sorting technique as it doesn't take extra space to manipulate the code.






Post a Comment

0 Comments