0% found this document useful (0 votes)
129 views27 pages

Computer Graphics Mini Project On: "Insertion Sort"

This document describes a computer graphics mini project on insertion sort submitted by Abhishek N J to the Visvesvaraya Technological University. The project was completed under the guidance of Prof. B.S Umashankar at Don Bosco Institute of Technology to fulfill the requirements for a Bachelor of Engineering degree in Computer Science and Engineering. The project uses OpenGL to visually demonstrate how insertion sort works by sorting a bar graph and allowing the user to see the sorting process.

Uploaded by

gagana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views27 pages

Computer Graphics Mini Project On: "Insertion Sort"

This document describes a computer graphics mini project on insertion sort submitted by Abhishek N J to the Visvesvaraya Technological University. The project was completed under the guidance of Prof. B.S Umashankar at Don Bosco Institute of Technology to fulfill the requirements for a Bachelor of Engineering degree in Computer Science and Engineering. The project uses OpenGL to visually demonstrate how insertion sort works by sorting a bar graph and allowing the user to see the sorting process.

Uploaded by

gagana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

“Jnana Sangama”, Belgaum-590018.

Computer Graphics Mini Project On


“Insertion Sort”
Submitted in partial fulfillment for the requirements of the VI Semester
degree of

BACHELOR OF ENGINEERING
IN
COMPUTER SCIENCE AND ENGINEERING

For The Academic Year


2021-22
By
ABHISHEK N J
(1DB19CS004)

Under The Guidance Of


Prof. B.S UMASHANKAR
Professor
Dept. of CSE, DBIT

Department of Computer Science and Engineering

DON BOSCO INSTITUTE OF TECHNOLOGY


Kumbalagodu, Mysore Road, Bengaluru - 560 074.
DON BOSCO INSTITUTE OF TECHNOLOGY
Kumbalagodu, Bengaluru – 560 074.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

CERTIFICATE

This is to certify that the mini project report entitled “Insertion Sort” is a bonafide work carried
out by ABHISHEK N J (1DB19CS004) in partial fulfillment of award ofDegree of Bachelor
of Engineering in Computer Science and Engineering of Visvesvaraya Technological
University, Belagavi, during the academic year 2021-2022. It is certified that all
corrections/suggestions indicated for Internal Assessment have been incorporated. The mini
project has been approved as it satisfies the academic requirements associated with the degree
mentioned.

Signature of guide Signature of HOD

………………… ………………….
Prof.B.S Umashankar Dr K B Shiva Kumar
Professor Head of Dept.,
Dept. of CSE, Dept. of CSE,
DBIT, Bengaluru. DBIT, Bengalu
DON BOSCO INSTITUTE OF TECHNOLOGY
Kumbalagodu, Bangalore – 560 074.

I, ABHISHEK N J, student of sixth semester B.E, Department of Computer Science and


Engineering, Don Bosco Institute of Technology, Kumbalagodu, Bangalore, declare that the
mini project work entitled “Insertion Sort” has been carried out by me and submitted in partial
fulfillment of the course requirements for the award of degree in Bachelorof Engineering in
Computer Science and Engineering of Visvesvaraya Technological University, Belgaum
during the academic year 2021-2022. The matter embodied in this reporthas not been submitted
to any other university or institution for the award of any other degree or diploma.

Place: Bangalore ABHISHEK N J


Date: 15/07/2022 1DB19CS004
ACKNOWLEDGEMENT

Here by I am submitting the CG mini project report on “Insertion Sort”, as per the scheme of
Visvesvaraya Technological University, Belgaum.

In this connection, I would like to express my deep sense of gratitude to my beloved institution
Don Bosco Institute of Engineering and also, I like to express my sincere gratitude and
indebtedness to Prof. B.S Umashankar, Principal, DBIT, Bangalore.

I would like to express my sincere gratitude to Dr. K.B. Shiva Kumar, Head of Department
of Computer Science and Engineering, for providing a congenial environment to work in and
carryout my mini project.

I would like to express the deepest sense of gratitude to thank my Project Guide Prof.B.S.
Umashankar, Professor, Department of Computer Science and Engineering, DBIT,
Bangalore for his constant help and support extended towards me during the course of the
project.

Finally, I am very much thankful to all the teaching and non-teaching members of the
Department of Computer Science and Engineering, my seniors, friends and my parents for their
constant encouragement, support and help throughout completion of mini project.

ABHISHEK N J
(1DB19CS004)

(I)
ABSTRACT

The main aim and objective of this was to plan and implement the knowledge gained by
studying OpenGL, to create a simple and fully functional computer graphics project. The
project “Insertion Sort” was developed after understanding the concepts of the sorting the
elements and the logic behind Insertion Sorting technique.

(II)
LIST OF FIGURES

FIGURE: PAGE:

Fig 6.1: Flowchart...………………………………………………………………....13

Fig 6.2: Graphical interaction loop...………………………………………………14


Fig 9.1: Front page of the project……..………………………………………………..24

Fig 9.2: Bar graph to be sorted……..………………………………………………26

Fig 9.3: Sorting in process.………………………………………………………….26

Fig 9.4: Sorting Finished………………..…………………………………………..27

LIST OF ABBREVATIONS

• OpenGl: Open Graphics Library


• GLU: OpenGL Utility Library
• GLUT : OpenGL Utility Toolkit

(III)
CONTENTS

CHAPTERS Pg. No
1. INTRODUCTION 8
2. LITERATURE SURVEY 9
3. PROPOSED SYSTEM 10
4. ANALYSIS
Software Requirements 11
Hardware Requirements 11

5 PROJECT DESIGN 12

6. SYSTEM DESIGN
FLOWCHART/ARCHITECTURE 13
CONCEPTS AND PRINCIPLES OF OPENGL 14
7. IMPLEMENTION
Overview of System Implementation 15
8. SOURCE CODE 18
9. SNAPSHOTS 24
10. CONCLUSION 26

REFERENCES 27
Don Bosco Institute of Technology Insertion Sort

CHAPTER 1

INTRODUCTION

Graphics provides one of the most natural means of communicating with a computer, since our
highly developed 2D and 3D pattern recognition abilities allow us to perceive and process
pictorial data rapidly and efficiently. Interactive computer graphics is the most important means
of producing pictures since the invention of photography and television. It has the added
advantage that, with the computer, we can make pictures not only of concrete real world objects
but also of abstract, synthetic objects, such as mathematical surfaces and of data that have no
inherent geometry, such as survey results.

1.1. Aim
The aim of this project is to demonstrate the working of Insertion Sorting technique.

1.2. Objective
The objective of this project is to make the user understand the concept of how the process of
sorting occurs in Insertion Sort. The package must also have a user-friendly interface.

1.3 Scope
It is developed in OpenGL. It has been implemented on Visual Studio platform. The 3-D
graphics package designed here provides an interface for the users to view the sorting process
visually..

Dept. of CSE 2021-2022 8


Don Bosco Institute of Technology Insertion Sort

CHAPTER 2

LITERATURE SURVEY

OpenGL, a graphics software system has become a widely accepted standard for developing
graphics applications. Fortunately, OpenGL is easy to learn and it possesses most of the
characteristics of other popular graphics system.
• Using OpenGL library, we can create various graphics related structure.

• Different functions defined in the OpenGL can be used to give different colors and textures
to the surrounding objects.
• Glut (Toolkit of OpenGL library) can be used to create window, translate and rotate the
matrix of the object, to track the position of mouse, to show the render screen etc.

Example:

• To create the window Function used: glutCreateWindow ("window name")

• To define the position of window Function used: glutInitWindowposition (int x, int y) X


and Y are the co-ordinates of top left part of window in pixel.
• Define the size of window Function used: glutInitWindowsize (int x, int y) x and y are
the width and height of the window.

Likewise, many user-defined functions are used to manipulate the graphs sorting. The works
are commented in the source code. In this way I gathered most of my knowledge from the
previously made similar projects and got familiar with Open GL library and its toolkit.

Dept. of CSE 2021-2022 9


Don Bosco Institute of Technology Insertion Sort

CHAPTER 3
PROPOSED SYSTEM

In the proposed system, OpenGL is a graphic software system designed as a streamlined,


hardware-independent interface to be implemented on many different hardware platforms. To
achieve these qualities, no commands for performing windowing tasks or obtaining user input are
included in OpenGL; instead, we must work through whatever windowing system controls the
hardware you’re using.
Main aim of this project is to demonstrate the process of sorting using the insertion sort technique.

Dept. of CSE 2021-2022 10


Don Bosco Institute of Technology Insertion Sort

CHAPTER 4
ANALYSIS

4.1 SOFTWARE REQUIREMENTS:

• Operating system : Windows 10


• Language :C
• Compiler : GLUT GNU Compiler
• OpenGL : Library Files and dll Files
• Editor : Visual Studio 2010

4.2 Hardware Requirements:

• Processor : Intel 486/Pentium processor and above

• Processor speed : 500MHz or above

• RAM : 64 MB or above

• Storage space : 2 MB or above

• Monitor Resolution : color monitor with minimum resolution of 640*480

Dept. of CSE 2021-2022 11


Don Bosco Institute of Technology Insertion Sort

CHAPTER 5
PROJECT DESIGN

This chapter documents a detailed description of the implementation of our project. We have incorporated
several inbuilt OpenGL functions in this project. The following code snippet enables the easy rendering of
the graphs to be visualized in the form of bar graphs.
{
glClear(s. . .);
glVertex2f(. . .);
glColor3f(. . .);
glMatrixMode(. . .);
glLoadIdentity(. . .);
}

5.1 The header files used are:


1. #include<stdio.h>: This is a C library function for standard input and output.
2. #include<math.h>: This is a C library that contains mathematic functions.
3. #include<GL/glut.h>: This header is included to read the glut.h, gl.h and glu.h.
4. #include<string.h>: This is a C library function for performing operations on strings.

5.2 Functions used:


Init(); Here we initialize the color buffer, set the matrix mode and set window
co-ordinate values.
front(); This function is used to demonstrate the first main page of the project which displays the title with
the info to run the program.
initialise(); This function is used set the sorting array.
glutPostRedisplay(); It ensures that display will be drawn only once each time program goes through the
event loop.
glutMainLoop(); This function whose execution will cause the program to begin an event processing loop.

Dept. of CSE 2021-2022 12


Don Bosco Institute of Technology Insertion Sort

CHAPTER 6
SYSTEM DESIGN

6.1 FLOWCHART/ARCHITECTURE

Fig6.1: Flowchart of the events in the project

Dept. of CSE 2021-2022 13


Don Bosco Institute of Technology Insertion Sort

6.2 CONCEPTS AND PRINCIPLES OF OPENGL

6.2.1 THE GRAPHICAL INTERFACE:

The application program has its own internal model. It draws the graphics using the facilities of the
graphics system. The user views the graphics, and uses input devices, such as a mouse, to interact.
Information about the users is sent back to the applications are sent back to the application, which
decides what action to take. Typically, it will make changes to its internal model, which will cause
the graphics to be updated, and so another loop in the interaction cycle begins.

Application model

Application program

Graphics system (OpenGL)

Mouse etc. Display

User

Fig 6.2: Graphical interaction

Dept. of CSE 2021-2022 14


Don Bosco Institute of Technology Insertion Sort

CHAPTER 7
IMPLEMENTATION

This program has been developed and implemented using OpenGL interface. Incorporate this
facility includes glut.h header files.

4.1 Attributes

1. Void glColor3[b I f d ub us ui](TYPE r,TYPEg,TYPE b)


Sets the present RGB colors. Valid types are byte(b),int (i), float (f), double (d), unsigned byte(ub),
unsigned short(us) and unsigned int (ui).

2. Void glClearColor(GLclampfg,Glclampfg,Glclampfb, Glclampfa)


Sets the present RGBA clear color used when clearing the color buffer. Variable of Glclampfare floating
point numbers between 0.0 and1.0.

4.2 Working with the window system

1. Void glFlush()
Forces any buffered openGL commands to execute.

2. Void glutInit(int argc,char **argv)


Initializes GLUT. The arguments from main are passed in and can be used by the application.

3. Void glutCreateWindow(char*title)
Creates a window on the display. The string title can be used by the window. The return value
provides reference to the windows that can be used when there are multiple windows.

4. Void glutInitDisplayMode(unsigned int mode)


Requests a display with the properties in mode. The value of mode is determined by the logical
OR of options including the color model(GLUT_RGB, GLUT_INDEX) and buffering
(GLUT_SINGLE,GLUT_DOUBLE).

Dept. of CSE 2021-2022 15


Don Bosco Institute of Technology Insertion Sort

5. void glutInitWindowSize(intwidth,int height)


Specifies the initial height and width of the window in pixels.

6. void glutInitWindowPosition(intx,int y)
Specifies the initial position of top-left corner of the window in pixels.

7. void glViewport(intx,inty,Glsizei height)


Specifies a width x height viewport in pixels whose lower left corner is at (x,y) measured
from origin of the window.

8. void glutMainLoop()
Causes the program to enter an event processing loop. It should be the last statement in main.

9. Void glutDisplayFunc(void(*func)(void))
Requests that the display callback be executed after the current callback returns.

10. void glutSwapBuffers()


Swaps the front and back buffers.

4.3 Interaction

1. void glutReshapeFunc(void *f(intwidth,int height)


Registers the reshape callback function f. The callback function returns the height and
width of the new window. The reshape callback invokes display callback.

4.4 Transformation

1. void glMatrixmode(GLenum mode)


Specifies which matrix will be affected by subsequent transactions. Mode can be
GL_MODEL_VIEW,GL_PROJECTION or GL_TEXTURES.

Dept. of CSE 2021-2022 16


Don Bosco Institute of Technology Insertion Sort

2. Void glLoadIdentity()
Sets the current transformation matrix to an identity matrix.

3. void glPushMatrix() & void glPopMatrix()


Pushes to and pops from the matrix stack corresponding to the current matrix mode.

4. void glRotate[fd](TYPE x, TYPE y,TYPE z)


Alters the current matrix by a rotation of the angle degree about the axis(dx,dy,dz).TYPE is
either Glfloat or Gldouble.

5. void glTranslate[fd](TYPE x, TYPE y,TYPE z)


Alters the current matrix by a displacement of(x,y,z). TYPE is of either Glfloat or
Gldouble.

4.5 Viewing

1. void glOrtho2D(Gldoubleleft,Gldoubleright,Gldoublebottom,Gldouble top)


Defines a two dimensional viewing rectangle in the plane z=0.

Dept. of CSE 2021-2022 17


Don Bosco Institute of Technology Insertion Sort

CHAPTER 8
SOURCE CODE

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<GL/glut.h>
#define SORT_NO 1 // Number of sorting algorithms
#define MAX 50 // Number of values in the array
#define SPEED 700 // Speed of sorting, must be greater than MAX always
int a[MAX]; // Array
int swapflag=0; // Flag to check if swapping has occured
int i=0,j=0; // To iterate through the array
int flag=0; // For Insertion Sort
int k=0; // To Switch from Welcome screen to Main Screen
int sorting=0; // 1 if Sorted
char *sort_string[]={"Insertion Sort"};
int sort_count=0; // To cycle through the string

// Function to display text on screen char by char


void bitmap_output(int x, int y, char *string, void *font)
{
int len, i;

glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len; i++) {
glutBitmapCharacter(font, string[i]);
}
}

// Function to integer to string


void int_str(int rad,char r[])
{
_itoa(rad,r,10);
}

void display_text()
{
glColor3f(0,0,1);
bitmap_output(150, 665, "DYNAMIC SORTING ALGORITHM
VISUALIZER",GLUT_BITMAP_TIMES_ROMAN_24);
glBegin(GL_LINE_LOOP);
glVertex2f(145, 660);
glVertex2f(520, 660);
glEnd();
Dept. of CSE 2021-2022 18
Don Bosco Institute of Technology Insertion Sort

// other text small font


bitmap_output(10, 625, "This program sorts a random set of numbers in ascending order and
demonstrates graphically the insertion sort technique ",GLUT_BITMAP_HELVETICA_18);

if (sorting == 0) // if not sorting display menu


{
bitmap_output(10, 575, "MENU",GLUT_BITMAP_HELVETICA_18);
bitmap_output(10, 555, "Press s to SORT",GLUT_BITMAP_HELVETICA_18);
bitmap_output(10, 535, "Press r to
RANDOMISE",GLUT_BITMAP_HELVETICA_18);
bitmap_output(10, 515, "Esc to QUIT",GLUT_BITMAP_HELVETICA_18);
bitmap_output(10, 475, "Insertion sort: ",GLUT_BITMAP_HELVETICA_18);
}
else if (sorting == 1) // while sorting
{
glColor3f(1.0,0.0,0.0);
bitmap_output(10, 555, "Sorting in progress...",GLUT_BITMAP_HELVETICA_18);
bitmap_output(10, 535, "Press p to PAUSE",GLUT_BITMAP_HELVETICA_18);
glColor3f(0.0,1.0,0.0);
}
}

void front()
{
glColor3f(0.0,1.0,0.0);
bitmap_output(300, 565, "WELCOME!",GLUT_BITMAP_TIMES_ROMAN_24);
glBegin(GL_LINE_LOOP);
glVertex2f(295, 560);
glVertex2f(395, 560);
glEnd();
bitmap_output(330, 525, "TO",GLUT_BITMAP_TIMES_ROMAN_24);
glBegin(GL_LINE_LOOP);
glVertex2f(325, 521);
glVertex2f(360, 521);
glEnd();

bitmap_output(150, 475, "DYNAMIC SORTING ALGORITHM


VISUALIZER",GLUT_BITMAP_TIMES_ROMAN_24);
glBegin(GL_LINE_LOOP);
glVertex2f(145, 470);
glVertex2f(520, 470);
glEnd();
glColor3f(0.0,0.0,1.0);
glBegin(GL_QUADS);

glVertex2f(520,120.0);glVertex2f(520,170);glVertex2f(796,170);glVertex2f(796,120.0);
glEnd();
glColor3f(0.0,1.0,0.0);

Dept. of CSE 2021-2022 19


Don Bosco Institute of Technology Insertion Sort

bitmap_output(530, 125, "Press Enter to continue.......",GLUT_BITMAP_HELVETICA_18);


}

void Initialize() {
int temp1;

// Reset the array


for(temp1=0;temp1<MAX;temp1++){
a[temp1]=rand()%100+1;
printf("%d ",a[temp1]);
}

// Reset all values


i=j=0;
flag=0;

glClearColor(1,1,1,1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 699,0,799);
}

// Return 1 if not sorted


int notsorted(){
int q;
for(q=0;q<MAX-1;q++)
{
if(a[q]>a[q+1])
return 1;
}
return 0;
}

// Main function for display


void display()
{
int ix,temp;
glClear(GL_COLOR_BUFFER_BIT);

if(k==0)
front();
else{
display_text();
char text[10];

for(ix=0;ix<MAX;ix++)
{
glColor3f(0.5,0.5,0.5);
glBegin(GL_LINE_LOOP);
glVertex2f(10+(700/(MAX+1))*ix,50);
Dept. of CSE 2021-2022 20
Don Bosco Institute of Technology Insertion Sort

glVertex2f(10+(700/(MAX+1))*(ix+1),50);
glVertex2f(10+(700/(MAX+1))*(ix+1),50+a[ix]*4);
glVertex2f(10+(700/(MAX+1))*ix,50+a[ix]*4);
glEnd();

int_str(a[ix],text);
//printf("\n%s",text);
glColor3f(0,0,0);
bitmap_output(12+(700/(MAX+1))*ix, 35,
text,GLUT_BITMAP_TIMES_ROMAN_10);
}

if(swapflag || sorting==0)
{
glColor3f(0.5,0.5,0.5);
glBegin(GL_LINE_LOOP);
glVertex2f(10+(700/(MAX+1))*j,50);
glVertex2f(10+(700/(MAX+1))*(j+1),50);
glVertex2f(10+(700/(MAX+1))*(j+1),50+a[j]*4);
glVertex2f(10+(700/(MAX+1))*j,50+a[j]*4);
glEnd();
swapflag=0;
}
}
glFlush();
}

// Insertion Sort
void insertionsort()
{
int temp;

while(i<MAX)
{
if(flag==0){j=i; flag=1;}
while(j<MAX-1)
{
if(a[j]>a[j+1])
{
swapflag=1;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;

goto A;
}
j++;
if(j==MAX-1){flag=0;}
printf("swap %d and %d\n",a[j],a[j+1]);
}

Dept. of CSE 2021-2022 21


Don Bosco Institute of Technology Insertion Sort

i++;
}
sorting=0;
A:
i=j=0;
}

// Timer Function, takes care of sort selection


void makedelay(int)
{
if(sorting)
{
switch(sort_count)
{
case 0: insertionsort(); break;
}
}
glutPostRedisplay();
glutTimerFunc(SPEED/MAX,makedelay,1);
}

// Keyboard Function
void keyboard (unsigned char key, int x, int y)
{
if(key==13)
k=1;
if (k==1 && sorting!=1)
{
switch (key)
{
case 27 :exit (0); // 27 is the ascii code for the ESC key
case 's' :sorting = 1; break;
case 'r' :Initialize(); break;
}
}
if(k==1 && sorting==1)
if(key=='p') sorting=0;
}
// Main Function
void main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(1000,600);
glutInitWindowPosition(0,0);
glutCreateWindow("Dynamic Sorting Visualizer");
Initialize();

Dept. of CSE 2021-2022 22


Don Bosco Institute of Technology Insertion Sort

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutTimerFunc(1000,makedelay,1);
glutMainLoop();
}

Dept. of CSE 2021-2022 23


Don Bosco Institute of Technology Insertion Sort

CHAPTER 9

SNAPSHOTS

Fig 9.1 Initial page of the project on running the project.

The user has to press enter key to be redirected to sorting page

Fig 9.2 Displaying the bar graph to be sorted.

The bar graph of the randomly generated which has to be sorted.

Dept. of CSE 2021-2022 24


Don Bosco Institute of Technology Insertion Sort

Fig 9.3 Sorting in progress.

The array is getting sorted with the insertion sort technique

Fig 9.4 Sorting finished

The process of insertion sort is completed.

Dept. of CSE 2021-2022 25


Don Bosco Institute of Technology Insertion Sort

CHAPTER 10
CONCLUSION

An attempt has been made to develop an openGL which meets necessary requirements of the
user successfully. Since it is user friendly it enables user to interact efficiently and easily.The
development of mini project has given us a good exposure to openGL by which we havelearnt
some of the techniques which helps in the development of animation .Hence it is helpful for us
to even take this field as our career too and develop some otherfeatures in openGL and provide
as token of contribution to the graphics world.

FUTURE ENHANCEMENT:

This project has been designed using C++, which works on the windows platform.The project
can be designed using other languages and better graphical interfaces. The following features
could have been incorporated.

✓ Circular and priority queues can be implemented.


✓ The project could have been entirely in 3D instead of 2D

Dept. of CSE 2021-22 26


Don Bosco Institute of Technology Insertion Sort

REFERENCES

[1] Edward Angel’s Interactive Computer Graphics Pearson Education 5th Edition
[2] Interactive computer Graphics - A top down approach using open GL by Edward Angel
[3] Jackie L.Neider,Mark Warhol, Tom R.Davis,"OpenGL Red Book", Second Revised
Edition,2005.
[4] Donald D Hearn and M.PaulineBaker,"Computer Graphics with OpenGL",3rd
Edition.

WEBSITES

[5] www.OpenGLRedbook.com
[6] www.OpenGLsimpleexamples.com
[7] www.OpenGLprogramminguide.com
[8] www.basic4glwikispaces.com
[9] www.opengl.org

Dept. of CSE 2021-22 27

You might also like