| | |
Help Me With Sorting Algo
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2008
Posts: 22
Reputation:
Solved Threads: 0
Pls Help me, i am nearing the deadline July 30 of this case study and the code is almost finshed it does sort the input elements the way my algo logic does however i am not able to display the correct number of passes to show the law of the algo, it shoud be:
number of passes = total number of elements / 2
if total number of elements / 2 = .5 round off to left subgroup
the main logic of the algo is to divide it into two groups and sort it in either ascending or descending order, for example:
total number of elements = 6
order: descending
element 1: 5
element 2: 4
element 3: 6
element 4: 8
element 5: 2
element 6: 1
Original: 5 4 6 | 8 2 1
| = MID
Pass 1: the underlined elements are the numbers involved to be sorted, they shoud be swapped if ever the right first element of the right subgroup (in this case 1) is greater than the left subgroup (in this case 5)
5 4 6 | 8 2 1
Pass 3: then increase the number of elements to be sorted on both sides and repeat the seapping process as long as it satisfies the condition.
8 6 5 | 4 2 1
The problem is it didn't print out Pass 2: which should be 5 4 6 | 8 2 1
Here is the code:
Pls Help me T___T
number of passes = total number of elements / 2
if total number of elements / 2 = .5 round off to left subgroup
the main logic of the algo is to divide it into two groups and sort it in either ascending or descending order, for example:
total number of elements = 6
order: descending
element 1: 5
element 2: 4
element 3: 6
element 4: 8
element 5: 2
element 6: 1
Original: 5 4 6 | 8 2 1
| = MID
Pass 1: the underlined elements are the numbers involved to be sorted, they shoud be swapped if ever the right first element of the right subgroup (in this case 1) is greater than the left subgroup (in this case 5)
5 4 6 | 8 2 1
Pass 3: then increase the number of elements to be sorted on both sides and repeat the seapping process as long as it satisfies the condition.
8 6 5 | 4 2 1
The problem is it didn't print out Pass 2: which should be 5 4 6 | 8 2 1
Here is the code:
c Syntax (Toggle Plain Text)
#include"stdio.h" #include"stdlib.h" void read_list(int element[], int total,char choice) { int ctr; if (choice == 'a' || choice == 'A') { for(ctr=0;ctr<total;ctr++) { printf("\n Enter Element [%d] ::" ,ctr); scanf("%s", &element[ctr]); } } if (choice == 'b' || choice == 'B') { for(ctr=0;ctr<total;ctr++) { printf("\n Enter Element [%d] ::" ,ctr); scanf("%d", &element[ctr]); } } } void print_list(int element[], int total,char choice) { int ctr; if (choice == 'a' || choice == 'A') { for(ctr=0;ctr<total;ctr++) printf(" %c", element[ctr]); } if (choice == 'b' || choice == 'B') { for(ctr=0;ctr<total;ctr++) printf(" %d", element[ctr]); } } void insert_sort(int element[], int total,char choice) { int pt, mid, left, right, ctr, ctr2, ctr3, temp; mid = total/2; pt = 0; ctr3 = 0; while (pt++ < mid) { for(ctr=total-1, ctr2=0; ctr>=mid && ctr2 <mid; ctr--, ctr2++) { if (element[ctr2] < element[ctr]) { temp = element[ctr]; element[ctr] = element[ctr2]; element[ctr2] = temp; printf("\n Go Pass %d :: ", ctr2); print_list(element,total,choice); } if ((element[ctr2] >= element[ctr]) && ctr3 == 0) { ctr3 = 1; printf("\n To Pass %d :: ", ctr2); print_list(element,total,choice); } for (ctr3=0; ctr3<mid; ctr3++) { for (left=0; left<mid; left++) { if (element < element [left+1]) { temp = element[left+1]; element[left+1] = element; element = temp; } } for (right=total-1; right>=mid; right--) { if (element[ctr-1] < element[ctr]) { temp = element[ctr]; element[ctr] = element[ctr-1]; element[ctr-1] = temp; } } } } } pt++; printf("\n Pass %d :: ", ctr2); print_list(element,total,choice); } void main() { int element[20], total; char choice; clrscr(); printf("Choose: A - Alphanumeric B - Numeric :: "); scanf("%c", &choice); printf("\n Enter Array Length :: "); scanf("%d", &total); read_list(element,total,choice); printf("\n The Array Elements are as follows :: "); print_list(element,total,choice); insert_sort(element,total,choice); printf("\n\n Final Answer :: "); print_list(element,total,choice); getch(); }
Pls Help me T___T
•
•
Join Date: Jul 2008
Posts: 22
Reputation:
Solved Threads: 0
We all know that creating your own algorithm should have a law, but is it possible that the number of passes it makes may be lesser or more than the average performance?
example:
average case: number of passes = total elements / 2
best case: number of passes < (total element / 2)
worst case: number of passes > (total element /2) but not more than total element
Pls help me, i would like to hear your comments about this one.
example:
average case: number of passes = total elements / 2
best case: number of passes < (total element / 2)
worst case: number of passes > (total element /2) but not more than total element
Pls help me, i would like to hear your comments about this one.
•
•
Join Date: Jul 2008
Posts: 22
Reputation:
Solved Threads: 0
Hee is the re-written code @ Ancient Dragon,
c Syntax (Toggle Plain Text)
#include"stdio.h" #include"stdlib.h" void read_list(int element[], int total,char choice) { int ctr; if (choice == 'a' || choice == 'A') { for(ctr=0;ctr<total;ctr++) { printf("\n Enter Element [%d] ::" ,ctr); scanf("%s", &element[ctr]); } } if (choice == 'b' || choice == 'B') { for(ctr=0;ctr<total;ctr++) { printf("\n Enter Element [%d] ::" ,ctr); scanf("%d", &element[ctr]); } } } void print_list(int element[], int total,char choice) { int ctr; if (choice == 'a' || choice == 'A') { for(ctr=0;ctr<total;ctr++) printf(" %c", element[ctr]); } if (choice == 'b' || choice == 'B') { for(ctr=0;ctr<total;ctr++) printf(" %d", element[ctr]); } } void insert_sort(int element[], int total,char choice) { int pt, mid, left, right, ctr, ctr2, ctr3, temp; mid = total/2; pt = 0; while (pt++ < mid) { for(ctr=total-1, ctr2=0; ctr>=mid && ctr2 <mid; ctr--, ctr2++) { if (element[ctr2] > element[ctr]) { temp = element[ctr]; element[ctr] = element[ctr2]; element[ctr2] = temp; printf("\n Pass %d :: ", ctr2); print_list(element,total,choice); } /* printf("\n Pass %d :: ", ctr2); print_list(element,total,choice); */ for (ctr3=0; ctr3<mid; ctr3++) { for (left=0; left<mid; left++) { if (element > element [left+1]) { temp = element[left+1]; element[left+1] = element; element = temp; } } for (right=total-1; right>=mid; right--) { if (element[ctr-1] > element[ctr]) { temp = element[ctr]; element[ctr] = element[ctr-1]; element[ctr-1] = temp; } } } } } pt++; printf("\n Pass %d :: ", ctr2); print_list(element,total,choice); } void main() { int element[20], total; char choice; clrscr(); printf("Choose: A - Alphanumeric B - Numeric :: "); scanf("%c", &choice); printf("\n Enter Array Length :: "); scanf("%d", &total); read_list(element,total,choice); printf("\n The Array Elements are as follows :: "); print_list(element,total,choice); insert_sort(element,total,choice); printf("\n\n Final Answer :: "); print_list(element,total,choice); getch(); }
GCC doesn't like your code.
GCC doesn't have clrscr() or getch() -- they're non-standard functions, after all -- but if your compiler does you can ignore those errors.
main() should really return int instead of void, though.
And the other warnings I think are important.
Should be %d, no?
[edit] Ah, I see that you really do want to store a string. Well, you can't do that when you have an array of ints. I suggest either using a struct/union with int and char[] members, or two separate arrays. [/edit]
I think perhaps you wanted this:
[edit] With those modifications, numeric sorting works as far as I can tell. Though the code could be a lot better -- with something like this, for example:
Alphanumeric sorting doesn't work, of course, because you can't read a string into a number. [/edit]
[edit=2] In case you're interested, here's my modified code. Sorry about the length, everyone.
[/edit]
C Syntax (Toggle Plain Text)
sort.c: In function ‘read_list’: sort.c:13: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int *’ sort.c: In function ‘insert_sort’: sort.c:75: warning: comparison between pointer and integer sort.c:78: warning: assignment makes integer from pointer without a cast sort.c:79: warning: assignment makes pointer from integer without a cast sort.c: At top level: sort.c:102: warning: return type of ‘main’ is not ‘int’ sort.c: In function ‘main’: sort.c:106: warning: implicit declaration of function ‘clrscr’ sort.c:124: warning: implicit declaration of function ‘getch’ /tmp/ccNDVhKN.o: In function `main': sort.c:(.text+0x3d1): undefined reference to `clrscr' sort.c:(.text+0x489): undefined reference to `getch' collect2: ld returned 1 exit status
main() should really return int instead of void, though.
And the other warnings I think are important.
printf("\n Enter Element [%d] ::" ,ctr);
scanf("%s", &element[ctr]);[edit] Ah, I see that you really do want to store a string. Well, you can't do that when you have an array of ints. I suggest either using a struct/union with int and char[] members, or two separate arrays. [/edit]
C Syntax (Toggle Plain Text)
if (element > element [left+1]) /* ... */ element[left+1] = element; element = temp;
C Syntax (Toggle Plain Text)
if (element > element [left+1]) /* ... */ element[left+1] = element; element = temp;
[edit] With those modifications, numeric sorting works as far as I can tell. Though the code could be a lot better -- with something like this, for example:
c Syntax (Toggle Plain Text)
void order(int *a, int *b) { if(*a > *b) { int temp = *a; *a = *b; *b = temp; } }
[edit=2] In case you're interested, here's my modified code. Sorry about the length, everyone.
c Syntax (Toggle Plain Text)
#include"stdio.h" #include"stdlib.h" void read_list(int element[], int total,char choice) { int ctr; if (choice == 'a' || choice == 'A') { for(ctr=0;ctr<total;ctr++) { printf("\n Enter Element [%d] ::" ,ctr); scanf("%d", &element[ctr]); } } if (choice == 'b' || choice == 'B') { for(ctr=0;ctr<total;ctr++) { printf("\n Enter Element [%d] ::" ,ctr); scanf("%d", &element[ctr]); } } } void print_list(int element[], int total,char choice) { int ctr; if (choice == 'a' || choice == 'A') { for(ctr=0;ctr<total;ctr++) printf(" %c", element[ctr]); } if (choice == 'b' || choice == 'B') { for(ctr=0;ctr<total;ctr++) printf(" %d", element[ctr]); } } void insert_sort(int element[], int total,char choice) { int pt, mid, left, right, ctr, ctr2, ctr3, temp; mid = total/2; pt = 0; while (pt++ < mid) { for(ctr=total-1, ctr2=0; ctr>=mid && ctr2 <mid; ctr--, ctr2++) { if (element[ctr2] > element[ctr]) { temp = element[ctr]; element[ctr] = element[ctr2]; element[ctr2] = temp; printf("\n Pass %d :: ", ctr2); print_list(element,total,choice); } /* printf("\n Pass %d :: ", ctr2); print_list(element,total,choice); */ for (ctr3=0; ctr3<mid; ctr3++) { for (left=0; left<mid; left++) { if (element > element [left+1]) { temp = element[left+1]; element[left+1] = element; element = temp; } } for (right=total-1; right>=mid; right--) { if (element[ctr-1] > element[ctr]) { temp = element[ctr]; element[ctr] = element[ctr-1]; element[ctr-1] = temp; } } } } } pt++; printf("\n Pass %d :: ", ctr2); print_list(element,total,choice); } int main() { int element[20], total; char choice; printf("Choose: A - Alphanumeric B - Numeric :: "); scanf("%c", &choice); printf("\n Enter Array Length :: "); scanf("%d", &total); read_list(element,total,choice); printf("\n The Array Elements are as follows :: "); print_list(element,total,choice); insert_sort(element,total,choice); printf("\n\n Final Answer :: "); print_list(element,total,choice); return 0; }
Last edited by dwks; Jul 16th, 2008 at 4:14 pm.
dwk
Seek and ye shall find.
"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.
"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison
"The only real mistake is the one from which we learn nothing."
-- John Powell
Seek and ye shall find.
"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.
"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison
"The only real mistake is the one from which we learn nothing."
-- John Powell
That last post was getting crowded so I thought I'd start another one -- especially since it's a higher-level one.
Since you're trying to handle both numbers and strings, why not have two separate functions: one to read an array of numbers, and the other an array of strings? The functions themselves could declare the necessary arrays, and then call the sorting function.
The sorting function would need to be able to sort strings or numbers. You could declare two functions, but that's just a waste. You could pass the array in as a void type and then pass in a function pointer to compare the types, just like qsort() does. http://www.cplusplus.com/reference/c...lib/qsort.html
If you don't feel up to that, you could use something a bit simpler, such as:
Or you could just write two separate functions . . . .
Something I gave away in that example: you use strcmp() to compare strings. It returns less than zero, equal to zero, or greater than zero, depending on whether its arguments were less than, equal to, or great than each other, respectively.
Since you're trying to handle both numbers and strings, why not have two separate functions: one to read an array of numbers, and the other an array of strings? The functions themselves could declare the necessary arrays, and then call the sorting function.
The sorting function would need to be able to sort strings or numbers. You could declare two functions, but that's just a waste. You could pass the array in as a void type and then pass in a function pointer to compare the types, just like qsort() does. http://www.cplusplus.com/reference/c...lib/qsort.html
If you don't feel up to that, you could use something a bit simpler, such as:
c Syntax (Toggle Plain Text)
void sort(void *data, int is_string, /*...*/) { /* ... */ if(is_string) { if(strcmp(((char **)data)[0], ((char **)data)[1]) > 0) /* ... */ } else { if(((int **)data)[0] > ((int **)data)[1]) /* ... */ } }
Something I gave away in that example: you use strcmp() to compare strings. It returns less than zero, equal to zero, or greater than zero, depending on whether its arguments were less than, equal to, or great than each other, respectively.
dwk
Seek and ye shall find.
"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.
"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison
"The only real mistake is the one from which we learn nothing."
-- John Powell
Seek and ye shall find.
"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.
"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison
"The only real mistake is the one from which we learn nothing."
-- John Powell
•
•
Join Date: Jun 2008
Posts: 79
Reputation:
Solved Threads: 7
Is this right? (Line 75 or so)
element is the *address* of the array, a pointer to the array, and it's being compared with a value of the array??
You should be getting a warning about this from your compiler. You shouldn't compare an address with an integer or char value, nor should you try assigning the address of the array (which can't be assigned), the value of a char or array, or even another address.
C Syntax (Toggle Plain Text)
if (element > element [left+1]) //??? { temp = element[left+1]; element[left+1] = element; //??? element = temp; //??? }
element is the *address* of the array, a pointer to the array, and it's being compared with a value of the array??
You should be getting a warning about this from your compiler. You shouldn't compare an address with an integer or char value, nor should you try assigning the address of the array (which can't be assigned), the value of a char or array, or even another address.
Last edited by Adak; Jul 16th, 2008 at 9:41 pm.
•
•
Join Date: Jul 2008
Posts: 22
Reputation:
Solved Threads: 0
oh my god!! i'm only a beginner at c and i don't know structs and unions, and i don't know the difference between int main and void main T_T that's why this is how i constructed my sorts.. T_T what is GCC anyway?? hmm.. but of course i do know how to understand the logic tha's my strong points.. sorry >_< if my logic is somehow difficult to understand T_T
•
•
Join Date: Jul 2008
Posts: 22
Reputation:
Solved Threads: 0
Uhhm, i was able to fix the recent problems.. now, i think that my sorting algo is a bit off than what i imagined it, uhmm.. i haven't edited it though to integrate it with what you've written here.. but thanks! i think i'll try it later tonight or tomorrow, T__T 10 days before the judgement day..
c Syntax (Toggle Plain Text)
#include"stdio.h" #include"stdlib.h" void read_list(int element[], int total,char choice) { int ctr; if (choice == 'a' || choice == 'A') { for(ctr=0;ctr<total;ctr++) { printf("\n Enter Element [%d] ::" ,ctr); scanf("%s", &element[ctr]); } } if (choice == 'b' || choice == 'B') { for(ctr=0;ctr<total;ctr++) { printf("\n Enter Element [%d] ::" ,ctr); scanf("%d", &element[ctr]); } } } void print_list(int element[], int total,char choice) { int ctr; if (choice == 'a' || choice == 'A') { for(ctr=0;ctr<total;ctr++) printf(" %c", element[ctr]); } if (choice == 'b' || choice == 'B') { for(ctr=0;ctr<total;ctr++) printf(" %d", element[ctr]); } } void insert_sort(int element[], int total,char choice) { int pt, mid, left, right, ctr, ctr2, ctr3, temp; mid = (total+1)/2; pt = 0; for(ctr=total-1, ctr2=0; ctr>=mid && ctr2 <mid; ctr--, ctr2++) { if (element[ctr2] > element[ctr]) { temp = element[ctr]; element[ctr] = element[ctr2]; element[ctr2] = temp; } for (left=0; left<=ctr2; left++) { if (element > element [left+1]) { temp = element[left+1]; element[left+1] = element; element = temp; } } for (right=total-1; right>=ctr; right--) { if (element[right-1] > element) { temp = element; element = element[right-1]; element[right-1] = temp; } } printf("\n Go Pass %d :: ", ctr2); print_list(element,total,choice); } } void main() { int element[20], total; char choice; clrscr(); printf("Choose: A - Alphanumeric B - Numeric :: "); scanf("%c", &choice); printf("\n Enter Array Length :: "); scanf("%d", &total); read_list(element,total,choice); printf("\n The Array Elements are as follows :: "); print_list(element,total,choice); insert_sort(element,total,choice); printf("\n\n Final Answer :: "); print_list(element,total,choice); getch(); }
![]() |
Similar Threads
- Linked list help (C++)
- Question: Linear Time Sorting Problem (Computer Science)
- Help needed (Assembly)
- how to use sorting algorithm (Java)
- sorting array (C++)
- sorting a text file (C++)
- Use sys calls fork(),pipe(),open()..... (C)
- C++ Reorder random numbers (C++)
Other Threads in the C Forum
- Previous Thread: cocatenattion without strcat function
- Next Thread: two letters = one number?
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft multi mysql oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation testing threads unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






