Advertisement
Guest User

Visualising sorting algorithms on NXT

a guest
Oct 21st, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.88 KB | None | 0 0
  1. #Visualising 3 sorting algorithms (bubble, insertion, shell) for NXT brick
  2. #Author: Alex Voron
  3. #Details: nnxt.blogspot.com/2012/10/nxt.html
  4.  
  5. #define WAIT_TIME 20
  6. #define MAX_NUMBERS 50
  7. #define NUM_OFFSET 60
  8.  
  9. int tArray[MAX_NUMBERS];
  10.  
  11. // Keep track of our sorting times.
  12. float sortingTimes[3] = {0.0, 0.0, 0.0};
  13.  
  14. long timer;
  15.  
  16. // Draw the lines on the screen after each iteration.
  17. void drawLines(int arr[], string functionName)
  18. {
  19.   ClearScreen();
  20.   TextOut(0, LCD_LINE1, functionName);
  21.   NumOut(NUM_OFFSET, LCD_LINE1, (CurrentTick() - timer) / 1000.0)
  22.  
  23.   for (int i = 0; i < MAX_NUMBERS; i++)
  24.   {
  25.     LineOut(i*2, 0, i*2, arr[i]);
  26.   }
  27.   Wait(WAIT_TIME);
  28. }
  29.  
  30. // Randomise our array by first filling it up
  31. void randomiseArray(int& arr[])
  32. {
  33.   int temp;
  34.   int randomPos;
  35.  
  36.   string functionName = "Random";
  37.  
  38.   timer = CurrentTick();
  39.  
  40.   for (int i = 0; i < MAX_NUMBERS; i++)
  41.   {
  42.     arr[i] = i;
  43.   }
  44.  
  45.   // now randomly mix it up, 2 times.
  46.   for (int i = 0; i < 2; i++)
  47.   {
  48.       for (int j = 0; j < MAX_NUMBERS; j++)
  49.       {
  50.         randomPos = Random(MAX_NUMBERS - 1);
  51.         temp = arr[j];
  52.         arr[j] = arr[randomPos];
  53.         arr[randomPos] = temp;
  54.             drawLines(arr, functionName);
  55.       }
  56.   }
  57. }
  58.  
  59. // Do a bubble sort on the numbers, returns the number of seconds taken
  60. float bubbleSort(int arr[])
  61. {
  62.   int temp;
  63.   int i, j;
  64.  
  65.   string functionName = "Bubble";
  66.  
  67.   timer = CurrentTick();
  68.  
  69.   for(i = 0; i < MAX_NUMBERS; i++)
  70.     {
  71.       for(j = 0; j < (MAX_NUMBERS - 1); j++)
  72.       {
  73.         if (arr[j] > arr[j+1])
  74.         {
  75.                 temp = arr[j];
  76.                 arr[j] = arr[j+1];
  77.                 arr[j+1] = temp;
  78.                 drawLines(arr, functionName);
  79.             }
  80.         }
  81.     }
  82.    
  83.     return (CurrentTick() - timer) / 1000.0;
  84. }
  85.  
  86. // Do an insertion sort on the numbers, returns the number of seconds taken
  87. float insertionSort(int arr[])
  88. {
  89.   int temp;
  90.   int i, j, k;
  91.  
  92.   timer = CurrentTick();
  93.  
  94.   string functionName = "Insertion";
  95.  
  96.   for (i = 1 ; i < MAX_NUMBERS ; i++ )
  97.     {
  98.         for (j = 0 ; j < i ; j++ )
  99.         {
  100.             if ( arr[j] > arr[i] )
  101.             {
  102.                 temp = arr[j] ;
  103.                 arr[j] = arr[i] ;
  104.  
  105.                 for (k = i ; k > j ; k-- )
  106.                 {
  107.                     arr[k] = arr[k - 1] ;
  108.                     drawLines(arr, functionName);
  109.                 }
  110.                 arr[k + 1] = temp ;
  111.                 drawLines(arr, functionName);
  112.             }
  113.         }
  114.     }
  115.     return (CurrentTick() - timer) / 1000.0;
  116. }
  117.  
  118. // Do a shell sort on the numbers, returns the number of seconds taken
  119. float shellSort(int arr[])
  120. {
  121.     int j, i, m, mid;
  122.   timer = CurrentTick();
  123.     string functionName = "Shell";
  124.  
  125.     for(m = MAX_NUMBERS/2 ; m > 0; m /= 2)
  126.     {
  127.         for(j = m; j < MAX_NUMBERS; j++)
  128.         {
  129.             for(i = j - m; i >= 0; i -= m)
  130.             {
  131.                 if(arr[i + m] >= arr[i])
  132.                     break;
  133.                 else
  134.                 {
  135.                     mid = arr[i];
  136.                     arr[i] = arr[i + m];
  137.                     arr[i + m] = mid;
  138.                     drawLines(arr, functionName);
  139.                 }
  140.             }
  141.         }
  142.     }
  143.     return (CurrentTick() - timer) / 1000.0;
  144. }
  145.  
  146. // The main task
  147. task main ()
  148. {
  149.   int arr[MAX_NUMBERS];
  150.   int copy[MAX_NUMBERS];
  151.   ArrayInit(arr, 0, MAX_NUMBERS);
  152.  
  153.   ClearScreen();
  154.  
  155.   // Populate the array with random data
  156.   randomiseArray(arr);
  157.  
  158.   Wait(1000);
  159.   // Iterate through each sorting algorithm
  160.   for (int i = 0; i < 3; i++)
  161.   {
  162.     // Make a copy of the original and sort that.
  163.     memcpy(copy, arr, sizeof(int) * MAX_NUMBERS);
  164.     ClearScreen();
  165.     switch(i)
  166.     {
  167.       case 0: sortingTimes[0] = bubbleSort(copy); break;
  168.       case 1: sortingTimes[1] = shellSort(copy); break;
  169.       case 2: sortingTimes[2] = insertionSort(copy); break;
  170.     }
  171.     Wait(1000);
  172.   }
  173.  
  174.   // Show the results.
  175.     ClearScreen();
  176.  
  177.   TextOut(10, LCD_LINE1, "Sorting times:");
  178.   TextOut(0, LCD_LINE2, "Bubble:");
  179.   NumOut(NUM_OFFSET, LCD_LINE2, sortingTimes[0]);
  180.   TextOut(0, LCD_LINE3, "Shell:");
  181.   NumOut(NUM_OFFSET, LCD_LINE3, sortingTimes[1]);
  182.   TextOut(0, LCD_LINE4, "Insertion:");
  183.   NumOut(NUM_OFFSET, LCD_LINE4, sortingTimes[2]);
  184.   TextOut(0, LCD_LINE5, "[Enter] to exit");
  185.  
  186.   while (!ButtonPressed(BTNCENTER, true));
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement