C program for Time Complexity plot of Bubble, Insertion and Selection Sort using Gnuplot
Last Updated :
12 Jul, 2025
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
B-Spline Curve in Computer Graphics Prerequisite - Bezier CurveConcept of B-spline curve came to resolve the disadvantages having by Bezier curve, as we all know that both curves are parametric in nature. In Bezier curve we face a problem, when we change any of the control point respective location the whole curve shape gets change. B
3 min read
Data Structures and Algorithms | Set 6 Following questions have been asked in GATE CS exam. 1. The usual Θ(n^2) implementation of Insertion Sort to sort an array uses linear search to identify the position where an element is to be inserted into the already sorted part of the array. If, instead, we use binary search to identify the
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, we
4 min read
C Program For Bubble Sort Bubble Sort is a comparison based simple sorting algorithm that works by comparing the adjacent elements and swapping them if the elements are not in the correct order. It is an in-place and stable sorting algorithm that can sort items in data structures such as arrays and linked lists.Implementatio
4 min read
Sorting Algorithms Visualization : Bubble Sort The human brain can easily process visuals in spite of long codes to understand the algorithms. In this article, Bubble sort visualization has been implemented using graphics.h library. As we all know that bubble sort swaps the adjacent elements if they are unsorted and finally the larger one being
5 min read
Visualizing Bubble sort using Python Prerequisites: Introduction to Matplotlib, Introduction to PyQt5, Bubble Sort Learning any algorithm can be difficult, and since you are here at GeekforGeeks, you definitely love to understand and implement various algorithms. It is tough for every one of us to understand algorithms at the first go.
2 min read