#include iolib.h
#include args.h
#include printf1.h

char array[ 700 ]; /* Maximum of 700 byte data file, can be changed */
int count, *ptr[ 50 ]; /* Maximum of 50 strings to sort, can be changed */

main()
{
setargs();  /* Scan command line for I/O redirection */

puts( "\n\n**** Bubble Sort ****\n" );
puts( "After the command name put these:-\n" );
puts( "<<filename  -  filename is the name of the input data file\n" );
puts( ">filename   -  Will put sorted list to filename\n" );
puts( "If the > option is omited, the list is put to screen\n\n" );

/* The next line works from the innermost pair of brackets */
/* and each function passes the number of lines used to    */
/* the next function block.                                */

write( sort( read() ) );
}

sort( lines )
int lines; /* Max number of lines */
{
int pos, flag;

puts( "\n\nSorting....\n" );

flag = 1;
while( flag )
	{

 /* This program will continue to loop until the comparing  */
 /* section cannot find anything to swap. The value of      */
 /* flag will still be zero so while() will be false & exit */

	flag = 0; count = 0;

 /* First the program searches by going from the first */
 /* string to the last                                 */

	while( count++ < lines )
		{

  /* The small compound block below searches through the */
  /* two strings count & count+1 character at a time     */
  /* until it finds a character that is not the same     */

		pos = 0;
		while( 1 )
			{
			if( *(ptr[ count ] * pos ) != *(ptr[ count+1 ] * pos ) )
				break;
			pos++;
			}

  /* Once the character has been found, it is compared    */
  /* to see if count > count+1, if it is they are swapped */

		if( *(ptr[ count ] * pos ) > *(ptr[ count+1 ] * pos ) ) 
   			{
   			swap( count, count+1 );
   			flag = 1;
   			}
  		}

 /* Then the program searches by going from the */
 /* last string to the first                    */

	count = lines-1;
	while( count-- )
		{
		pos = 0;
		while( 1 )
			{
			pos++;
			if( *(ptr[ count ] * pos ) != *(ptr[ count+1 ] * pos ) )
				break;
   			}
  		if( *(ptr[ count ] * pos ) > * (ptr[ count+1 ] * pos ) )
   			{
   			swap( count, count+1 );
   			}
  		}
 	}
/* Now the array of pointers will hold the addresses */
/* of the strings in the right order. The next line  */
/* returns the maximum number of lines to print      */

return lines;
}

swap( line1, line2 )
int line1, line2;
{
int temp;

/* Swap two pointers that can be found at line1 & line2 */

temp = ptr[ line1 ];
ptr[ line1 ] = ptr[ line2 ];
ptr[ line2 ] = temp;
}

read()
{
int fin, count, pcount;
char c;

count = 0; pcount = 1;

ptr[ pcount ] = &array[ 0 ]; /* First pointer points to start of data */

while( ( c = getchar() ) != -1 )  /* Read until EOF */
	{
 	array[ count ] = c; /* Put char into data array */

 	if( c == '\n' ) /* Is char a CR/LF ? */
  		{
  		array[ count ] = '\000';
  		pcount++;
  		ptr[ pcount ] = &array[ count+1 ]; /* Set pointer to char */
  		}
 	count++;
 	}

printf( "\n%d bytes in data file & %d words to be sorted", count, pcount);

return pcount-1; /* Above loop will add an extra line so subract 1 */
}

write( max )
int max;
{
int count;

count = 0;
while( count++ <= max )
 	{
 	puts( ptr[ count ] ); /* Output list of pointers in correct order*/
 	putchar( 13 );
 	}
}


