re: Sorting Algorithms

Reply

Join Date: Oct 2003
Posts: 1
Reputation: OoTOAoO is an unknown quantity at this point 
Solved Threads: 0
OoTOAoO OoTOAoO is offline Offline
Newbie Poster

re: Sorting Algorithms

 
0
  #1
Oct 21st, 2003
Ok guys... I'm sorta new to this forum.. so hello all.. but neways.. here is my problem.. This is the assignment I have:
http://www.ece.uc.edu/~berman/228-2003/program2.html

Can someone basically explain what I should do? Am I supposed to like sort part of it using insertionsort and the other part with one of the other 2? I'm confused as to this whole threshold thing.

Here is what I have so far:
  1. /* Program 2
  2. Author: B. Josh Becker
  3. */
  4.  
  5. #include <iostream> // preprocessor
  6. #include <ctime>
  7. #include <cstdlib>
  8. #include <cstdio>
  9.  
  10. using namespace std;
  11.  
  12. void mergesort(int[], int, int);
  13. void merge(int[], int, int);
  14. void quicksort(int[], int, int);
  15. void choosePivot(int[], int, int);
  16. void partition(int[], int, int, int&);
  17. void insertionSort(int[], int);
  18.  
  19. void main(){
  20.  
  21. int numbers[200] = {0};
  22. int response, choice,threshhold;
  23.  
  24. cout << endl << "Would you like to randomly choose a list or should I create one at random?" << endl;
  25. cout << "\t1) Create one (less than 100 values)" << endl;
  26. cout << "\t2) Random" << endl;
  27. cin >> choice;
  28.  
  29. switch(choice){
  30. case 1:
  31. {
  32. int number;
  33. cout << endl << "Please enter the number of values you would like in the array (less than 100):";
  34. cin >> number;
  35. cout << endl << "Please enter each number followed by the return key" << endl;
  36. if (number>100)
  37. break;
  38. for (int i=0;i<number;i++)
  39. cin >> numbers[i];
  40. cout << "Here are the numbers you just entered:" << endl;
  41. for(i=0;i<number;i++)
  42. cout << numbers[i] << " ";
  43. break;
  44. }
  45. case 2:
  46. {
  47. int number;
  48. cout << endl << "Please enter the number of values you would like in the array:";
  49. cin >> number;
  50. srand( (unsigned)time( NULL ) );
  51. for(int i=0;i<number;i++)
  52. numbers[i]=rand();
  53.  
  54. cout << "Here are the random values:" << endl;
  55.  
  56. for(i=0;i<number;i++)
  57. cout << numbers[i] << " ";
  58. break;
  59. }
  60. default:{
  61. cout << "Sorry invalid value" << endl;
  62. break;
  63. }
  64.  
  65. }
  66.  
  67. cout << endl << "Please enter a threshold value:" << endl;
  68. cin >> threshhold;
  69.  
  70. cout << "This program allows the user to sort numbers with different sorting methods" << endl;
  71. cout << "Would you like to use :" << endl;
  72. cout << "\t1) Quicksort" << endl;
  73. cout << "\t2) Mergesort" << endl;
  74. cout << "\t3) Insertionsort" << endl;
  75. cin >> response;
  76.  
  77. }
  78.  
  79. // Start for Mergesort
  80.  
  81. void mergesort(int a[], int left, int right)
  82. {
  83. if(left<right)
  84. {
  85. int mid=(left + right)/2;
  86. mergesort(a, left, mid);
  87. mergesort(a, mid+1, right);
  88. merge(a,left,right);
  89. }
  90. }
  91.  
  92.  
  93. void merge(int a[],int left, int right)
  94. {
  95. int size=right-left+1,
  96. mid=(left+right)/2;
  97. int y, put;
  98. int *b=new int[size];
  99. int count=0;
  100. for(int x=left;x<=mid;x++,count++)
  101. {
  102. b[count]=a[x];
  103. }
  104. for(x=right;x>=mid+1;x--,count++)
  105. {
  106. b[count]=a[x];
  107. }
  108. for(x=0,y=size-1,put=left;x<=y;put++)
  109. {
  110. if(b[x]<=b[y])
  111. {
  112. a[put]=b[x];
  113. x++;
  114. }
  115. else
  116. {
  117. a[put]=b[y];
  118. y--;
  119. }
  120. }
  121. delete[] b;
  122. }
  123.  
  124. // Start for quicksort
  125.  
  126. void choosePivot(int theArray[], int first, int last){
  127. {
  128. int pivotIndex;
  129. int mid = (first + last) / 2;
  130.  
  131. if( theArray[ first ] <= theArray[ mid ] )
  132. { if( theArray[ mid ] <= theArray[ last ] ) pivotIndex = mid;
  133. else pivotIndex = ( theArray[ first ] <= theArray[ last ] ? last : first );
  134. }
  135. else if( theArray[ last ] <= theArray[ mid ] ) pivotIndex = mid;
  136. else pivotIndex = ( theArray[ first ] <= theArray[ last ] ? first : last );
  137. swap( theArray[ first ], theArray[ pivotIndex ] );
  138. }
  139. }
  140.  
  141. void partition(int theArray[], int first, int last, int& pivotIndex)
  142. {
  143. choosePivot(theArray, first, last);
  144. int pivot = theArray[first];
  145.  
  146. int lastS1 = first;
  147. int firstUnknown = first + 1;
  148.  
  149. for (; firstUnknown<=last;++firstUnknown)
  150. {
  151. if (theArray[firstUnknown]<pivot)
  152. {
  153. ++lastS1;
  154. swap(theArray[firstUnknown],theArray[lastS1]);
  155. }
  156. }
  157. swap(theArray[first], theArray[lastS1]);
  158. pivotIndex = lastS1;
  159. }
  160.  
  161. void quicksort(int theArray[], int first, int last)
  162. {
  163. int pivotIndex;
  164.  
  165. if (first<last)
  166. {
  167. partition(theArray, first, last, pivotIndex);
  168.  
  169. quicksort(theArray, first, pivotIndex-1);
  170. quicksort(theArray, pivotIndex+1, last);
  171. }
  172. }
  173.  
  174.  
  175. //Start insertion sort
  176.  
  177. void insertionSort(int theArray[], int n)
  178. {
  179. for (int unsorted=1;unsorted<n;++unsorted)
  180. {
  181. int nextItem = theArray[unsorted];
  182. int loc = unsorted;
  183.  
  184. for (;(loc>0) && (theArray[loc-1]>nextItem);--loc)
  185. theArray[loc] = theArray[loc-1];
  186.  
  187. theArray[loc] = nextItem;
  188. }
  189. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2003
Posts: 16
Reputation: Wiecek5 is an unknown quantity at this point 
Solved Threads: 0
Wiecek5 Wiecek5 is offline Offline
Newbie Poster

Re: Sorting Algorithms

 
0
  #2
Oct 27th, 2003
OoTOAoO aka Josh Becker
You still lookin for help on this? ...
"Beware of computer programmers that carry screwdrivers."
Leonard Brandwein.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC