Advertisement
darkhelmet125

335project1

Feb 25th, 2013
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include "timer.h"
  4. using namespace std;
  5.  
  6. //function declarations
  7. void BubbleSort(vector<int>, int n);
  8. void SelectionSort(int n);
  9. int MinElement(int n);
  10.  
  11. //main
  12. int main()
  13. {
  14.     int choice=0;
  15.     int length=0;
  16.     cout<<"Choose algorithm: "<<endl;
  17.     cout<<"1. MinElement"<<endl;
  18.     cout<<"2. Bubble Sort"<<endl;
  19.     cout<<"3. Selection Sort"<<endl;
  20.     cout<<"Choice: ";
  21.     cin>>choice;
  22.     cout<<"How big is the array? ";
  23.     cin>>length;
  24.     vector <int> sortThis[length];
  25.     for(int i=0;i<length;i++)
  26.     {
  27.         sortThis[i]=rand()%100;
  28.     }
  29.     switch(choice)
  30.     {
  31.         case 1:
  32.             MinElement(length);
  33.             break;
  34.         case 2:
  35.             BubbleSort(sortThis, length);
  36.             break;
  37.         case 3:
  38.             SelectionSort(length);
  39.             break;
  40.         default:
  41.             cout<<"Invalid choice. End program.";
  42.             break;
  43.     }
  44.     return 0;
  45. }
  46.  
  47. //function definitions
  48. void BubbleSort(vector <int> sortThis, int n)
  49. {
  50.     int temp;
  51.     for(int i=0;i<n-1;i++)
  52.     {
  53.         if(sortThis[i]>sortThis[i+1])
  54.         {
  55.             temp=sortThis[i];
  56.             sortThis[i]=sortThis[i+1];
  57.             sortThis[i+1]=temp;
  58.         }
  59.     }
  60. }
  61. void SelectionSort(vector <int> sortThis, int n)
  62. {
  63.     int temp;
  64.     for(int i=0;i<n-1;i++)
  65.     {
  66.         if(sortThis[i]<sortThis[i+1])
  67.         {
  68.             temp=sortThis[i];
  69.             sortThis[i]=sortThis[i+1];
  70.             sortThis[i+1]=temp;
  71.         }
  72.     }
  73. }
  74. int MinElement(vector <int> sortThis, int n)
  75. {
  76.     int min=sortThis[0];
  77.     for(int i=1;i<n;i++)
  78.     {
  79.         if(sortThis[i]<min)
  80.             min=sortThis[i];
  81.     }
  82.     return min;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement