| | |
New Sorting Algo Pls Help Me! :(
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2008
Posts: 22
Reputation:
Solved Threads: 0
I am creating a new sorting algorithm as a machine problem and i encounter difficulties.
The logic of my said the said algorithm is to create an array by inputing the total numbers to be accepted followed by the array variables and sorting it ascending or descending. The array would be sorted on both sides, through insertion sort.
So far, i didn't have any luck with it and my deadline is coming to a close. I really need anyone's help on this one i'm begging you.
Here is an example:
Array Total: 6
Ascending: 4 5 9 1 3 2
Compare the 1st and last number in ascending order. Swap if true.
1st Pass: 2 5 9 1 3 4
Increment both sides. this time the numbers to be compared are Group1(2,5) and Group2(3,4). Swap them according to their places.
2nd Pass: 2 3 9 1 4 5
Then the next pass, wherein you do the same procedure as number two. Group1(2,3,9) and Group2(1,4,5) Then swap.
3rd Pass: 2 3 9 1 4 5
The last pass where you arrange the whole array.
Last Pass: 1 2 3 4 5 9
If the amount of numbers to be arrange is odd. Then include the number in the middle to the left group and arrange it before doing the last pass.
Please everyone. Anyone. I really need your help on this.
I really need you help on this one pls.
The logic of my said the said algorithm is to create an array by inputing the total numbers to be accepted followed by the array variables and sorting it ascending or descending. The array would be sorted on both sides, through insertion sort.
So far, i didn't have any luck with it and my deadline is coming to a close. I really need anyone's help on this one i'm begging you.
Here is an example:
Array Total: 6
Ascending: 4 5 9 1 3 2
Compare the 1st and last number in ascending order. Swap if true.
1st Pass: 2 5 9 1 3 4
Increment both sides. this time the numbers to be compared are Group1(2,5) and Group2(3,4). Swap them according to their places.
2nd Pass: 2 3 9 1 4 5
Then the next pass, wherein you do the same procedure as number two. Group1(2,3,9) and Group2(1,4,5) Then swap.
3rd Pass: 2 3 9 1 4 5
The last pass where you arrange the whole array.
Last Pass: 1 2 3 4 5 9
If the amount of numbers to be arrange is odd. Then include the number in the middle to the left group and arrange it before doing the last pass.
Please everyone. Anyone. I really need your help on this.
c Syntax (Toggle Plain Text)
#include"stdio.h" #include"stdlib.h" void read_list(int element[], int total) { int ctr; for(ctr=0;ctr<total;ctr++) { printf("\n Enter Element [%d] ::" ,ctr); scanf("%d", &element[ctr]); } } void print_list(int element[], int total) { int ctr; for(ctr=0;ctr<total;ctr++) printf(" %d", element[ctr]); } void insert_sort(int element[], int total) { int mid, left, right, ctr, ctr2, ctr3, temp; mid = total/2; for(ctr=total-1; ctr>=mid; ctr--) { for(ctr2=0; ctr2<mid; ctr2++) { if (element[ctr2] > element[ctr]) { temp = element[ctr]; element[ctr] = element[ctr2]; element[ctr2] = temp; } if ((ctr2 < mid) && )(element[ctr2] > element [ctr2+1])) { temp = element[ctr2+1]; element[ctr2+1] = element[ctr2]; element[ctr2] = temp; } if ((ctr >= mid) && (element[ctr-1] > element[ctr])) { temp = element[ctr]; element[ctr] = element[ctr-1]; element[ctr-1] = temp; } } printf("\n Pass %d :: ", ctr2+1); print_list(element,total); } } void main() { int element[20], total; clrscr(); printf("\n Enter Array Length :: "); scanf("%d", &total); read_list(element,total); printf("\n The Array Elements are as follows :: "); print_list(element,total); insert_sort(element,total); printf("\n Last Pass :: "); print_list(element,total); getch(); }
Last edited by Ancient Dragon; Jul 2nd, 2008 at 11:52 pm. Reason: add code tags and line numbers
>>I am creating a new sorting algorithm
Not very likely unles you are doing a Ph.D. thesis. Sort algorithms have been one of the most researched topics in computer science, so designing your own unique algorithm is very unlikely.
Not very likely unles you are doing a Ph.D. thesis. Sort algorithms have been one of the most researched topics in computer science, so designing your own unique algorithm is very unlikely.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jul 2008
Posts: 22
Reputation:
Solved Threads: 0
i have re-written the code, it gives the first and last pass correct but it kinda does not in the in-between passes from 1st to last. . T_T i think i'm gonna have migraine. .
c Syntax (Toggle Plain Text)
#include"stdio.h" #include"stdlib.h" void read_list(int element[], int total) { int ctr; for(ctr=0;ctr<total;ctr++) { printf("\n Enter Element [%d] ::" ,ctr); scanf("%d", &element[ctr]); } } void print_list(int element[], int total) { int ctr; for(ctr=0;ctr<total;ctr++) printf(" %d", element[ctr]); } void insert_sort(int element[], int total) { int mid, left, right, ctr, ctr2, ctr3, temp; mid = total/2; for(ctr=total-1; ctr>=mid; ctr--) { for(ctr2=0; ctr2<mid; ctr2++) { if ((ctr >= mid) && (ctr2 < mid)) { if (element[ctr2] > element[ctr]) { temp = element[ctr]; element[ctr] = element[ctr2]; element[ctr2] = temp; } } } for (left=0; left<mid; left++) { if (element[ctr2] > element [ctr2+1]) { temp = element[ctr2+1]; element[ctr2+1] = element[ctr2]; element[ctr2] = 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; } } printf("\n Pass %d :: ", ctr); print_list(element,total); } } void main() { int element[20], total; clrscr(); printf("\n Enter Array Length :: "); scanf("%d", &total); read_list(element,total); printf("\n The Array Elements are as follows :: "); print_list(element,total); insert_sort(element,total); printf("\n Last Pass :: "); print_list(element,total); getch(); }
Last edited by SwiftDeath; Jul 3rd, 2008 at 12:23 am.
> actually it is my girlfriend's case study
Get her to do her own homework you sap!
> i really don't know if i can make it though i don't wanna disappoint her.
Or what? She'll leave you?
She'll do that anyway once you've out-lived your usefulness to her.
Get her to do her own homework you sap!
> i really don't know if i can make it though i don't wanna disappoint her.
Or what? She'll leave you?
She'll do that anyway once you've out-lived your usefulness to her.
Last edited by Salem; Jul 3rd, 2008 at 12:28 am.
•
•
Join Date: Jun 2008
Posts: 79
Reputation:
Solved Threads: 7
I'll agree with you that you need help - anyone who names their array "element[]", is utterly at their limit! 
I don't agree with your algorithm, however. It looks like it will fail when you are sorting more numbers which are not favorably arranged already, but I haven't run it yet, either.
I'll take a peek at it later tonight or tomorrow. Absolutely have no expectations about this, however. Sorting has been studied to death, btw. Is there some name for this kind of sort? May have info on the net by the bucket load, if so.

I don't agree with your algorithm, however. It looks like it will fail when you are sorting more numbers which are not favorably arranged already, but I haven't run it yet, either.
I'll take a peek at it later tonight or tomorrow. Absolutely have no expectations about this, however. Sorting has been studied to death, btw. Is there some name for this kind of sort? May have info on the net by the bucket load, if so.
•
•
Join Date: Jul 2008
Posts: 22
Reputation:
Solved Threads: 0
oohh.. uhmm, actually you're right it does limit the number of variables it can store in the array to 20, her study is theory based and the teacher said that a support program is mandatory so i'm trying the best i can to do so. .
the code sorts the array accurately up to 8 numbers so far, i haven't ttried 10 up.. nd yet i already encounter so many difficulties,
ascending order:
for example: 5 4 6 8 2 1 -- 1st last to be compared, then increment left and decrement right.
1st pass: 1 4 6 8 2 5 -- then compare the underline again and swap
2nd pass: 1 2 6 8 4 5 -- compare the underline again and swap
last pass: 1 2 4 5 6 8 -- final answer.
this is the flow of the algorithms logic. . thanks a lot every one i really appreciate it T_T
the code sorts the array accurately up to 8 numbers so far, i haven't ttried 10 up.. nd yet i already encounter so many difficulties,
ascending order:
for example: 5 4 6 8 2 1 -- 1st last to be compared, then increment left and decrement right.
1st pass: 1 4 6 8 2 5 -- then compare the underline again and swap
2nd pass: 1 2 6 8 4 5 -- compare the underline again and swap
last pass: 1 2 4 5 6 8 -- final answer.
this is the flow of the algorithms logic. . thanks a lot every one i really appreciate it T_T
•
•
Join Date: Jun 2008
Posts: 79
Reputation:
Solved Threads: 7
I believe I understand what the algo should be. You're using an insertion sort, like the last step in Quicksort, but you're using it over and over, on ever-increasing sizes of two groups.
So the algorithm is:
So you have three functions, one to make the left and right side sets up with the right amount of numbers, the second to combine them into one new subset, and the third to sort the new subset via insertion sort.
You can't just compare the right and left subsets, as I understand your description. As the amount of numbers to be sorted increases, it will not work.
So the algorithm is:
C Syntax (Toggle Plain Text)
i = 0; while(i++ < numbersToBeSorted/2) { increase the numbers by one in both the first and last subsets and combine the subsets into one new subset. sort the new subset using insertion sort } //handle any odd middle number here, again by insertion sort
You can't just compare the right and left subsets, as I understand your description. As the amount of numbers to be sorted increases, it will not work.
Last edited by Adak; Jul 3rd, 2008 at 10:40 am.
![]() |
Other Threads in the C Forum
- Previous Thread: Convert struct to short
- Next Thread: boolean expression as a for loop iteration (???)
| Thread Tools | Search this Thread |
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string strings structures suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h






