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.
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:
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.
- Open Gnuplot simply using.
- 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
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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Python Code for time Complexity plot of Heap Sort
- An Insertion Sort time complexity question
- Time complexity of insertion sort when there are O(n) inversions?
- Program to sort an array of strings using Selection Sort
- C++ Program for Bubble Sort
- C Program for Bubble Sort
- Python Program for Bubble Sort
- C++ Program for Recursive Bubble Sort
- Java Program for Bubble Sort
- Java Program for Recursive Bubble Sort
- C Program for Bubble Sort on Linked List
- C Program for Selection Sort
- 8086 program for selection sort
- C++ program for Sorting Dates using Selection Sort
- Time complexity of recursive Fibonacci program
- C Program for Insertion Sort
- C Program for Binary Insertion Sort
- C Program for Recursive Insertion Sort
- Java Program for Recursive Insertion Sort
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.