Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. //#include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. void InsertionSort(int** arr, const int str, const int stlb){
  8. int temp, item, counter=0;
  9. for (int i=0; i<str; i++){
  10. for (int j=1; j<stlb; j++){
  11. temp=arr[i][j];
  12. item=j-1;
  13. while((item>=0)&&(arr[i][item]>temp))
  14. {
  15. counter++;
  16. arr[i][item+1] = arr[i][item];
  17. arr[i][item] = temp;
  18. item--;
  19. }
  20.  
  21. }
  22. } cout<< endl <<"Затрачено шагов: " <<counter<<endl;
  23. }
  24.  
  25. void BubbleSort(int** arr, const int str, const int stlb){
  26. int counter=0;
  27. for (int z=0; z<stlb; z++){
  28. for (int i=0; i<str; i++){
  29. for (int j=0; j<stlb-1; j++){
  30. if (arr[i][j]>arr[i][j+1]){
  31. counter++;
  32. swap(arr[i][j+1],arr[i][j]);
  33. }
  34. }
  35. }
  36. } cout<< endl <<"Затрачено шагов: " <<counter<<endl;
  37. }
  38.  
  39. /*void BubbleSort(char** arr, const int str, const int stlb){
  40. int counter=0;
  41.  
  42. }*/
  43.  
  44.  
  45. int main(){
  46. setlocale(LC_ALL,"rus");
  47. //srand(time(0));
  48. int N, M;
  49. clock_t start;
  50. double duration;
  51. cout << "Ввод количества строк: "; cin >> M;
  52. cout << "Ввод количества столбцов: "; cin >> N;
  53. int** matrix;
  54. matrix = new int*[M];
  55. for (int i=0; i<M; i++)
  56. matrix[i]=new int[N];
  57.  
  58. for (int i=0; i<M; i++){
  59. for (int j=0; j<N; j++){
  60. matrix[i][j]=rand()%100;
  61. cout << matrix[i][j]<<" ";
  62. //cout << "Введите элемент матрицы: ";
  63. //cin >> matrix[i][j];
  64. } cout << endl;
  65. }
  66.  
  67. /*cout << "Изначальная матрица: " << endl;
  68. for (int i=0; i<M; i++){
  69. for (int j=0; j<N; j++){
  70. cout <<matrix[i][j]<< " ";
  71. } cout << endl;
  72. }*/
  73.  
  74. cout << "Выберите сортировку: 1. Вставками 2. Пузырёк";
  75. int key; cin >> key;
  76. switch (key){
  77. case 1:
  78. start = clock();
  79. InsertionSort(matrix,M,N);
  80. duration = (double)(clock()-start)/CLOCKS_PER_SEC;
  81.  
  82. cout << "Отсортированная матрица: " << endl;
  83. for (int i=0; i<M; i++) {
  84. for (int j=0; j<N; j++) {
  85. cout << matrix[i][j] << " ";
  86. } cout << endl;
  87. }
  88. cout << duration<<endl;
  89. break;
  90. case 2:
  91. start = clock();
  92. BubbleSort(matrix,M,N);
  93. duration = (double)(clock()-start)/CLOCKS_PER_SEC;
  94.  
  95. cout << "Отсортированная матрица: " << endl;
  96. for (int i=0; i<M; i++) {
  97. for (int j=0; j<N; j++) {
  98. cout << matrix[i][j] << " ";
  99. } cout << endl;
  100. }
  101. cout << duration<<endl;
  102. break;
  103.  
  104. }
  105. return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement