Advertisement
Mancolo

Сортировка выбором

Oct 15th, 2022
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int findSmallestPosition(int list[], int startPosition, int listLength)
  5. {
  6.     int smallestPosition = startPosition;
  7.    
  8.     for(int i = startPosition; i < listLength; i++)
  9.     {
  10.         if(list[i] < list[smallestPosition])
  11.             smallestPosition = i;
  12.     }
  13.     return smallestPosition;
  14. }
  15.  
  16. void selectionSort(int list[], int listLength)
  17. {
  18.     for(int i = 0; i < listLength; i++)
  19.     {
  20.         int smallestPosition = findSmallestPosition(list, i, listLength);
  21.         swap(list[i], list[smallestPosition]);
  22.     }
  23.     return;
  24. }
  25.  
  26. int main ()
  27. {
  28.     int list[5] = {12, 5, 48, 0, 4};
  29.    
  30.     cout << "Input array ..." << endl;
  31.     for(int i = 0; i < 5; i++)
  32.         cout << list[i] << '\t';
  33.     cout << endl;
  34.    
  35.     selectionSort(list, 5);
  36.    
  37.     cout << "Sorted array ..." << endl;
  38.     for(int i = 0; i < 5; i++)
  39.         cout << list[i] << '\t';
  40.     cout << endl;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement