Write a function that when invoked as bubble_string(s) causes the characters in the string s to be bubble-sorted. If s contains the string "xylophone", then the following statement should cause ehlnoopxy ... |
I'm writing a bubble sort program.
I use TCC (http://bellard.org/tcc/)?
I use long long variables in my program, because the input data is very large.
My problem is : When the number of input ... |
|
I am not an expert on how accurate this is, but ctime header file contains a bunch of methods and types that can be used to measure time between two events. On a more general note recording time as a method of evaluating a function is pretty unreliable. Because of processor time slicing and other factors your time may be wildly ... |
|
If you can write a function, say, printArray(), then you can call the function at the end (or beginning, but the end is probably closer to what you want) and have it print the array. If you'd rather not write a function, you can put in the code directly at the end of the loop. By the end, I mean the ... |
|
|
|
Thank You..... As I mentioned, this is the exact code I had in main and it worked fine. So, I'm not sure why I wouldn't get the same compile error in main. My data that I am trying to sort is an array of names that I am passing into the function. |
|
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 ... |
12. Bubblesort cboard.cprogramming.com#include #define ARRAY_SIZE 10 void sort(const int * input, int * output, const int size); int main(void) { int mixed[ARRAY_SIZE] = { 9,8,7,6,5,4,3,2,1,0 }; int fixed[ARRAY_SIZE] = { 9,8,7,6,5,4,3,2,1,0 }; sort(mixed,fixed,ARRAY_SIZE); } void sort(const int * input, int * output, const int size) { int i = 0; int temp; for (i=0;ioutput[i+1]) { temp = output[i]; output[i] ... |
13. bubble sort cboard.cprogramming.com |
@CommonTater: I consider using other people algorithms a valid way to right code. So, you spend all of your time reinventing the wheel. I hope you are not the type that says we need to throw out all the project past code just because you did not write it. I worked with a few college kids who said things like that ... |
Ok I updated the code again, and included a function that is part of the larger program. I tested everything you guys suggested and it worked beautifully...until i tested random numbers that don't include 1. I used this as initial array [3,5,2,4,6] and got this as output: Unsorted array |3| |5| |2| |4| |6| Pass number 1 rendered |3| |2| |4| ... |
I am working on a program for my intro to C programming class and I cant quite get the program to work. Here is the directions for the assignment... Purpose The purpose of the lab is to learn how to pass arrays to functions and how to sort integer arrays. Process Create a program that: Reads in a series of ages ... |
My call by reference bubble sort code isn't displaying the correct output The program compiles, however the output is incorrect Output: Code: Input size of array5 Input elements3 5 4 7 8 Enter 1 to sort in ascending order, Enter 2 to sort in descending order: 1 Data items in ascending order 0327673276716064169201606420320 logout [Process completed] Code: /* This program puts ... |
|
19. bubble sort cboard.cprogramming.com |
|
|
|
Code: #include #include #include #define SIZE 6 #define MAX 53 void printArray(int N[], int T); void fillArray(int N[], int T); int main() { srand(time(NULL)); int PICKED[] = {0,0,0,0,0,0}; fillArray(PICKED, SIZE); printArray(PICKED, SIZE); return 0; } void fillArray(int N[], int T) { for(int P=0; P |
I'm as noobish as you get, first time using C and my first time programming. This is the 3rd program I am attempting to understand and replicate. I understand that bubble sorting compares two values and swaps them if the larger value is in front. However, with the following code, I just don't see how it happens when I try to ... |
|
Code: #include #include void bubblesort(int a[],int n); int main() { int comp, pass, tmp, n; int a[]; pass = 1; while(comp <= n - pass) { if(a[comp-1] > a[comp]) { tmp = a[comp-1]; a[comp-1] = a[comp]; a[comp] = tmp; } comp = comp + 1; } pass = pass + 1; system("pause"); return 0; } is what i have ... |
27. bubble sort cboard.cprogramming.comvoid sort_array_by_month(void) { struct sStudent Temp; int i, n, swap; do { swap=0; for(i=0; i<3; ++i)//bubble sort { if(Algebra[i].birthday.month>Algebra[i+1].birthday.month) Temp.birthday.month=Algebra[i].birthday.month; // temp[Count]=Array[count+1] Temp.birthday.day=Algebra[i].birthday.day; Temp.birthday.year=Algebra[i].birthday.year; for(n=0; Algebra[i].first_name[n]!=0; ++n) { Temp.first_name[n]=Algebra[i].first_name[n]; Temp.first_name[n]='\0'; } for(n=0; Algebra[i].last_name[n]!=0; ++n) { Temp.last_name[n]=Algebra[i].last_name[n]; Temp.last_name[n]='\0'; } Algebra[i].birthday.day=Algebra[i+1].birthday.day; //Array[Count]=Array[Count+1] Algebra[i].birthday.month=Algebra[i+1].birthday.month; Algebra[i].birthday.year=Algebra[i+1].birthday.year; for(n=0; Algebra[i].first_name[n]!=0; ++n) { Algebra[i].first_name[n]=Algebra[i+1].first_name[n]; Algebra[i].first_name[n]='\0'; } for(n=0; Algebra[i].last_name[n]!=0; ++n) { Algebra[i].last_name[n]=Algebra[i+1].last_name[n]; Algebra[i].last_name[n]='\0'; } Algebra[i+1].birthday.month=Temp.birthday.month; //Array[Count+1]=Temp Algebra[i+1].birthday.day=Temp.birthday.day; Algebra[i+1].birthday.year=Temp.birthday.year; for(n=0; ... |
struct info{char name[30]; int account_num; float balance; }; struct info people[25]; int x,a,y,temp=0; //loop to collect info*/ for(x = 1; x <= 3; ++x) { printf("Enter Name: "); gets(people[x].name); printf("Enter Account Number: "); scanf("%i",&people[x].account_num); printf("Enter Balance: "); scanf("%f",&people[x].balance); printf("\n"); } //bubble sort for (a=1; apeople[x+1].account_num) { temp = people[x+1].account_num; people[x+1].account_num = people[x].account_num; people[x].account_num = ... |
bubble sort not working... wats d prob? here's my code... the colored portion is the bubble sort... i just commented the bubble sort... if i remove the bubble sort... it works fine, except for its order... Code: #include #include typedef struct student_profile{ long long int stud_num; char name[50]; char bday[50]; char course[50]; } STUDENT; void login(char username[], char password[]); ... |
#include main() { /*Variables*/ /*-------------------------------------------------------------------------------*/ struct info{char name[30]; int account_num[30]; float balance[30]; }; struct info people[25]; int x,a,temp; /*------------------------------------------------------------------------------*/ /*Heading*/ /*-------------------------------------------------------------------------------* //*Loop to collect info*/ for(x = 1; x <= 2; ++x) { printf("Enter Name: "); scanf("%s",people[x].name); printf("Enter Account Number: "); scanf("%i",people[x].account_num); printf("Enter Balance: "); scanf("%f",&people[x].balance); } for (a=1; apeople[x+1].account_num) { temp = ... |
This is the version of bubblesort that I use. It is *very* similar to your version, but does sort all the way through the items. Code: for(i = 0; i < MaxItems - 1; i++) { for(j = i + 1; j < MaxItems; j++) { if(Array[i] > Array[j]) { temp = Array[i]; Array[i] = Array[j]; Array[j] = temp; } } ... |
Im not sure why this bubble sort code isn't working at all, could someone give me a hint ? Help would be much appreciated. Thanks. Code: #include #include #include #include int array[2]; int temp[2][2]; int checkint(char s[]) //Checks if string is purely numerical { int length = strlen(s); // Define string length for (int i = 0; ... |
Hi All, in the below code, the author uses what he labels as a code to re-order the elements of a bubble. Some of my colleagues believe that it is not truly a bubble process. Is this true? They couldn't recall what it is really called, what the author is doing. Anyone know or disagree? Cheers. Code: #include #include ... |
Hiya im writing a program that reads 20 floats into an array then sorts them using bubble sort and prints them out in assending order. But I am getting an error that says variable 'i' is undecared. ( I have marked the line with the error ). I delcared 'i' before the program inside main so I do not understand why ... |
35. bubble sort cboard.cprogramming.com |
#include void swap(int*, int *); int main(void) { int a[8]; int i,j; for(i = 0; i < 8; ++i) if(scanf("%d",&a[i])){ printf("Unordered data: %d\n", a[i]);} for(i = 0; i < 8; ++i) for(j = i + 1 ; j < 8 ; j++) if(a[i] > a[j]) swap(&a[i], &a[j]); for(i = 0; i < 8; ++i) printf("after pass 1: %d\n",a[i]); getchar(); } void ... |
Hi, I just recently joined and i need some help with one of my assignments. please can you have a look and try to help thank you. task: Write an efficient bubble sort function optimised so as not to check data that is guaranteed to be in the correct position (i.e. each pass is shorter than the previous one). It should ... |
|
Hey guys, I have been trying to learn c, i have this book which has an example of what is labled as a bubble sort. I am having a little trouble understand a few parts, if anyone can help clear it up it would be greatly appreciated. Ok, I understand the first part with rand, it is generating 10 random numbers ... |
40. Bubble sort cboard.cprogramming.comBubble sort Hi! I'm attempting to create a bubble sort. By the way: a bubble sort is essentially 2 linear sorts at the same time, right? So far I've: 1) Created a list of random numbers in an array 2) Performed a linear sort from the beginning forward and the ending backward (at the same time) 3) Set it up in ... |
41. Bubble sort cboard.cprogramming.comHi I have recently started C and i am trying to get my program to sort 10 numbers in an array i have it so that it will sort them in ascending order but i dont know how to do it so it will display at the same time descending order, my code is as follows, Code: #include #define NUM ... |
#include void bubble(int a[],int n); void swap(int *p, int *q); int main(void) { int a[]={7,6,5,4,3,2,1}; int i; for (i = 0; i < 7; i++) printf("%d ",a[i]); printf("\n"); bubble(a,8); for (i = 0; i < 7; i++) printf("%d ",a[i]); printf("\n"); } void bubble(int a[],int n) { int i,j; for(i=0; ia[j]) swap(&a[j-1],&a[j]); } void swap(int *p, int *q) { int ... |
I am having a small problem with my bubble sort. It will read the file print the current file then maybe or or maybe not does not go into my bubblesort funcion then closes. But i need it to print back out the sorted data. Any ideas and or suggestions please feel free to post. Code is below. Code: #include ... |
44. bubble sort cboard.cprogramming.com |
Okay, I'm back again. From what I understand this bubble sort, should sort the entire struct in ascending order according to the price member... But it just prints out the struct in its original form for me. Probably just a stupid mistake, but can anyone point it out? Code: #include #include #define NUM 5 struct inv { char partID[6]; ... |
46. Bubble Sort cboard.cprogramming.com |
I think ive got the idea now, and also how would i trace this program? like show each step of the way until it ends? How can i change the code below to output a trace. Code: #include #define SIZE 10 int main() { int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 ... |
Hello everyone, at first I didn't care about the speed of my bubble sort function - a working program was/is enough ... but today I found out that some newbie was able to write a faster bubble sort function than me. So, I decided to tweak the sourcecode a little bit. I tried to reach the performance of the bubble sort ... |
|
50. bubble sort cboard.cprogramming.comfor(outer = 0; outer < (numList-1); outer++) { swap = 0; // Initialize after each pass for(inner = outer; inner < numList; inner++) { if(strcmp(info[outer].lname,info[inner].lname) > 0) { temp = info[outer]; info[outer] = info[inner]; info[inner] = temp; swap = 1; } } if(!swap) /* if no swap end sort */ {break;} } |
51. Bubble Sort cboard.cprogramming.com |
Help with Bi-Directional Bubble Sort in C I need to implement a bi-directional bubble sort algo that will sort 12 randomly generated integers of an array. I am able to generate and print the 12 random integers, as well as sort them in their final ascending order. However, the program also needs to print each pass of the array as it ... |
|
the bubble sort doesn't work help thanks to all that have looked at this but i still can't get the bubble sort to work, please help or i am F***ed, if i can see it coded then i will be able to see were i was going wrong. heres the code, I'm a beginner by the way(in case you cant tell. ... |
#include int main() { int /* PersonID,Wage,Sex,Age, */ n, i; int a[10][4]; printf("please enter the amount of people you will use \n"); scanf("%d",&n); printf("\n=%d\n\n", n); for (i=0; i |
56. Bubble Sort cboard.cprogramming.comI will be using the following sequence of numbers... 3 6 8 2 9 0 1 4 7 5 The basic action we're going to is compare two numbers, and if they're out of order, we'll switch them. We're going to do this to every pair of numbers... here's basically how the process goes Code: 3 6 8 2 9 0 ... |
|
Hello People, I'm a beginner in c. And I'm trying to convert a normal bubblesort into an "Enhanced bubblesort". The bubblesort code i've got now is: Code: #include #include #include void bubbleSort(int *, int); void swap(int *, int , int); int main(void) { int iArray[] = {10, 9, 8 , 7, 6, 5, 4, 3, 2, 1, 0}; ... |
59. Bubble sort forums.devshed.comYou can fix that by testing scanf's return. It returns the number of items successfully converted and stored. If that number is not what you expect, chastise your user and loop back and try again. If your user can't get it right in a few tries, them dump his azz on the ground and exit. You could have discovered this had ... |
i try to write a bubble sort program,but it does not work as i expected.i try to sort the array elements from the smallest number to the largest number.when the program executes,it just place the smallest number in the first index,and other numbers are not sorted in order. please tell me what mistake i made,and i also want some suggestions to ... |
|
google i'm sure you can search for the algorithm on google and figure it out. you'll probably even find a page that does it for you. all it is doing is comparing ints, so you won't have to use any namespace such as std for any stl functions or anything. edit....bored i got this from http://www.cs.princeton.edu/~ah/alg...BubbleSort.html Bubble Sort This is probably ... |
|
64. bubble sort forums.devshed.comI am getting into simple algorithms. I am starting with the bubble sort which was suggested by a friend. He ended up giving me the code to look at. Here is the code and the questions I have: #include int main() { int array[] = {22, 23, 21, 20}; int n = 4; for ( int i = 0; i ... |
#include #define MAXIMUM 10 void sort(int a[], int n) { int j, jj; int t; for(j=0; j<(n-1); j++) /* outer loops and counts passes */ { for(jj=0; jj< (n-(1-j)); jj++) /* inner loop and compares adjenct items */ { if (a[jj] > a[jj+1]) /* out of order */ { t= a[jj]; a[jj] = a[jj+1]; a[jj + 1] = t; } ... |
/********************************************* * Demonstration program of Bubble sorting * * (about n*n comparisons used). * * ------------------------------------------ * * Reference: "A book on C By Al Kelley and * * Ira Pohl, The Benjamin/Cummings Publishing * * Company, Inc, 1984" [BIBLI 09]. * * * * C++ version by J-P Moreau. * * ------------------------------------------ * * SAMPLE RUN: * * * * ... |
|
#include #include #include void bubble_sort(int [],int); int main() { int a[0]; int n=10; int i; const int m=10000; srand(time(NULL)); for(i=0;ia[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } printf("\nAfter pass : %d",i); for(k=0;k |
|
|