Home     Mosaic     CLIP     Octave     R Project     Processing     Contact  
Search
<http://cnfolio.com/IntroComputingPractical10>
Introduction to Computing – B142L

B142L Computing practical – Introduction to algorithms with insertion and bubble sort

Academic year 2009/10


Learning outcomes

  1. Practice using pseudo code and D structures to document program design.
  2. Practice implementation of algorithms to sort basic data types.



Typical problem solving steps:
  1. Identify and understand the problem
    • Ask clarification questions
    • Confirm assumptions
    • Confirm the range of acceptable inputs
    • Confirm the output requirements
    • Document possible error conditions
  2. Identify possible solutions
    • Sources of possible solutions:
      • Prior knowledge and experience
      • Experimentation
      • Research
    • Identify and describe each possible solutions
    • There are 2 types of solutions:
      • Algorithmic solutions use a series of steps that generally occur in the same order.
      • Heuristic solutions require a series of steps that do not always occur in the same order.
  3. Select best or appropriate solution from alternatives
    • Identify the key reason(s) for selecting a particular solution
  4. Implement the selected solution
  5. Use and evaluate the solution
    • Select suitable inputs for testing the solution
    • Be prepared to adjust the solution based on test results
    • Be prepared to use a different solution if the test results are not satisfactory



An algorithm is a list of steps for solving a problem which is suited for computer implementation. This includes algorithmic and heuristic solutions.



Exercise 1: Examples of algorithms


Microsoft Excel macros are excellent examples of algorithms that are widely used by many people.

What are other examples of algorithms that are commonly used on a daily basis?



Exercise 2: Practice with numeric arrays


Write a C program that uses an array to store up to 15 input floating point numbers. The program should:
#include <stdio.h>

int main( void )
{
   float numbers[ 15 ];
   int counter;

   for ( counter = 0; counter < 15; counter++ )
   {
      scanf( "%f", &( numbers[ counter ] ) );

      if ( numbers[ counter ] < 0 ) break;
      else printf( "%.2f ", numbers[ counter ] );
   }
}



Exercise 3: Practice with character arrays


Write a C program that accepts a text string input of up to 64 characters. The program should:
#include <stdio.h>
#include <ctype.h>

int main( void )
{
   char text[ 65 ];
   int counter;

   scanf( "%64c", text );

   for ( counter = 0; counter < 64; counter++ )
   {
      if ( isalnum( text[ counter ] ) ) printf( "%c", text[ counter ] );
      else break;
   }

   printf( " %d", counter );
}



Insertion sort algorithm


Read the description of the algorithm on Wikipedia:

Additionally, the following images and animation from Wikipedia may be useful:

Insertion sort sequence



Insertion sort sequence 2



Insertion sort animation



Insertion sort program


The following program implements insertion sorting for a fixed array of numbers.

#include <stdio.h>

#define QUANTITY   10

int main( void )
{
   int numbers[ QUANTITY ] = { 4, 7, 1, 94, 23, 56, 81, 12, 37, 46 };
   int outside, inside, display, temp;
   
   for ( outside = 1; outside < QUANTITY; outside++ )
   {
     for ( inside = outside; inside > 0; inside-- )
     {
         if ( numbers[ inside ] < numbers[ inside - 1 ] )
         {
            temp = numbers[ inside ];
            numbers[ inside ] = numbers[ inside - 1 ];
            numbers[ inside - 1 ] = temp;
         }
         else
            break;
      }

      for ( display = 0; display < QUANTITY; display++ )
         printf( "%d ", numbers[ display ] );

      printf( "\n" );
   }
}




Exercise 4: Insertion sort program


Write a C program that uses an array to store up to 10 input integer numbers. The program should:
#include <stdio.h>

#define QUANTITY   15

int main( void )
{
   int numbers[ QUANTITY ];
   int outside, inside, counter, temp;

   /* Obtain input numbers */
   for ( counter = 0; counter < QUANTITY; counter++ )
      scanf( "%d", &( numbers[ counter ] ) );

   /* Sort numbers */
   for ( outside = 1; outside < QUANTITY; outside++ )
   {
      for ( inside = outside; inside > 0; inside-- )
      {
         if ( numbers[ inside ] < numbers[ inside - 1 ] )
         {
            temp = numbers[ inside ];
            numbers[ inside ] = numbers[ inside - 1 ];
            numbers[ inside - 1 ] = temp;
         }
         else
            break;
      }
   }

   /* Display sorted numbers */
   for ( counter = 0; counter < QUANTITY; counter++ )
      printf( "%d ", numbers[ counter ] );
}



Exercise 5: Insertion sort program using characters


Write a C program that uses an array to store up to 20 input characters. The program should:
#include <stdio.h>

#define QUANTITY   20

int main( void )
{
   char text[ QUANTITY + 1 ];
   int outside, inside, counter;
   char temp;

   /* Obtain input characters */
   scanf( "%20c", text );

   /* Sort characters */
   for ( outside = 1; ( outside < QUANTITY ) && ( outside < strlen( text ) ); outside++ )
   {
      for ( inside = outside; inside > 0; inside-- )
      {
         if ( text[ inside ] > text[ inside - 1 ] )
         {
            temp = text[ inside ];
            text[ inside ] = text[ inside - 1 ];
            text[ inside - 1 ] = temp;
         }
         else
            break;
      }
   }

   /* Display sorted characters */
   printf( "%s", text );
}



Bubble sort algorithm


Read the description of the algorithm on Wikipedia:



Bubble sort program


The following program implements bubble sort for a fixed array of numbers.

#include <stdio.h>

#define QUANTITY   10

int main( void )
{
   int numbers[ QUANTITY ] = { 4, 7, 1, 94, 23, 56, 81, 12, 37, 46 };
   int outside, inside, display, temp, sorted;
   
   sorted = 0;
   for ( outside = 1; ( outside < QUANTITY ) && ( sorted == 0 ); outside++ )
   {
     sorted = 1;
        
     for ( inside = 0; inside < ( QUANTITY - outside ); inside++ )
     {
         if ( numbers[ inside ] > numbers[ inside + 1 ] )
         {
            temp = numbers[ inside ];
            numbers[ inside ] = numbers[ inside + 1 ];
            numbers[ inside + 1 ] = temp;
            sorted = 0;
         }
      }

      for ( display = 0; display < QUANTITY; display++ )
         printf( "%d ", numbers[ display ] );

      printf( "\n" );
   }
}




Exercise 6: Bubble sort program


Write a C program that uses an array to store up to 10 input integer numbers. The program should:
#include <stdio.h>

#define QUANTITY   10

int main( void )
{
   int numbers[ QUANTITY ];
   int outside, inside, counter, temp, sorted;

   /* Obtain input numbers */
   for ( counter = 0; counter < QUANTITY; counter++ )
      scanf( "%d", &( numbers[ counter ] ) );

   /* Sort numbers */
   sorted = 0;
   for ( outside = 1; ( outside < QUANTITY ) && ( sorted == 0 ); outside++ )
   {
     sorted = 1;

     for ( inside = 0; inside < ( QUANTITY - outside ); inside++ )
     {
         if ( numbers[ inside ] < numbers[ inside + 1 ] )
         {
            temp = numbers[ inside ];
            numbers[ inside ] = numbers[ inside + 1 ];
            numbers[ inside + 1 ] = temp;
            sorted = 0;
         }
      }
   }

   /* Display sorted numbers */
   for ( counter = 0; counter < QUANTITY; counter++ )
      printf( "%d ", numbers[ counter ] );
}



Exercise 7: Bubble sort program using characters


Write a C program that uses an array to store up to 20 input characters. The program should:
#include <stdio.h>

#define QUANTITY   20

int main( void )
{
   char text[ QUANTITY + 1 ];
   int outside, inside, sorted;
   char temp;

   /* Obtain input characters */
   scanf( "%20c", text );

   /* Sort characters */
   sorted = 0;
   for ( outside = 1; ( outside < QUANTITY ) && ( sorted == 0 ); outside++ )
   {
     sorted = 1;

     for ( inside = 0; inside < ( QUANTITY - outside ); inside++ )
     {
         if ( text[ inside ] > text[ inside + 1 ] )
         {
            temp = text[ inside ];
            text[ inside ] = text[ inside + 1 ];
            text[ inside + 1 ] = temp;
            sorted = 0;
         }
      }
   }

   /* Display sorted characters */
   printf( "%s", text );
}