2020#include < chrono>
2121#include < conio.h>
2222#include < time.h>
23+ #include < thread>
2324#include < queue>
2425
2526using namespace std ;
27+ int countOfNumber = 43 ;
28+ int delay = 0 ;
29+ int compares = 0 ;
30+
2631void Draw (vector<int >& colors, Renderer& renderer, Shader& shader, vector<VertexArray>& vas, vector<glm::vec3>& translations, glm::mat4& result, IndexBuffer& ib, GLFWwindow* window)
2732{
2833 renderer.Clear ();
2934
30- for (int i = 0 ; i < 38 ; i++)
35+ for (int i = 0 ; i < countOfNumber ; i++)
3136 {
3237 glm::mat4 model = glm::translate (glm::mat4 (1 .0f ), translations[i]);
3338 glm::mat4 mvp = result * model;
@@ -45,6 +50,11 @@ void Draw(vector<int>& colors, Renderer& renderer, Shader& shader, vector<Vertex
4550 {
4651 shader.SetUniform4f (" u_Color" , 1 .0f , 0 .0f , 0 .0f , 1 .0f );
4752
53+ }
54+ else if (colors[i] == 3 )
55+ {
56+ shader.SetUniform4f (" u_Color" , 0 .0f , 1 .0f , 0 .0f , 1 .0f );
57+
4858 }
4959 shader.Bind ();
5060 vas[i].Bind ();
@@ -54,28 +64,30 @@ void Draw(vector<int>& colors, Renderer& renderer, Shader& shader, vector<Vertex
5464 glfwSwapBuffers (window);
5565 glfwPollEvents ();
5666}
67+
5768int partition (vector<int >& colors, vector<float >& array, int low, int high, Renderer& renderer, Shader& shader, vector<VertexArray>& vas, vector<glm::vec3>& translations, glm::mat4& result, IndexBuffer& ib, GLFWwindow* window)
5869{
5970 int pivot = array[high]; // pivot
6071 int p = high;
6172 int i = (low - 1 ); // Index of smaller element and indicates the right position of pivot found so far
62-
6373 for (int j = low; j <= high - 1 ; j++)
6474 {
6575 // If current element is smaller than the pivot
6676 colors[j] = 1 ;
67- colors[p] = 1 ;
77+ colors[p] = 3 ;
6878 Draw (colors, renderer, shader, vas, translations, result, ib, window);
69- Sleep (200 );
79+ Sleep (delay);
80+ compares++;
81+
7082 if (array[j] < pivot)
7183 {
7284 colors[j] = 2 ;
73- colors[p] = 2 ;
74-
85+
7586 i++; // increment index of smaller element
7687 colors[i] = 2 ;
7788 Draw (colors, renderer, shader, vas, translations, result, ib, window);
78- Sleep (200 );
89+ Sleep (delay);
90+
7991 swap (array[i], array[j]);
8092 float position1[] = {
8193 0 .0f , 0 .0f , // Bottom Left
@@ -94,30 +106,34 @@ int partition(vector<int>& colors, vector<float>& array, int low, int high, Rend
94106 vas[j].changeData (position2);
95107 vas[j].UnBind ();
96108 Draw (colors, renderer, shader, vas, translations, result, ib, window);
97- Sleep (200 );
109+ Sleep (delay);
110+
98111 colors[j] = 0 ;
99112 colors[p] = 0 ;
100113 colors[i] = 0 ;
101114
102115 Draw (colors, renderer, shader, vas, translations, result, ib, window);
103- Sleep (200 );
104-
116+
105117 }
106118 else
107119 {
108120 colors[j] = 0 ;
109121 colors[p] = 0 ;
110122 // colors[i] = 0;
111123 Draw (colors, renderer, shader, vas, translations, result, ib, window);
112- Sleep (100 );
124+ Sleep (delay);
125+
126+
113127 }
114128
115129 }
116130 colors[i+1 ] = 2 ;
117- colors[high] = 2 ;
131+ colors[high] = 3 ;
118132 Draw (colors, renderer, shader, vas, translations, result, ib, window);
119- Sleep ( 200 );
133+
120134 swap (array[i + 1 ], array[high]);
135+ Sleep (delay);
136+
121137 float position1[] = {
122138 0 .0f , 0 .0f , // Bottom Left
123139 20 .0f , 0 .0f , // Bottom Right
@@ -138,7 +154,8 @@ int partition(vector<int>& colors, vector<float>& array, int low, int high, Rend
138154 colors[i + 1 ] = 0 ;
139155 colors[high] = 0 ;
140156 Draw (colors, renderer, shader, vas, translations, result, ib, window);
141- Sleep (200 );
157+ Sleep (delay);
158+
142159 return (i + 1 );
143160}
144161void quickSort (vector<int >& colors, vector<float >& array, int low, int high, Renderer& renderer, Shader& shader, vector<VertexArray>& vas, vector<glm::vec3>& translations, glm::mat4& result, IndexBuffer& ib, GLFWwindow* window)
@@ -165,14 +182,16 @@ void bubblesort(vector<int>& colors,vector<float> &array, int const n, Renderer&
165182 {
166183 colors[j] = 1 ;
167184 colors[j + 1 ] = 1 ;
168- Draw (colors, renderer, shader, vas, translations, result, ib, window);
169- Sleep (100 );
185+ Draw (colors, renderer, shader, vas, translations, result, ib, window);
186+ Sleep (delay);
187+ compares++;
170188 if (array[j] > array[j + 1 ])
171189 {
172190 colors[j] = 2 ;
173191 colors[j + 1 ] = 2 ;
174192 Draw (colors, renderer, shader, vas, translations, result, ib, window);
175- Sleep (100 );
193+ Sleep (delay);
194+
176195 colors[j] = 0 ;
177196 colors[j + 1 ] = 0 ;
178197
@@ -194,17 +213,16 @@ void bubblesort(vector<int>& colors,vector<float> &array, int const n, Renderer&
194213 vas[j].UnBind ();
195214 vas[j+1 ].changeData (position2);
196215 vas[j+1 ].UnBind ();
197- // swap(translations[j].x, translations[j + 1].x);
198216 Draw (colors, renderer, shader, vas, translations, result, ib, window);
199- Sleep (100 );
200-
217+ Sleep (delay);
201218 }
202219 else
203220 {
204221 colors[j] = 0 ;
205222 colors[j + 1 ] = 0 ;
206223 Draw (colors, renderer, shader, vas, translations, result, ib, window);
207- Sleep (100 );
224+ Sleep (delay);
225+
208226 }
209227
210228 }
@@ -239,26 +257,20 @@ int main(void)
239257 return -1 ;
240258 }
241259 glfwMakeContextCurrent (window);
260+ glfwSwapInterval (1 );
242261 glewInit ();
243262 glEnable (GL_BLEND);
244263 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
245264 // ////////////////////////////////////////////////////////////
246265
247- vector<vector<float >> positions (38 );
248- vector<VertexArray> vas (38 );
266+ vector<vector<float >> positions (countOfNumber );
267+ vector<VertexArray> vas (countOfNumber );
249268 VertexBufferLayout layouts;
250269 layouts.Push <float >(2 );
251-
252- // = {
253- // 0.0f, 0.0f, //Bottom Left
254- // 20.0f, 0.0f, //Bottom Right
255- // 20.0f, 20.0f, //Top right
256- // 0.0f, 20.0f, //Top Left
257- // };
258270 srand ((unsigned )time (0 ));
259- vector<float > heights (38 );
271+ vector<float > heights (countOfNumber );
260272
261- for (int i = 0 ; i < 38 ; i++)
273+ for (int i = 0 ; i < countOfNumber ; i++)
262274 {
263275 float result = 10 + (rand () % 960 );
264276 heights[i] = result;
@@ -272,7 +284,6 @@ int main(void)
272284 vas[i].AddBuffer (*vb, layouts);
273285 }
274286
275-
276287 unsigned int indices[] = {
277288 0 ,1 ,2 ,
278289 2 ,3 ,0
@@ -288,27 +299,24 @@ int main(void)
288299 Shader ss;
289300 ss.Bind ();
290301 ib.UnBind ();
291- /* for (int i = 0; i < 20; i++)
292- {
293- vas[i].UnBind();
294- }*/
295302
296303 Renderer renderer;
297- vector<glm::vec3> translations (38 );
304+ vector<glm::vec3> translations (countOfNumber );
298305
299306 Shader shader;
300307 float stepX = 0 ;
301- vector<int > colors (38 );
302- for (int i = 0 ; i < 38 ; i++)
308+ vector<int > colors (countOfNumber );
309+ for (int i = 0 ; i < countOfNumber ; i++)
303310 {
304311 translations[i].x = stepX;
305312 translations[i].y = 0 ;
306- stepX += 25 ;
313+ stepX += 22 ;
307314 }
308315
309- bubblesort (colors, heights, 38 , renderer, shader, vas, translations, result, ib, window);
310- // quickSort(colors, heights, 0,37 , renderer, shader, vas, translations, result, ib, window);
316+ // bubblesort(colors, heights, countOfNumber , renderer, shader, vas, translations, result, ib, window);
317+ quickSort (colors, heights, 0 , countOfNumber- 1 , renderer, shader, vas, translations, result, ib, window);
311318
319+ cout << compares;
312320 while (!glfwWindowShouldClose (window))
313321 {
314322 Draw (colors,renderer,shader,vas,translations,result,ib,window);
0 commit comments