⬆️ ⬇️

Visualization of sorting exchanges



This article discusses various options for sorting exchanges, and also describes a simple graphical application ( processing.js ) with examples of sorts.



Before reading, I recommend reading the articles:



Sort exchanges

Bubble sort and all-all

Stupid sorting and some others, smarter

')

Bubble Sort



The simplest option is to iterate over the array from the first element to the last, swapping (if necessary) adjacent elements.



→ Check here



In order to move the slider, you need to click on the gray button in the lower left corner.

Pressing the button moves the slider one step to the right.

incPaddle++; 


Next, we check: have we reached the end of the array (then jump to the beginning), then compare (swap) the neighboring elements:



Button code
 //  //  void mousePressed() { if(boolButton) { counter++; //    if(mods[incPaddle].rectHight < mods[incPaddle-1].rectHight) { vTemp= mods[incPaddle-1].rectHight; mods[incPaddle-1].rectHight=mods[incPaddle].rectHight; mods[incPaddle].rectHight=vTemp; } } } // void mouseReleased() { if(boolButton) { //   incPaddle++; //     if(incPaddle>=moduleSize) { incPaddle=1; } } } 




Processing.js uses Java data structures, then the code is translated into javascript, so

declaration of an array of instances of the Module class, responsible for drawing elements and initializing instances, is as follows:



Code
 Module[] mods; ... mods[0] = new Module(1*30, 30); mods[1] = new Module(2*30, 50); mods[2] = new Module(3*30, 10); mods[3] = new Module(4*30, 60); mods[4] = new Module(5*30, 20); mods[5] = new Module(6*30, 100); mods[6] = new Module(7*30, 80); mods[7] = new Module(8*30, 70); mods[8] = new Module(9*30, 90); mods[9] = new Module(10*30, 40); 




The main function of drawing void draw () is an infinite loop in which instances of a class are sorted:

 for (Module mod : mods) { mod.display(); } 




You can reduce the number of iterations if you do not iterate through already sorted items. To do this, add the limiter (limiter) to the end of the array, which will move to the beginning of the array after each iteration.

 if(incPaddle>=limiter) { incPaddle=1; limiter--; if(limiter<1) limiter=1; } 




→ Check here



Another way to reduce the number of searches can be found in the article Sorting bubble (Wikipedia). If during the passage of the array we have never changed the adjacent elements in places, then the array is sorted and the cycle can be completed.



Add a flag flag that rises when we meet a couple of items that need to be swapped. If the flag is raised, loop through the array again. If not, end the cycle. The status of the flag is displayed in the console.



Neighbor Check
 if(mods[incPaddle].rectHight < mods[incPaddle-1].rectHight) { flag=true; //   vTemp= mods[incPaddle-1].rectHight; mods[incPaddle-1].rectHight=mods[incPaddle].rectHight; mods[incPaddle].rectHight=vTemp; } 




→ Check here



Sort of even-odd



We will move the slider in two steps, comparing the neighboring elements

 incPaddle=incPaddle+2; 


So we compare elements 0 and 1, 2 and 3, 4 and 5, 6 and 7, etc.

Having reached the end of the mvssiv, move the limiter one step to the left, put the slider at the beginning of the array and compare the elements 1 and 2, 3 and 4, 5 and 6, 7 and 8, etc.

Having reached the limiter, we shift it one step to the right, we put the slider at the beginning of the array.



→ Check here



Dwarf sort



After raising the flag, we save the coordinate of the slider in the variable limiterR and move the slider to the beginning of the array in steps.

 incPaddle--; 


Once at the beginning of the array, reset the flag and put the slider in the cell with the coordinate that we saved to the variable limiterR

 if(incPaddle==0){ flag=false; incPaddle=limiterR; } 




→ Check here



Hairbrush sorting



Changing the logic of the limiter, we obtain the sorting by combing / combing. Compare (swap) elements. We move the limiter left one step and save its index in the variable limiterStore .

 if(limiter==moduleSize) { limiter = limiterStore-1; limiterStore = limiter; incPaddle=1; } 


Clicking on the steps slider with the limiter to the end of the array

 if(limiter!=moduleSize) { //  limiter     limiter++; incPaddle++; } 




Reaching the limiter to the end of the array, put the slider at the beginning of the array, and put the limiter in limiterStore-1 :

 if(limiter==moduleSize) { limiter = limiterStore-1; limiterStore = limiter; incPaddle=1; } 




→ Check here



Shaker sorting





Add the limiterL limiter to the beginning of the array.

Let the flag flag rise when the slider reaches the right limiterR , then the limiter is shifted left by one step. Further, when the flag is raised, the slider moves in steps back to the left limiter limiterL - the limiter moves to the right by one step - the slider moves in steps to the right ...

 //  void mouseReleased() { if(boolButton) { if(!flag) { incPaddle++; if(incPaddle==limiterR) { flag=true; limiterR--; if(limiterR<=limiterL+1) limiterR=limiterL+1; } } if(flag) { incPaddle--; if(incPaddle==limiterL) { flag=false; limiterL++; if(limiterL>=limiterR-1) limiterL=limiterR-1; } } } } 




→ Check here

Source: https://habr.com/ru/post/454792/



All Articles