Advertisement
Guest User

for you

a guest
Mar 28th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. #include <cstdio>
  7. #include <vector>
  8. #include <ctime>
  9. using namespace std;
  10.  
  11. template <class Type>
  12. void my_sort( vector <Type> & mass){
  13. int a;
  14. for(int i=1;i<mass.size();i++){
  15. for(int j=i-1;j>=0;j--){
  16. if(mass[i]<mass[j]){
  17. a = mass[i];
  18. mass[i]=mass[j];
  19. mass[j]=a;
  20. i--;
  21. }
  22. }
  23. }
  24. }
  25.  
  26. template <class Type>
  27. void cout_mass(vector <Type> & mass)
  28. {
  29. cout<<"[";
  30. if(mass.size()>20){
  31. for(int i =0;i<20;i++){
  32. cout<<mass[i]<<" ";
  33. }
  34. cout<<" ...]"<<endl;
  35. }
  36. else{
  37. for(int i =0;i<mass.size();i++){
  38. cout<<mass[i]<<" ";
  39. }
  40. cout<<"]"<<endl<<endl;
  41. }
  42. }
  43.  
  44. int main(){
  45. setlocale(LC_ALL,"Russian");
  46. vector <int>arr;
  47. vector <int>avto;
  48. cout<<"Выберите, как будет заполняться вектор:"<<endl<<"1 - Вручную "<<endl
  49. <<"2 - автоматически "<<endl<<"3 - автоматически с малым диапазоном значений"<<endl<<"Ваш выбор: ";
  50. char r;
  51. cin>>r;
  52. srand(time(0));
  53. switch(r)
  54. {
  55. case '1' :{
  56. cout<<"Выберите количество элементов массива --> n = ";
  57. int n;
  58. cin>>n;
  59. for(int i=0;i<n;i++)
  60. {
  61. int m;
  62. //cout<<"mass["<<i<<"] = ";
  63. cin >>m;
  64. arr.push_back(m);
  65. avto.push_back(m);
  66. }
  67.  
  68. break;
  69. }
  70. case '2' :{
  71. cout<<"Выберите количество элементов массива --> n = ";
  72. int n;
  73. cin>>n;
  74. for(int i=0;i<n;i++)
  75. {
  76. int m = rand()%n;
  77. arr.push_back(m);
  78. avto.push_back(m);
  79. }
  80. break;
  81. }
  82. case '3' :{
  83. cout<<"Выберите количество элементов массива --> n = ";
  84. int n;
  85. cin>>n;
  86. for(int i=0;i<n;i++)
  87. {
  88. int m = rand()%20;
  89. arr.push_back(m);
  90. avto.push_back(m);
  91. }
  92. break;
  93. }
  94. default : {
  95. cout<<"error"<<endl;
  96. exit(0);}
  97. }
  98. cout_mass(arr);
  99. cout_mass(avto);
  100. cout<<endl<<endl;
  101. clock_t time1,time2;
  102. time1 = clock();
  103. my_sort(arr);
  104. time2 = clock();
  105. cout_mass(arr);
  106. cout<<"time = "<<time2 - time1<<endl<<endl;
  107. time1 = clock();
  108. sort(avto.begin(),avto.end());
  109. cout_mass(avto);
  110. cout<<"time2 = "<<time2 - time1<<endl;
  111. system("pause");
  112. return 0;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement