Urgent help need Bubble sort

In summary, the conversation discussed a task for a PCB manufacturing company to create a program using ANSI C to convert drill data and minimize drill bed movement. The program should include a numerical bubble sort and a user-friendly menu system. The conversation also mentioned the use of functions, structured commenting, and limitations/enhancements of the program. Suggestions were made to input data from a file and use arrays of structures instead of just x values.
  • #1
Apple4ever
8
0
Urgent help need ! Bubble sort !

hi all, I was asked to built a C programme using bubble sort. here is the Question.
TASK



A PCB manufacturing company requires a software program to convert a series of a table of drill data to minimise the movement of the drill bed to speed up production i.e.to place all the holes near each other row by row. This requires sorting out a series of X coordinates points in ascending/descending order.
You are required to write an ANSI C visual studio program to provide a numerical bubble sort, to a set of data. The user should be able to select from a menu system the type of bubble sort (ascending or descending), data input and final data display i.e. four options. The program should be user friendly and coded in best programming style: no global data, good use of functions and structured commenting. You are required to submit a word document report comprising of the final source code, sample test screen dumps and indications of limitations/enhancements of your program. Below is shown an example of basic pseudo code to help with the bubble sort approach to be used.
Pseudo code

Main
Define storage array input and output
Define menu
Option 1: Call array fill for input
Option 2: Call descending bubble sort on input for X data only
Option 3: Call ascending bubble sort on input for X data only
Option 4: Call display array output
Array fill
Input: Array size, Input arrays
Output: Array full of real numbers i.e. floats
Function: Single dimension loop using stdio to input data
Sort
Input: Array size, Input array, Output array, type of sort a or d
Output: Output array with sorted data
Function: Principle used bubble sort
Compare current value in array to array+1 element
Sort A: If value is greater than value+1 then swap over elements else carry on to next element
Sort D: If value is less than value+1 then swap over elements else carry on to next element
Repeat till you have compared all elements and no swaps have occurred and then return back the final output array

Can someone please check if the undermentioned answer is correct for the above question and can you simplify more if it is possible; any new answers are wellcome !

Work Done So far;

#include<stdio.h>
#define NUM 4
void array_direction(int ar[],char direction);
int main (void)
{
int n;
int x[NUM];
char order;
int y;

printf("This is the assignment 2 of Softwares for engineers \n");

printf("Welcome to the PCB Company :\n");
printf("Please enter the values of x coordinate to be sorted:\n");
for(n=0;n< NUM;n++)
{
printf(" please enter X Value %d:",n+1);
scanf("%3d",&x[n]);

}
printf(" How do you want your values of X coordinate sorted\n"); fflush(stdin);
printf("Pease enter A for ascending order or D for descending order\n:");
scanf("%c",&order);
array_direction(x, order);
printf(" The order is:\n");
for(y=0;y< NUM;y++)
{
printf("%3d, ",x[y]);
}
fflush(stdin);
getchar();
return 0;
}
void array_direction(int ar[],char direction)
{
int outer;
int inner;
int hold;
/*by using bubble Sort*/

/*For Ascending Order*/
if( direction == 'a' || direction == 'A')
{
for (outer = 0;outer < NUM; outer++)
{
for(inner = 0;inner < NUM;inner++)
{
if (ar[inner] > ar[inner+1])
{

//swap
hold = ar[inner];
ar[inner] = ar[inner+1];
ar[inner+1] = hold;
}
}
}
}







/* For Descending Order */
if(direction == 'd' || direction == 'D')
{
for (outer = 0;outer < NUM; outer++)
{
for(inner = 0;inner < NUM;inner++)
{
if(ar[inner] < ar[inner+1])
{
//swap
hold = ar[inner];
ar[inner] = ar[inner+1];
ar[inner+1] = hold;
}
}
}
}
}
 
Technology news on Phys.org
  • #2


Hello,

Here is a link to one of the older C books from Numerical Recipes. Chapter 8 contains sorting algorithms. Check it out. If it is to complicated I can help you with some other code for a bubble sort.

http://www.nrbook.com/a/bookcpdf.php

Thanks
 
  • #3


Thanks 4 da reply. i am new to this programming and i am not a Computer Student.

Can someone let me know the indications of limitations/enhancements of the above program...?

atleast 1 is enough thanks !
 
  • #4


You might want to input the data from a file instead of user input. It will be easier to debug the program this way. Also, it's likely that the array will contain both x and y values. You could make an array of structures (since these can be assigned directly instead of having to use memcpy to move a pair of numbers).
 

1. What is a bubble sort algorithm?

A bubble sort algorithm is a simple sorting algorithm that repeatedly compares adjacent elements in a list and swaps them if they are in the wrong order. This process is repeated until the entire list is sorted.

2. How does a bubble sort work?

A bubble sort works by comparing two adjacent elements in a list and swapping them if they are in the wrong order. This process is repeated until the entire list is sorted.

3. Is a bubble sort efficient?

No, a bubble sort is not considered to be an efficient sorting algorithm. It has a time complexity of O(n^2) which means the time it takes to sort a list increases exponentially as the size of the list increases.

4. When should I use a bubble sort?

A bubble sort is best used for sorting small lists or lists that are almost sorted. It is not recommended for use on large or complex lists as it is not efficient.

5. Are there any variations of a bubble sort?

Yes, there are variations of a bubble sort such as the optimized bubble sort and the cocktail shaker sort. These variations aim to improve the efficiency of the algorithm by reducing the number of comparisons and swaps needed.

Similar threads

  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
943
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top