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 shifted towards to the end of array in each pass. Sometimes, it becomes difficult to analyze the data manually, but after plotting graphically it is much easier to understand as shown in the figure below.
Approach:
Even comparing two types of data-set is also difficult with numbers if the size of the array is large.
The graphical representation of randomly distributed numbers and reverse sorted numbers is shown below.
The white line is used to represent the length of number (9 being represented by 9 pixels vertically upwards) while its position represents its index in the array.
Graphically sorting can be shown simply by swapping the lines.
As we swap the numbers in bubble sort, a different colour line can be used to see the current index in the array (here green colour).
Here delay() can be increased to see the transition in the graph.
Examples:
Random arrayReverse sorted array
Pre-defined Functions Used:
setcurrentwindow():A function which is used to set the size of current window.
setcolor(n): A function which is used to change the color of cursor by changing the value of n.
delay(n): A function which is used to delay the program by n milliseconds. It is being used for slowing down the transitions speed
line(x1, y1, x2, y2): A function which is used to draw an line from point (x1, y1) to point (x2, y2). (0, 0) being the left top corner of the screen and bottom right be (n1, n2) where n1, n2 are the width and height of the current window. There are other graphics which can be applied to this line using setcolor().
// C++ program for visualization of bubble sort#include"graphics.h"#include<bits/stdc++.h>usingnamespacestd;// Initialize the size// with the total numbers to sorted// and the gap to be maintained in graphvector<int>numbers;intsize=200;intgap=4;// Function for swapping the lines graphicallyvoidswap(inti,intj,intx,inty){// Swapping the first line with the correct line// by making it black again and then draw the pixel// for white color.setcolor(GREEN);line(i,size,i,size-x);setcolor(BLACK);line(i,size,i,size-x);setcolor(WHITE);line(i,size,i,size-y);// Swapping the first line with the correct line// by making it black again and then draw the pixel// for white color.setcolor(GREEN);line(j,size,j,size-y);setcolor(BLACK);line(j,size,j,size-y);setcolor(WHITE);line(j,size,j,size-x);}// Bubble sort functionvoidbubbleSort(){inttemp,i,j;for(i=1;i<size;i++){for(j=0;j<size-i;j++){if(numbers[j]>numbers[j+1]){temp=numbers[j];numbers[j]=numbers[j+1];numbers[j+1]=temp;// As we swapped the last two numbers// just swap the lines with the values.// This is function call// for swapping the linesswap(gap*j+1,gap*(j+1)+1,numbers[j+1],numbers[j]);}}}}// Driver programintmain(){// auto detection of screen sizeintgd=DETECT,gm;intwid1;// Graph initializationinitgraph(&gd,&gm,NULL);// setting up window size (gap*size) * (size)wid1=initwindow(gap*size+1,size+1);setcurrentwindow(wid1);// Initializing the arrayfor(inti=1;i<=size;i++)numbers.push_back(i);// Find a seed and shuffle the array// to make it random.// Here different type of array// can be taken to results// such as nearly sorted, already sorted,// reverse sorted to visualize the resultunsignedseed=chrono::system_clock::now().time_since_epoch().count();shuffle(numbers.begin(),numbers.end(),default_random_engine(seed));// Initial plot of numbers in graph taking// the vector position as x-axis and its// corresponding value will be the height of line.for(inti=1;i<=gap*size;i+=gap){line(i,size,i,(size-numbers[i/gap]));}// Delay the codedelay(200);// Call sortbubbleSort();for(inti=0;i<size;i++){cout<<numbers[i]<<" ";}cout<<endl;// Wait for sometime .delay(5000);// Close the graphclosegraph();return0;}
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.