Bubble sort using C / How do i use do while

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • poreko
    New Member
    • Nov 2006
    • 38

    Bubble sort using C / How do i use do while

    hi guys! i am doing a bubble sort on using the C language. I have to prompt the user to enter 10 integers values of his/her choice.

    Store these values into an array in the order the user entered them.
    Call a function that will display the values entered in either

    i) Ascending order
    ii) Descending order
    i have already written it but i need some suggestions on how to improve my program. I was told to use use the Do While function but i cant think of anything.

    thanks!

    here is what have come up with:

    Code:
    # include<stdio.h> 
    
    void array_direction(int a[],char k); 
    
    void main (void) 
    { 
            int n; 
            int z[10]; 
            char order; 
            int s; 
            printf("Please enter the  values of your array:\n"); 
    
    
    
            for(n=0;n<10;n++) 
                    { 
                            printf(" please enter %d:",n+1); 
                            scanf("%d",&z[n]); 
                    } 
    
            printf(" How do you want your array sorted\n"); 
            fflush(stdin); 
            printf("Pease enter A for ascendind order or D for descending order\n:"); 
            scanf("%c",&order);     
    
            array_direction(z, order); 
            
            printf(" The order is:\n"); 
                    
                    for(s=0;s<10;s++) 
                    {       
                    
                            printf("%3d ",z[s]); 
    
                    } 
    
    
    
                    
    
    } 
    
    void array_direction(int x[],char k) 
    { 
            
    
            int pass; 
            int t; 
            int hold; 
    
            if( k=='a'||k=='A') 
            { 
                    for (pass=0;pass<10; pass++) 
                    { 
                            for(t=0;t<10;t++) 
                            { 
    
                            
                            
                            if (x[t]>x[t+1]) 
                            { 
                                    hold=x[t]; 
                                    x[t]=x[t+1]; 
                                    x[t+1]=hold; 
                            } 
                    } 
    
            } 
    
    
    
    
    
            } 
    
    
    
            if(k=='d'||k=='D') 
            { 
    
                    for (pass=0;pass<10; pass++) 
            { 
                            for(t=0;t<10;t++) 
                            { 
                                    if(x[t]<x[t+1]) 
                                    { 
                                            hold=x[t]; 
                                            x[t]=x[t+1]; 
                                            x[t+1]=hold; 
                                    } 
                            } 
    
                    } 
            } 
    
    }
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    poreko-

    I can think of two ways you could use a do-while loop in your program.

    The first would be for error-checking on the user input.

    [Pseudocode]
    do
    print 'do you want ascending or descending order?'
    get input
    while
    input is not equal to a, A, d or D
    [\Pseudocode]

    or you could use them to refine your different loops, but that would take some refactoring in your array_direction method. I will have to read up on my bubble sort technique before I gave any more advice, though - it's been a bit since I've used it!

    (Anyone else more up-to-date on it than me and want to give an algorithm/help refactor? Is it easier to refactor, or is it easier how poreko has it?)

    Comment

    • willakawill
      Top Contributor
      • Oct 2006
      • 1646

      #3
      A couple of things. Firstly, congratulations you have a working program.

      I have changed a few minor things in your code to help you to look at coding in a more efficient way for the future. White space is free in c so there is no benefit in writing compact statements such as
      Code:
      x[t]=x[t+1];
      or using single characters for variable names. It works in terms of functionality but it is a nightmare when it comes to reading your own code for debugging or remembering what does what in six months time. You should always annotate your code with lots of comments. This will make your life easier and longer :)

      You could improve your code by removing the input validation from the sort algorithm and using a more generic term for the sort direction. You could also send the size of the array as a parameter to the sort algorithm which would allow you to use the sort in other projects without having to rewrite any of the code.

      Here are a few alterations as a start:
      Code:
      void array_direction(int ar[],char direction); 
      
      int main (void) 
      { 
          int n; 
          int z[10]; 
          char order; 
          int s; 
          printf("Please enter the 10 values to be sorted:\n");
      
          for(n=0;n<10;n++) 
          { 
              printf(" please enter %d:",n+1); 
              scanf("%d",&z[n]); 
          } 
      
          printf(" How do you want your array sorted\n"); 
          fflush(stdin); 
          printf("Pease enter A for ascending order or D for descending order\n:"); 
          scanf("%c",&order);     
      
          array_direction(z, order); 
          
          printf(" The order is:\n"); 
                  
          for(s=0;s<10;s++) 
          {
              printf("%3d, ",z[s]);
          }
          fflush(stdin);
          getchar();
          return 0;
      } 
      
      void array_direction(int ar[],char direction) 
      { 
          int outer; 
          int inner; 
          int hold; 
      
          //bubble sort
          //A is ascending, D is descending order
      
          if( direction == 'a' || direction == 'A') 
          { 
              for (outer = 0;outer < 10; outer++) 
              { 
                  for(inner = 0;inner < 10;inner++)
                  { 
                     if (ar[inner] > ar[inner+1]) 
                     { 
                        //swap
                        hold = ar[inner]; 
                        ar[inner] = ar[inner+1]; 
                        ar[inner+1] = hold; 
                     } 
                  }
             }
         }
      
      
          if(direction == 'd' || direction == 'D') 
          {
             for (outer = 0;outer < 10; outer++) 
            { 
                  for(inner = 0;inner < 10;inner++) 
                  { 
                      if(ar[inner] < ar[inner+1]) 
                      { 
                          //swap
                          hold = ar[inner]; 
                          ar[inner] = ar[inner+1]; 
                          ar[inner+1] = hold; 
                      } 
                  }
              } 
          }
      }

      Comment

      • poreko
        New Member
        • Nov 2006
        • 38

        #4
        thank you guys!! but i still need some help on error trapping

        Comment

        • FB2006 TEAM
          New Member
          • Feb 2007
          • 7

          #5
          If you still need suggestions I will be glad to help you, but don’t think that I am trying to be a SMART GUY!

          Suggestion I - always use constant instead of numbers. In this case you can only make changes in one plays instead of looking in all program to change each number.
          Suggestion II - use functions as much us possible, even if function will contain one line of code. And ALWAYS use names that describe what this function supposes to do.
          > If you wish to mark some section of code that makes something specific, put it into function.
          > If you repeat some code many times, put it into function.
          > If you don’t know how to solve particular problem, transform it into function and back to this problem later.
          > Try not to use variable in function declaration, only they types.
          Suggestion III - Grope particular types of variable in one grope (int with int…). The best way to prevent “hair tear” problems, it’s to set all variables to 0 in declaration section. And ones more use names that mean something, and not just: a, b, c, d, kuku, kiki, hruku... even if this name will contain two and more words!
          Suggestion IV - Try not repeat your code (for this you have functions). Because of this make right conditions that will “answer” on much as possible questions.
          Suggestion V - If you wish to make space (interval) use Tab instead of space. And try not to leave “white” spaces without reason.
          Suggestion VI - Don’t stick your code in one big sausage, make some space, as do Microsoft Word, Open Office, what ever…
          And more important:
          You should always annotate your code with lots of comments. This will make your life easier and longer
          I think it’s what I can suggestions you for a future. About the program, I hope it will help you!
          Code:
          # include <stdio.h>
          
          # define ARRAY_LENGTH 10 // Suggestion I
          
          // Suggestion II
          void F_SortArrayContents (int [], char);
          void F_SwapContentsOfTwoCells (int [], int);
          
          void main () 
          {
          	// Suggestion III
          	int index = 0;
          	int array [ARRAY_LENGTH] = {12, 45, 86, 2, 78, 11, 56, 10, 96, 25};
          	char sort_direction;
          	
          	printf("> How do you want your array sorted\n");
          	printf ("> Pease enter A for ascending order or D for descending order: ");
          	scanf ("%c", &sort_direction);
          	F_SortArrayContents (array, sort_direction);
          	printf("> The new order of array, is: ");
          	for(; index < ARRAY_LENGTH; index++)
          	{
          		printf ("%3d ", array [index]);
          	}
          } 
          
          void F_SortArrayContents (int array [], char direction) 
          {
          	int index_f = 0;				// This variable moves forward in array "index_forword"
          	int index_b = ARRAY_LENGTH - 1;	// This variable moves backward in array "index_backword"
          
          	// Suggestion IV
          	do
          	{ // Suggestion V
          		do 
          		{
          			if ((array [index_f] > array [index_f + 1]) && (direction == 'a' || direction == 'A')) // Suggestion VI
          			{ 
          				F_SwapContentsOfTwoCells (array, index_f);
          			}
          			else if ((array [index_f] < array [index_f + 1]) && (direction == 'd' || direction == 'D'))
          			{
          				F_SwapContentsOfTwoCells (array, index_f);
          			}
          		}
          		while (++index_f < index_b);
          		index_f = 0;
          	}
          	while (--index_b > 0);
          }
          
          void F_SwapContentsOfTwoCells (int array [], int index)
          {
          	int t_number = 0; // temporary_number
          
          	t_number = array [index]; 
          	array [index] = array [index + 1];
          	array [index + 1] = t_number; 
          }

          Comment

          • poreko
            New Member
            • Nov 2006
            • 38

            #6
            thank you !!

            Comment

            Working...