By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,454 Members | 1,803 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,454 IT Pros & Developers. It's quick & easy.

Bubble sort using C / How do i use do while

P: 38
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:

Expand|Select|Wrap|Line Numbers
  1. # include<stdio.h> 
  2.  
  3. void array_direction(int a[],char k); 
  4.  
  5. void main (void) 
  6.         int n; 
  7.         int z[10]; 
  8.         char order; 
  9.         int s; 
  10.         printf("Please enter the  values of your array:\n"); 
  11.  
  12.  
  13.  
  14.         for(n=0;n<10;n++) 
  15.                 { 
  16.                         printf(" please enter %d:",n+1); 
  17.                         scanf("%d",&z[n]); 
  18.                 } 
  19.  
  20.         printf(" How do you want your array sorted\n"); 
  21.         fflush(stdin); 
  22.         printf("Pease enter A for ascendind order or D for descending order\n:"); 
  23.         scanf("%c",&order);     
  24.  
  25.         array_direction(z, order); 
  26.  
  27.         printf(" The order is:\n"); 
  28.  
  29.                 for(s=0;s<10;s++) 
  30.                 {       
  31.  
  32.                         printf("%3d ",z[s]); 
  33.  
  34.                 } 
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41. void array_direction(int x[],char k) 
  42.  
  43.  
  44.         int pass; 
  45.         int t; 
  46.         int hold; 
  47.  
  48.         if( k=='a'||k=='A') 
  49.         { 
  50.                 for (pass=0;pass<10; pass++) 
  51.                 { 
  52.                         for(t=0;t<10;t++) 
  53.                         { 
  54.  
  55.  
  56.  
  57.                         if (x[t]>x[t+1]) 
  58.                         { 
  59.                                 hold=x[t]; 
  60.                                 x[t]=x[t+1]; 
  61.                                 x[t+1]=hold; 
  62.                         } 
  63.                 } 
  64.  
  65.         } 
  66.  
  67.  
  68.  
  69.  
  70.  
  71.         } 
  72.  
  73.  
  74.  
  75.         if(k=='d'||k=='D') 
  76.         { 
  77.  
  78.                 for (pass=0;pass<10; pass++) 
  79.         { 
  80.                         for(t=0;t<10;t++) 
  81.                         { 
  82.                                 if(x[t]<x[t+1]) 
  83.                                 { 
  84.                                         hold=x[t]; 
  85.                                         x[t]=x[t+1]; 
  86.                                         x[t+1]=hold; 
  87.                                 } 
  88.                         } 
  89.  
  90.                 } 
  91.         } 
  92.  
Feb 7 '07 #1
Share this Question
Share on Google+
5 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
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?)
Feb 7 '07 #2

100+
P: 1,646
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
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. void array_direction(int ar[],char direction); 
  2.  
  3. int main (void) 
  4.     int n; 
  5.     int z[10]; 
  6.     char order; 
  7.     int s; 
  8.     printf("Please enter the 10 values to be sorted:\n");
  9.  
  10.     for(n=0;n<10;n++) 
  11.     { 
  12.         printf(" please enter %d:",n+1); 
  13.         scanf("%d",&z[n]); 
  14.     } 
  15.  
  16.     printf(" How do you want your array sorted\n"); 
  17.     fflush(stdin); 
  18.     printf("Pease enter A for ascending order or D for descending order\n:"); 
  19.     scanf("%c",&order);     
  20.  
  21.     array_direction(z, order); 
  22.  
  23.     printf(" The order is:\n"); 
  24.  
  25.     for(s=0;s<10;s++) 
  26.     {
  27.         printf("%3d, ",z[s]);
  28.     }
  29.     fflush(stdin);
  30.     getchar();
  31.     return 0;
  32.  
  33. void array_direction(int ar[],char direction) 
  34.     int outer; 
  35.     int inner; 
  36.     int hold; 
  37.  
  38.     //bubble sort
  39.     //A is ascending, D is descending order
  40.  
  41.     if( direction == 'a' || direction == 'A') 
  42.     { 
  43.         for (outer = 0;outer < 10; outer++) 
  44.         { 
  45.             for(inner = 0;inner < 10;inner++)
  46.             { 
  47.                if (ar[inner] > ar[inner+1]) 
  48.                { 
  49.                   //swap
  50.                   hold = ar[inner]; 
  51.                   ar[inner] = ar[inner+1]; 
  52.                   ar[inner+1] = hold; 
  53.                } 
  54.             }
  55.        }
  56.    }
  57.  
  58.  
  59.     if(direction == 'd' || direction == 'D') 
  60.     {
  61.        for (outer = 0;outer < 10; outer++) 
  62.       { 
  63.             for(inner = 0;inner < 10;inner++) 
  64.             { 
  65.                 if(ar[inner] < ar[inner+1]) 
  66.                 { 
  67.                     //swap
  68.                     hold = ar[inner]; 
  69.                     ar[inner] = ar[inner+1]; 
  70.                     ar[inner+1] = hold; 
  71.                 } 
  72.             }
  73.         } 
  74.     }
Feb 7 '07 #3

P: 38
thank you guys!! but i still need some help on error trapping
Feb 7 '07 #4

P: 7
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!
Expand|Select|Wrap|Line Numbers
  1. # include <stdio.h>
  2.  
  3. # define ARRAY_LENGTH 10 // Suggestion I
  4.  
  5. // Suggestion II
  6. void F_SortArrayContents (int [], char);
  7. void F_SwapContentsOfTwoCells (int [], int);
  8.  
  9. void main () 
  10. {
  11.     // Suggestion III
  12.     int index = 0;
  13.     int array [ARRAY_LENGTH] = {12, 45, 86, 2, 78, 11, 56, 10, 96, 25};
  14.     char sort_direction;
  15.  
  16.     printf("> How do you want your array sorted\n");
  17.     printf ("> Pease enter A for ascending order or D for descending order: ");
  18.     scanf ("%c", &sort_direction);
  19.     F_SortArrayContents (array, sort_direction);
  20.     printf("> The new order of array, is: ");
  21.     for(; index < ARRAY_LENGTH; index++)
  22.     {
  23.         printf ("%3d ", array [index]);
  24.     }
  25.  
  26. void F_SortArrayContents (int array [], char direction) 
  27. {
  28.     int index_f = 0;                // This variable moves forward in array "index_forword"
  29.     int index_b = ARRAY_LENGTH - 1;    // This variable moves backward in array "index_backword"
  30.  
  31.     // Suggestion IV
  32.     do
  33.     { // Suggestion V
  34.         do 
  35.         {
  36.             if ((array [index_f] > array [index_f + 1]) && (direction == 'a' || direction == 'A')) // Suggestion VI
  37.             { 
  38.                 F_SwapContentsOfTwoCells (array, index_f);
  39.             }
  40.             else if ((array [index_f] < array [index_f + 1]) && (direction == 'd' || direction == 'D'))
  41.             {
  42.                 F_SwapContentsOfTwoCells (array, index_f);
  43.             }
  44.         }
  45.         while (++index_f < index_b);
  46.         index_f = 0;
  47.     }
  48.     while (--index_b > 0);
  49. }
  50.  
  51. void F_SwapContentsOfTwoCells (int array [], int index)
  52. {
  53.     int t_number = 0; // temporary_number
  54.  
  55.     t_number = array [index]; 
  56.     array [index] = array [index + 1];
  57.     array [index + 1] = t_number; 
  58. }
  59.  
Feb 7 '07 #5

P: 38
thank you !!
Feb 8 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.