Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. using namespace std;
  5.  
  6. void OutPut(int *arr, int lenght) {
  7. cout << "\n";
  8. for (int i = 0; i < lenght; ++i) {
  9. cout << arr[i] << " ";
  10. }
  11. }
  12.  
  13. int *Sorting(int *arr, int lenght) {
  14. // Сортировка массива
  15. int complexity = 0;
  16. int transposition = 0;
  17. for (int start_index = 0; start_index < lenght - 1; ++start_index) {
  18. // Найменьший элемент - Первый (по условию)
  19. int smallest_index = start_index;
  20. // Ищем элемент меньше в остальной части массива
  21. for (int current_index = start_index; current_index < lenght; ++current_index) {
  22. // Если текущий элеммент меньше найменьшего
  23. if (arr[current_index] < arr[smallest_index]) {
  24. // Запоминаем его
  25. smallest_index = current_index;
  26. complexity += 1;
  27. }
  28. transposition += 1;
  29. }
  30. std::swap(arr[start_index], arr[smallest_index]);
  31. }
  32. cout << "Transposition: " << transposition << " Comparison:" << complexity;
  33. return arr;
  34. }
  35.  
  36. int main() {
  37. srand(time(0));
  38. int lenght = 5;
  39. int arr[lenght];
  40. int arr_copy[lenght];
  41. int increase_arr[] = {1, 2, 5, 7, 10};
  42. int decrease_arr[] = {10, 9, 5, 3, 0};
  43.  
  44. // Заполнение массива
  45. for (int i = 0; i < lenght - 1; ++i) {
  46. arr[i] = rand() % 10;
  47. }
  48. // Copy
  49. for (int i = 0; i < lenght - 1; ++i) {
  50. arr_copy[i] = arr[i];
  51. }
  52.  
  53. // Вывод массива
  54. OutPut(arr, lenght);
  55.  
  56. // Сортировка
  57. //int *complexity = new int (1);
  58. cout << "\n" << "Random: \n";
  59. int *sort_array = Sorting(arr, lenght);
  60. cout << "\n" << "Increase: \n";
  61. int *inc_arr = Sorting(increase_arr, 5);
  62. cout << "\n" << "Decrease: \n";
  63. int *dec_arr = Sorting(decrease_arr, 5);
  64. // Вывод массива
  65. OutPut(arr_copy, lenght);
  66. OutPut(sort_array, lenght);
  67. OutPut(inc_arr, lenght);
  68. OutPut(dec_arr, lenght);
  69.  
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement