C program for Time Complexity plot of Bubble, Insertion and Selection Sort using Gnuplot
Last Updated :
24 Nov, 2021
Prerequisite:Comparison among bubble sort, insertion sort and selection sort.
Write a C program to plot and analyze the time complexity of Bubble sort, Insertion sort and Selection sort (using Gnuplot).
As per the problem we have to plot a time complexity graph by just using C. So we will be making sorting algorithms as functions and all the algorithms are given to sort exactly the same array to keep the comparison fair.
Examples:
Input: randomly generated arrays (using rand() function)
of different sizes as input for sorting.
Output:
A_size, Bubble, Insertion, Selection
10000, 366263, 80736, 152975
20000, 1594932, 332101, 609388
30000, 3773121, 785790, 1441547
40000, 7174455, 1574855, 2620006
50000, 10917061, 2029586, 4025993
60000, 15484338, 2998403, 5556494
70000, 21201561, 4146680, 7678139
80000, 29506758, 6027335, 10131950
90000, 36457272, 6699452, 12436376
100000, 43472313, 8335881, 15208712
Approach: We will be using arrays of different sizes to plot the graph between the time taken by the sorting algorithm versus array size. Execution of the program will take some time for sorting arrays of size up to 100000 elements.
Below is the implementation of the above approach:
[sourcecode highlight="language=CPP"]
// C program to store time taken by bubble sort,
// insertion sort and selection sort
// for sorting exactly same arrays.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Swap utility
void swap(long int* a, long int* b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
// Bubble sort
void bubbleSort(long int a[], long int n)
{
for (long int i = 0; i < n - 1; i++) {
for (long int j = 0; j < n - 1 - i; j++) {
if (a[j] > a[j + 1]) {
swap(&a[j], &a[j + 1]);
}
}
}
}
// Insertion sort
void insertionSort(long int arr[], long int n)
{
long int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
// Move elements of arr[0..i-1], that are
// greater than key, to one position ahead
// of their current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
// Selection sort
void selectionSort(long int arr[], long int n)
{
long int i, j, midx;
for (i = 0; i < n - 1; i++) {
// Find the minimum element in unsorted array
midx = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
midx = j;
// Swap the found minimum element
// with the first element
swap(&arr[midx], &arr[i]);
}
}
// Driver code
int main()
{
long int n = 10000;
int it = 0;
// Arrays to store time duration
// of sorting algorithms
double tim1[10], tim2[10], tim3[10];
printf("A_size, Bubble, Insertion, Selection\n");
// Performs 10 iterations
while (it++ < 10) {
long int a[n], b[n], c[n];
// generating n random numbers
// storing them in arrays a, b, c
for (int i = 0; i < n; i++) {
long int no = rand() % n + 1;
a[i] = no;
b[i] = no;
c[i] = no;
}
// using clock_t to store time
clock_t start, end;
// Bubble sort
start = clock();
bubbleSort(a, n);
end = clock();
tim1[it] = ((double)(end - start));
// Insertion sort
start = clock();
insertionSort(b, n);
end = clock();
tim2[it] = ((double)(end - start));
// Selection sort
start = clock();
selectionSort(c, n);
end = clock();
tim3[it] = ((double)(end - start));
// type conversion to long int
// for plotting graph with integer values
printf("%li, %li, %li, %li\n",
n,
(long int)tim1[it],
(long int)tim2[it],
(long int)tim3[it]);
// increases the size of array by 10000
n += 10000;
}
return 0;
}
[/sourcecode]
Output:
A_size, Bubble, Insertion, Selection
10000, 366263, 80736, 152975
20000, 1594932, 332101, 609388
30000, 3773121, 785790, 1441547
40000, 7174455, 1574855, 2620006
50000, 10917061, 2029586, 4025993
60000, 15484338, 2998403, 5556494
70000, 21201561, 4146680, 7678139
80000, 29506758, 6027335, 10131950
90000, 36457272, 6699452, 12436376
100000, 43472313, 8335881, 15208712
Now comes the question that how are we going to plot a graph on x-y coordinates?
For that, we will be using a very simple utility called
Gnuplot.
Gnuplot is a portable command-line driven graphing utility for Linux, MS Windows, OSX and many other platforms.
Note:In this article, Ubuntu(Linux) is used.
- First things first how to install Gnuplot.Use this command to install Gnuplot.
sudo apt-get install gnuplot
- After this compile your code and copy the output in a text file using this command.
./a.out>plot.txt
- Open Gnuplot simply using.
gnuplot
- Now the last thing is to plot the graph so use this command to plot the complexity graph.
plot './plot.txt' using 1:2 with linespoints,
'./plot.txt' using 1:3 with linespoints,
'./plot.txt' using 1:4 with linespoints
Here is a terminal-snap of commands.
Here is the graph of complexity comparing bubble sort(purple curve), insertion sort(green curve) and selection sort(blue curve).
Observation:
The average time complexity of all three algorithms is
O(n^2) but as the size of input data increases, insertion sort performs far better than bubble sort and slightly better than selection sort.
Similar Reads
C++ Program for Recursive Insertion Sort
Insertion sort is a simple and efficient sorting algorithm that is often used for small lists or as a building block for more complex algorithms. It works by dividing the input list into two parts: a sorted sublist of items that has already been traversed, and an unsorted sublist of items remaining
3 min read
Sorting integer data from file and calculate execution time
Prerequisite: Selection Sort In this article, we are going to apply selection sort algorithm, in which the source of input is A FILE CONTAINING 10000 INTEGERS and output will be the total time taken to sort. Important functions to be used: rand(): Used to generate random numbers. fopen(): Used to op
3 min read
C Program For Insertion Sort
Insertion sort is a simple sorting algorithm used to sort a collection of elements in a given order. It is less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort but it is simple to implement and is suitable to sort small data lists. In this article, w
4 min read
C Program for Binary Insertion Sort
We can use binary search to reduce the number of comparisons in normal insertion sort. Binary Insertion Sort find use binary search to find the proper location to insert the selected item at each iteration. In normal insertion, sort it takes O(i) (at ith iteration) in worst case. we can reduce it to
5 min read
C Program for Selection Sort
The selection sort is a simple comparison-based sorting algorithm that sorts a collection by repeatedly finding the minimum (or maximum) element and placing it in its correct position in the list. It is very simple to implement and is preferred when you have to manually implement the sorting algorit
4 min read
C Exercises - Practice Questions with Solutions for C Programming
The best way to learn C programming language is by hands-on practice. This C Exercise page contains the top 30 C exercise questions with solutions that are designed for both beginners and advanced programmers. It covers all major concepts like arrays, pointers, for-loop, and many more. So, Keep it U
12 min read
C Program to Sort a String of Characters
Sorting a string of characters refers to the process of rearranging all the characters in the given order. In this article, we will learn how to sort a string of characters using the C program. The most straightforward method to sort a string of characters is by using the qsort() function. Let's tak
3 min read
Program to creating Doraemon cartoon character using Computer Graphics
Computer Graphics is an important subject to improve coding skills. Many things can be implemented using computer graphics. For example- car animation, cartoon characters, and many more things. In this article, the cartoon character Doraemon is created using computer graphics. Implementation in C In
4 min read
Creating Butterfly themed Fractal in C++ Using Graphics
In Layman's terms, Fractals are beautiful patterns brought into existence upon the intertwining of computation and mathematics. To get a bit technical they are recursive in nature such that when looked upon a certain subset of a given fractal a similar pattern appears to emerge out of it. In this ar
3 min read
C/C++ Program for Finding the vertex, focus and directrix of a parabola
A parabola is a U-shaped curve that can be either concave up or down, depending on the equation. Different properties of the parabola such as vertex, focus, the axis of symmetry, latus rectum, directrix, etc. are used to solve many problems throughout all the fields. Vertex of a ParabolaIn the conte
4 min read