New Sorting Algo Pls Help Me! :(

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 22
Reputation: SwiftDeath is an unknown quantity at this point 
Solved Threads: 0
SwiftDeath SwiftDeath is offline Offline
Newbie Poster

New Sorting Algo Pls Help Me! :(

 
0
  #1
Jul 2nd, 2008
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.
  1. #include"stdio.h"
  2. #include"stdlib.h"
  3.  
  4. void read_list(int element[], int total)
  5. {
  6. int ctr;
  7. for(ctr=0;ctr<total;ctr++)
  8. {
  9. printf("\n Enter Element [%d] ::" ,ctr);
  10. scanf("%d", &element[ctr]);
  11. }
  12. }
  13.  
  14.  
  15. void print_list(int element[], int total)
  16. {
  17. int ctr;
  18. for(ctr=0;ctr<total;ctr++)
  19. printf(" %d", element[ctr]);
  20. }
  21.  
  22.  
  23. void insert_sort(int element[], int total)
  24. {
  25. int mid, left, right, ctr, ctr2, ctr3, temp;
  26.  
  27. mid = total/2;
  28.  
  29. for(ctr=total-1; ctr>=mid; ctr--)
  30. {
  31. for(ctr2=0; ctr2<mid; ctr2++)
  32. {
  33. if (element[ctr2] > element[ctr])
  34. {
  35. temp = element[ctr];
  36. element[ctr] = element[ctr2];
  37. element[ctr2] = temp;
  38. }
  39. if ((ctr2 < mid) && )(element[ctr2] > element [ctr2+1]))
  40. {
  41. temp = element[ctr2+1];
  42. element[ctr2+1] = element[ctr2];
  43. element[ctr2] = temp;
  44. }
  45. if ((ctr >= mid) && (element[ctr-1] > element[ctr]))
  46. {
  47. temp = element[ctr];
  48. element[ctr] = element[ctr-1];
  49. element[ctr-1] = temp;
  50. }
  51. }
  52. printf("\n Pass %d :: ", ctr2+1);
  53. print_list(element,total);
  54. }
  55. }
  56. void main()
  57. {
  58. int element[20], total;
  59. clrscr();
  60.  
  61. printf("\n Enter Array Length :: ");
  62. scanf("%d", &total);
  63. read_list(element,total);
  64. printf("\n The Array Elements are as follows :: ");
  65. print_list(element,total);
  66. insert_sort(element,total);
  67. printf("\n Last Pass :: ");
  68. print_list(element,total);
  69. getch();
  70. }
I really need you help on this one pls.
Last edited by Ancient Dragon; Jul 2nd, 2008 at 11:52 pm. Reason: add code tags and line numbers
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: New Sorting Algo Pls Help Me! :(

 
0
  #2
Jul 2nd, 2008
>>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.
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 22
Reputation: SwiftDeath is an unknown quantity at this point 
Solved Threads: 0
SwiftDeath SwiftDeath is offline Offline
Newbie Poster

Re: New Sorting Algo Pls Help Me! :(

 
0
  #3
Jul 3rd, 2008
uhmm thanks a lot Ancient Dragon, actually it is my girlfriend's case study. . i dunno why they proposed such, and the dead line is on july 30, 2008. . and i really am having a lot of difficulties. . i really don't know if i can make it though i don't wanna disappoint her. . T_T pls help me. .
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 22
Reputation: SwiftDeath is an unknown quantity at this point 
Solved Threads: 0
SwiftDeath SwiftDeath is offline Offline
Newbie Poster

Re: New Sorting Algo Pls Help Me! :(

 
0
  #4
Jul 3rd, 2008
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. .

  1. #include"stdio.h"
  2. #include"stdlib.h"
  3.  
  4. void read_list(int element[], int total)
  5. {
  6. int ctr;
  7. for(ctr=0;ctr<total;ctr++)
  8. {
  9. printf("\n Enter Element [%d] ::" ,ctr);
  10. scanf("%d", &element[ctr]);
  11. }
  12. }
  13.  
  14.  
  15. void print_list(int element[], int total)
  16. {
  17. int ctr;
  18. for(ctr=0;ctr<total;ctr++)
  19. printf(" %d", element[ctr]);
  20. }
  21.  
  22.  
  23. void insert_sort(int element[], int total)
  24. {
  25. int mid, left, right, ctr, ctr2, ctr3, temp;
  26.  
  27. mid = total/2;
  28.  
  29. for(ctr=total-1; ctr>=mid; ctr--)
  30. {
  31. for(ctr2=0; ctr2<mid; ctr2++)
  32. {
  33. if ((ctr >= mid) && (ctr2 < mid))
  34. {
  35. if (element[ctr2] > element[ctr])
  36. {
  37. temp = element[ctr];
  38. element[ctr] = element[ctr2];
  39. element[ctr2] = temp;
  40. }
  41. }
  42. }
  43.  
  44. for (left=0; left<mid; left++)
  45. {
  46. if (element[ctr2] > element [ctr2+1])
  47. {
  48. temp = element[ctr2+1];
  49. element[ctr2+1] = element[ctr2];
  50. element[ctr2] = temp;
  51. }
  52. }
  53.  
  54. for (right=total-1; right>=mid; right--)
  55. {
  56. if (element[ctr-1] > element[ctr])
  57. {
  58. temp = element[ctr];
  59. element[ctr] = element[ctr-1];
  60. element[ctr-1] = temp;
  61. }
  62. }
  63.  
  64. printf("\n Pass %d :: ", ctr);
  65. print_list(element,total);
  66.  
  67. }
  68. }
  69.  
  70. void main()
  71. {
  72. int element[20], total;
  73. clrscr();
  74.  
  75. printf("\n Enter Array Length :: ");
  76. scanf("%d", &total);
  77. read_list(element,total);
  78. printf("\n The Array Elements are as follows :: ");
  79. print_list(element,total);
  80. insert_sort(element,total);
  81. printf("\n Last Pass :: ");
  82. print_list(element,total);
  83. getch();
  84. }
Last edited by SwiftDeath; Jul 3rd, 2008 at 12:23 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: New Sorting Algo Pls Help Me! :(

 
0
  #5
Jul 3rd, 2008
> 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.
Last edited by Salem; Jul 3rd, 2008 at 12:28 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 22
Reputation: SwiftDeath is an unknown quantity at this point 
Solved Threads: 0
SwiftDeath SwiftDeath is offline Offline
Newbie Poster

Re: New Sorting Algo Pls Help Me! :(

 
0
  #6
Jul 3rd, 2008
uhh. . it's not that but. . i really don't think she can do it, so is there anyone ther whou might be able to help me?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 79
Reputation: Adak is an unknown quantity at this point 
Solved Threads: 7
Adak Adak is offline Offline
Junior Poster in Training

Re: New Sorting Algo Pls Help Me! :(

 
0
  #7
Jul 3rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 22
Reputation: SwiftDeath is an unknown quantity at this point 
Solved Threads: 0
SwiftDeath SwiftDeath is offline Offline
Newbie Poster

Re: New Sorting Algo Pls Help Me! :(

 
0
  #8
Jul 3rd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 79
Reputation: Adak is an unknown quantity at this point 
Solved Threads: 7
Adak Adak is offline Offline
Junior Poster in Training

Re: New Sorting Algo Pls Help Me! :(

 
0
  #9
Jul 3rd, 2008
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:
  1. i = 0;
  2. while(i++ < numbersToBeSorted/2) {
  3. increase the numbers by one in both the first and last subsets
  4. and combine the subsets into one new subset.
  5. sort the new subset using insertion sort
  6. }
  7.  
  8. //handle any odd middle number here, again by insertion sort
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.
Last edited by Adak; Jul 3rd, 2008 at 10:40 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 22
Reputation: SwiftDeath is an unknown quantity at this point 
Solved Threads: 0
SwiftDeath SwiftDeath is offline Offline
Newbie Poster

Re: New Sorting Algo Pls Help Me! :(

 
0
  #10
Jul 3rd, 2008
omg! T_T by brain is already at its max. . how am i suppose to do that? i mean. . where is the best way to start doing so? god, i didn't think of that earlier thanks! you're really good at c, ^_^
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC