473,473 Members | 1,524 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Bubble sort using C / How do i use do while

38 New Member
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
5 11553
sicarie
4,677 Recognized Expert Moderator Specialist
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
willakawill
1,646 Top Contributor
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
poreko
38 New Member
thank you guys!! but i still need some help on error trapping
Feb 7 '07 #4
FB2006 TEAM
7 New Member
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
poreko
38 New Member
thank you !!
Feb 8 '07 #6

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

Similar topics

34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
4
by: Chris | last post by:
I have a bubble sort for a 2-dimensional array that sorts a string,number pair based on the number. The code for the sort is as follows: Private Sub SortArray(ByRef roundarray(,) As String)...
0
by: Slowjam | last post by:
How do I correct the program below to search all the anagrams in a given file. First, for each word, build another word (its key) with all its letters sorted. For instance, build "dorw" for...
5
kuchma2000
by: kuchma2000 | last post by:
Hi. I have a C program (see code below) which I need to urgrade. All I need to do is: a. Extract the temps for the City #1 and store in a one dimensional array, display the array. b. Sort the one...
12
by: midknight5 | last post by:
Hello everyone, I am familiar with a normal bubble sort when dealing with an array of number but I am unsure as how to implement a sort when I have an array that is filled with classes which hold...
5
by: Bubbs | last post by:
Hi, I have to sort customer names in a customer class i have created. Now, there is a method called getName() that basically returns the string name stored in an instance variable of the customer...
14
by: xtheendx | last post by:
I am writing a gradbook type program. It first allows the user to enter the number of students they want to enter. then allows them to enter the first name, last name, and grade of each student. The...
7
by: mahdiahmadirad | last post by:
Hi dears! I wrote a simple bubble sort algorithm. it works properly when we compare full arrays but i want to sort a 2d array according to a specific part of array. it has some problem to swapping...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.