470,571 Members | 2,353 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,571 developers. It's quick & easy.

Sorting algorithms problem

beacon
579 512MB
I'm writing a program as an assignment that takes 5 sorting algorithms and and tests for the amount of time and the number of comparisons it takes to um, sort an array.

I have run into some trouble though. On lines 54-59, I previously had them placed from line 46 on. This worked out great for printing just the bubble sort, but I have to get the other four sorting algorithms in here and I was hoping to print the contents out in a for loop, as it shows on line 53.

When I attempt this, however, I get nothing but a blank executable. Any ideas?

Also, would someone please check out my sorting algorithms and see if you don't see any mistakes. My professor told us to just copy and paste them, but there were a ton of errors that I had to correct and I'm not sure if I got them all because I don't exactly know the ins and outs of the algorithms just yet. If someone does check this out, would you pay special attention to the QuickSort? I have to find a way to get that one to work using only 2 parameters and I'm pretty much out of ideas.

Thanks for the help...

MAIN.CPP
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <ctime>
  4. #include <iomanip>
  5.  
  6. #include "SortClass.h"
  7.  
  8. using namespace std;
  9.  
  10. //This copies the contents of A2 into A1
  11. void CopyArray(int A1[],int A2[],int size){
  12.     for(int j=0;j<size;j++){
  13.         A1[j]=A2[j];
  14.     }
  15. }
  16.  
  17. void FillArray(int A[],int size){
  18.     srand(29734);
  19.     for(int j=0;j<size;j++){
  20.         A[j]=rand()%size;
  21.     }
  22. }
  23.  
  24. void main(){
  25.  
  26.     int Sizes[] = {100,1000,5000,10000,50000,100000};
  27.     int ArraySize=0;
  28.     int * Nums;
  29.     int * TempNums;
  30.  
  31.     SortClass S;
  32.     SortResults SR[30];
  33.  
  34.     int count=0;
  35.  
  36.     //major driving loop
  37.     for(int i=0;i<6;i++){
  38.  
  39.         Nums = new int[Sizes[i]] ;
  40.         TempNums = new int[Sizes[i]];
  41.  
  42.         FillArray(Nums,Sizes[i]);
  43.  
  44.         CopyArray(TempNums,Nums,Sizes[i]);
  45.         SR[count++] = S.BubbleSort(TempNums,Sizes[i]);
  46.  
  47.         delete [] Nums;
  48.         delete [] TempNums;
  49.  
  50.     }//end driving loop
  51.  
  52.     //make another loop to print out results
  53.     for(i=0; i<30; i++){
  54.         cout<<SR[i].SortName<<endl;
  55.         cout<<SR[i].Comparisons<<endl;
  56.         cout<<SR[i].DataSetSize<<endl;
  57.         cout<<SR[i].RunTime<<endl;
  58.         cout<<SR[i].Swaps<<endl;
  59.         cout<<endl<<endl;
  60.     }
  61.     //end loop
  62.  
  63. }
SORTCLASS.CPP
Expand|Select|Wrap|Line Numbers
  1. // SortClass.cpp: implementation of the SortClass class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #include "SortClass.h"
  6.  
  7. //////////////////////////////////////////////////////////////////////
  8. // Construction/Destruction
  9. //////////////////////////////////////////////////////////////////////
  10.  
  11. SortClass::SortClass()
  12. {
  13.  
  14. }
  15.  
  16. SortClass::~SortClass()
  17. {
  18.  
  19. }
  20.  
  21. SortResults SortClass::BubbleSort(int numbers[], int array_size){
  22.  
  23.     int i=0, j=0, temp=0;
  24.     int comparisons=0,swaps=0;
  25.  
  26.     DWORD start_time,end_time,run_time;
  27.  
  28.     SortResults BR;
  29.     BR.SortName = "BubbleSort";
  30.     BR.DataSetSize = array_size;
  31.  
  32.     start_time = GetTickCount();
  33.     for (i = (array_size - 1); i >= 0; i--){
  34.         comparisons++;
  35.         for(j=1;j<=i;j++){
  36.             if (numbers[j-1] > numbers[j]){
  37.                 temp = numbers[j-1];
  38.                 numbers[j-1] = numbers[j];
  39.                 numbers[j] = temp;
  40.                 swaps++;
  41.             }
  42.         }
  43.     }
  44.  
  45.     end_time = GetTickCount();
  46.  
  47.     run_time = end_time-start_time;
  48.  
  49.  
  50.     BR.Comparisons = comparisons;
  51.     BR.Swaps = swaps;
  52.     BR.RunTime = (run_time/1000.0);
  53.  
  54.     return BR;
  55.  
  56. }
  57.  
  58. void SortClass::SelectionSort(int numbers[], int array_size){
  59.  
  60.     int i, j, temp;
  61.     int pos_greatest;
  62.     int comparisons=0,swaps=0;
  63.  
  64.     DWORD start_time,end_time,run_time;
  65.  
  66.     SortResults BR;
  67.     BR.SortName = "BubbleSort";
  68.     BR.DataSetSize = array_size;
  69.  
  70.     start_time = GetTickCount();
  71.  
  72.     for (i = (array_size - 1); i > 0; i--){
  73.         pos_greatest = 0;
  74.         for (j = 0; j <= i; j++){
  75.             if(numbers[j] > numbers[pos_greatest]){
  76.                 pos_greatest = j;
  77.             }
  78.         }
  79.         temp = numbers[i];
  80.         numbers[i] = numbers[pos_greatest];
  81.         numbers[pos_greatest] = temp;
  82.     }
  83.  
  84.     end_time = GetTickCount();
  85.  
  86.     run_time = end_time-start_time;
  87.  
  88.  
  89.     BR.Comparisons = comparisons;
  90.     BR.Swaps = swaps;
  91.     BR.RunTime = (run_time/1000.0);
  92.  
  93.     return BR;
  94. }
  95.  
  96. void SortClass::InsertionSort(int numbers[], int array_size){
  97.  
  98.     int i, j, index;
  99.     int comparisons=0,swaps=0;
  100.  
  101.     DWORD start_time,end_time,run_time;
  102.  
  103.     SortResults BR;
  104.     BR.SortName = "BubbleSort";
  105.     BR.DataSetSize = array_size;
  106.  
  107.     start_time = GetTickCount();
  108.  
  109.     for (i = 1; i < array_size; i++){
  110.         index = numbers[i];
  111.         j=i;
  112.         while ((j > 0) && (numbers[j-1] > index)){
  113.             numbers[j] = numbers[j-1];
  114.             j = j-1;
  115.         }
  116.         numbers[j] = index;
  117.     }
  118.  
  119.     end_time = GetTickCount();
  120.  
  121.     run_time = end_time-start_time;
  122.  
  123.  
  124.     BR.Comparisons = comparisons;
  125.     BR.Swaps = swaps;
  126.     BR.RunTime = (run_time/1000.0);
  127.  
  128.     return BR;
  129. }
  130.  
  131. void SortClass::ShellSort(int numbers[], int array_size){
  132.  
  133.     int i, j, increment, temp;
  134.     int comparisons=0,swaps=0;
  135.  
  136.     DWORD start_time,end_time,run_time;
  137.  
  138.     SortResults BR;
  139.     BR.SortName = "BubbleSort";
  140.     BR.DataSetSize = array_size;
  141.  
  142.     start_time = GetTickCount();
  143.  
  144.     increment = 3;
  145.     while (increment > 0){
  146.         for (i = 0; i < array_size; i++){
  147.             j = i;
  148.             temp = numbers[i];
  149.             while ((j >= increment) && (numbers[j-increment] > temp)){
  150.                 numbers[j] = numbers[j - increment];
  151.                 j = j - increment;
  152.             }
  153.             numbers[j] = temp;
  154.         }
  155.         if (increment/2 != 0)
  156.             increment = increment/2;
  157.         else if (increment == 1)
  158.             increment = 0;
  159.         else
  160.             increment = 1;
  161.     }
  162.  
  163.     end_time = GetTickCount();
  164.  
  165.     run_time = end_time-start_time;
  166.  
  167.  
  168.     BR.Comparisons = comparisons;
  169.     BR.Swaps = swaps;
  170.     BR.RunTime = (run_time/1000.0);
  171.  
  172.     return BR;
  173. }
  174.  
  175. void SortClass::QuickSort(int numbers[], int start, int end){
  176.  
  177.     int i = start;
  178.     int k = end;
  179.     int comparisons=0,swaps=0;
  180.  
  181.     DWORD start_time,end_time,run_time;
  182.  
  183.     SortResults BR;
  184.     BR.SortName = "BubbleSort";
  185.     BR.DataSetSize = array_size;
  186.  
  187.     start_time = GetTickCount();
  188.  
  189.     if (end - start >= 1){
  190.         int pivot = numbers[start];
  191.  
  192.         while (k > i){
  193.             while (numbers[i] <= pivot && i <= end && k > i)
  194.                 i++;
  195.             while (numbers[k] > pivot && k >= start && k >= i)
  196.                 k--;
  197.             if (k > i)
  198.                 swap(numbers, start, k);
  199.         }
  200.         swap(numbers, start, k);
  201.  
  202.         QuickSort(numbers, start, k-1);
  203.  
  204.  
  205.     }
  206.     else{
  207.         return;
  208.     }
  209.  
  210.     end_time = GetTickCount();
  211.  
  212.     run_time = end_time-start_time;
  213.  
  214.  
  215.     BR.Comparisons = comparisons;
  216.     BR.Swaps = swaps;
  217.     BR.RunTime = (run_time/1000.0);
  218.  
  219.     return BR;
  220. }
  221.  
  222. void SortClass::swap(int numbers[], int index1, int index2){
  223.  
  224.     int temp = numbers[index1];
  225.     numbers[index1] = numbers[index2];
  226.     numbers[index2] = temp;
  227. }
  228.  
SORTCLASS.H
Expand|Select|Wrap|Line Numbers
  1. // SortClass.h: interface for the SortClass class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include <windows.h>
  8. #include <stdio.h>
  9.  
  10. using namespace std;
  11.  
  12. #if !defined(AFX_SORTCLASS_H__B90D8BDD_9661_4EC7_9789_4EB5E7E4B0EA__INCLUDED_)
  13. #define AFX_SORTCLASS_H__B90D8BDD_9661_4EC7_9789_4EB5E7E4B0EA__INCLUDED_
  14.  
  15. #if _MSC_VER > 1000
  16. #pragma once
  17. #endif // _MSC_VER > 1000
  18.  
  19. struct SortResults{
  20.     string SortName;
  21.     int DataSetSize;
  22.     unsigned int Swaps;
  23.     int Comparisons;
  24.     double RunTime;
  25. };
  26.  
  27. class SortClass  
  28. {
  29.  
  30.  
  31. public:
  32.  
  33.     SortClass();
  34.     ~SortClass();
  35.     SortResults BubbleSort(int[], int);
  36.     SortResults SelectionSort(int[], int);
  37.     SortResults InsertionSort(int[], int);
  38.     SortResults ShellSort(int[], int);
  39.     SortResults QuickSort(int[], int, int);
  40.     SortResults swap(int[], int, int);
  41.  
  42. };
  43.  
  44. #endif // !defined(AFX_SORTCLASS_H__280BFDFC_EE79_43E7_B877_EE25B636B5FC__INCLUDED_)
  45.  
P.S. Sorry for such a looooooooonnnnnggg post!
Nov 14 '07 #1
7 2196
Ganon11
3,652 Expert 2GB
I'm not sure why your loop isn't working in your driver program (the one with main()), but I can help with your QuickSort question. The two parameter QuickSort should need just the array of numbers and its length in an int variable (an int[] and an int - that's two parameters). However, this short method only calls your three parameter QuickSort with the array, the start (which index is the start?), and the end (which index is the end?). The three-parameter QuickSort can be private, to prevent anyone from using it incorrectly. Now your QuickSort looks just like the other sorting methods, and people can use it in the same way.
Nov 14 '07 #2
beacon
579 512MB
I noticed the part about the quick sort after the fact and I had already changed it to resemble what you described. Thanks for pointing it out though because if I hadn't seen it I surely would have been greatful for the help.
Nov 14 '07 #3
beacon
579 512MB
I got this straightened out. In my frustration I started playing around with numbers in the array and was telling the program to loop through and sort for 30 data sets in my struct.

I must have been have asleep when I did this...
Nov 14 '07 #4
beacon
579 512MB
Expand|Select|Wrap|Line Numbers
  1. SortResults SortClass::BubbleSort(int numbers[], int array_size){
  2.  
  3.     int i=0, j=0, temp=0;
  4.     int comparisons=0,swaps=0;
  5.  
  6.     DWORD start_time,end_time,run_time;
  7.  
  8.     SortResults BR;
  9.     BR.SortName = "BubbleSort";
  10.     BR.DataSetSize = array_size;
  11.  
  12.     start_time = GetTickCount();
  13.     for (i = (array_size - 1); i >= 0; i--){
  14.         comparisons++;
  15.         for(j=1;j<=i;j++){
  16.             if (numbers[j-1] > numbers[j]){
  17.                 temp = numbers[j-1];
  18.                 numbers[j-1] = numbers[j];
  19.                 numbers[j] = temp;
  20.                 swaps++;
  21.             }
  22.         }
  23.     }
  24.  
  25.     end_time = GetTickCount();
  26.  
  27.     run_time = end_time-start_time;
  28.  
  29.  
  30.     BR.Comparisons = comparisons;
  31.     BR.Swaps = swaps;
  32.     BR.RunTime = (run_time/1000.0);
  33.  
  34.     return BR;
  35.  
  36. }
  37.  
  38. SortResults SortClass::SelectionSort(int numbers[], int array_size){
  39.  
  40.     int i, j, temp;
  41.     int pos_greatest;
  42.     int comparisons=0,swaps=0;
  43.  
  44.     DWORD start_time,end_time,run_time;
  45.  
  46.     SortResults SS;
  47.     SS.SortName = "SelectionSort";
  48.     SS.DataSetSize = array_size;
  49.  
  50.     start_time = GetTickCount();
  51.  
  52.     for (i = (array_size - 1); i > 0; i--){
  53.         pos_greatest = 0;
  54.         for (j = 0; j <= i; j++){
  55.             comparisons++;
  56.             if(numbers[j] > numbers[pos_greatest]){
  57.                 pos_greatest = j;
  58.             }
  59.         }
  60.         temp = numbers[i];
  61.         numbers[i] = numbers[pos_greatest];
  62.         numbers[pos_greatest] = temp;
  63.         swaps++;
  64.     }
  65.  
  66.     end_time = GetTickCount();
  67.  
  68.     run_time = end_time-start_time;
  69.  
  70.  
  71.     SS.Comparisons = comparisons;
  72.     SS.Swaps = swaps;
  73.     SS.RunTime = (run_time/1000.0);
  74.  
  75.     return SS;
  76. }
  77.  
  78. SortResults SortClass::InsertionSort(int numbers[], int array_size){
  79.  
  80.     int i, j, index;
  81.     int comparisons=0,swaps=0;
  82.  
  83.     DWORD start_time,end_time,run_time;
  84.  
  85.     SortResults IS;
  86.     IS.SortName = "InsertionSort";
  87.     IS.DataSetSize = array_size;
  88.  
  89.     start_time = GetTickCount();
  90.  
  91.     for (i = 1; i < array_size; i++){
  92.         index = numbers[i];
  93.         j=i;
  94.         while ((j > 0) && (numbers[j-1] > index)){
  95.             comparisons++;
  96.             numbers[j] = numbers[j-1];
  97.             j = j-1;
  98.         }
  99.         numbers[j] = index;
  100.         swaps++;
  101.     }
  102.  
  103.     end_time = GetTickCount();
  104.  
  105.     run_time = end_time-start_time;
  106.  
  107.  
  108.     IS.Comparisons = comparisons;
  109.     IS.Swaps = swaps;
  110.     IS.RunTime = (run_time/1000.0);
  111.  
  112.     return IS;
  113. }
  114.  
  115. SortResults SortClass::ShellSort(int numbers[], int array_size){
  116.  
  117.     int i, j, increment, temp;
  118.     int comparisons=0,swaps=0;
  119.  
  120.     DWORD start_time,end_time,run_time;
  121.  
  122.     SortResults ShS;
  123.     ShS.SortName = "ShellSort";
  124.     ShS.DataSetSize = array_size;
  125.  
  126.     start_time = GetTickCount();
  127.  
  128.     increment = 3;
  129.     while (increment > 0){
  130.         for (i = 0; i < array_size; i++){
  131.             j = i;
  132.             temp = numbers[i];
  133.             while ((j >= increment) && (numbers[j-increment] > temp)){
  134.                 comparisons++;
  135.                 numbers[j] = numbers[j - increment];
  136.                 j = j - increment;
  137.             }
  138.             numbers[j] = temp;
  139.             swaps++;
  140.         }
  141.         if (increment/2 != 0)
  142.             increment = increment/2;
  143.         else if (increment == 1)
  144.             increment = 0;
  145.         else
  146.             increment = 1;
  147.     }
  148.  
  149.     end_time = GetTickCount();
  150.  
  151.     run_time = end_time-start_time;
  152.  
  153.  
  154.     ShS.Comparisons = comparisons;
  155.     ShS.Swaps = swaps;
  156.     ShS.RunTime = (run_time/1000.0);
  157.  
  158.     return ShS;
  159. }
  160.  
  161. SortResults SortClass::QuickSort(int numbers[], int array_size){
  162.  
  163.                 int comparisons=0,swaps=0;
  164.                 int start = 0, end = 0;
  165.  
  166.     DWORD start_time,end_time,run_time;
  167.  
  168.     SortResults QS;
  169.     QS.SortName = "QuickSort";
  170.     QS.DataSetSize = array_size;
  171.  
  172.     start_time = GetTickCount();
  173.  
  174.     quickSort(numbers, start, end);
  175.  
  176.     end_time = GetTickCount();
  177.  
  178.     run_time = end_time-start_time;
  179.  
  180.  
  181.     QS.Comparisons = comparisons;
  182.     QS.Swaps = swaps;
  183.     QS.RunTime = (run_time/1000.0);
  184.  
  185.     return QS;
  186.  
  187. }
  188.  
  189. void SortClass::quickSort(int numbers[], int start, int end){
  190.  
  191.     int i = start;
  192.     int k = end;
  193.  
  194.     if (end - start >= 1){
  195.         int pivot = numbers[start];
  196.  
  197.         while (k > i){
  198.             while (numbers[i] <= pivot && i <= end && k > i)
  199.                 i++;
  200.             while (numbers[k] > pivot && k >= start && k >= i)
  201.                 k--;
  202.             if (k > i){
  203.                 comparisons++;
  204.                 swap(numbers, start, k);
  205.                                                 }
  206.         }
  207.         swaps++;
  208.         swap(numbers, start, k);
  209.                                 quickSort(numbers, start, k-1);
  210.                                 quickSort(numbers, k+1, end);
  211.  
  212.  
  213.     }
  214.     else{
  215.         return;
  216.     }
  217.  
  218. }
  219.  
  220. void SortClass::swap(int numbers[], int index1, int index2){
  221.  
  222.     int temp = numbers[index1];
  223.     numbers[index1] = numbers[index2];
  224.     numbers[index2] = temp;
  225. }
  226.  
My last dilemma with this is that I have to count the number of comparisons and swaps each sort makes. I got the bubble, insertion, selection, and shell to work, but not the quick. Can anybody tell me where I should place my comparisons and swap counters to get them to increment correctly?

Have I even declared the quickSort function correctly in the QuickSort class? Do I need to in order to get the QuickSort to work properly?

Also, please check that I have them placed in the correct location for the other sorts. I believe I do, but it never hurts to have a trained eye take a look.

Thanks...
Nov 14 '07 #5
Ganon11
3,652 Expert 2GB
You make a comparison at every execution of your inner while loops, too, so you should have comparisons++ in there too.
Nov 14 '07 #6
beacon
579 512MB
So if I have a while loop, I need to place a comparison in every inner while loop? What about the swaps? Were they in the right place?

Also, I'm still not sure how to get the quick sort to test for anything. I don't think I have called the function correctly or something.
Nov 14 '07 #7
Laharl
849 Expert 512MB
Your swaps look OK, I think.

As to testing quicksort, you can put in some printing statements to print the array after each swap, or each recursive call, or whatever interval you feel works. This may require a printArray function so you're not constantly rewriting the same code.
Nov 14 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by York | last post: by
22 posts views Thread by mike | last post: by
2 posts views Thread by drud via AccessMonster.com | last post: by
2 posts views Thread by Jack | last post: by
25 posts views Thread by Allie | last post: by
20 posts views Thread by martin-g | last post: by
5 posts views Thread by lemlimlee | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.