Answer the question
In order to leave comments, you need to log in
How to count the number of comparisons and transfers in a direct selection sort?
void select_sort(int array[SIZE], int &comparison2, int &transfer2);
{
int min, temp;
cout << "\nсортировка методом прямого выбора" << endl;
for (int i = 0; i < SIZE-1; i++)
{
min = i; // индекс минимального элемента
for (int j = i+1; j < SIZE; j++)
{
comparison2++; // инкремент сравнений
if (array[j] < array[min]) // если текущий элемент меньше минимального
{
min = j; // запоминаю его индекс
}
temp = array[i]; //
array[i] = array[min]; // меняю их местами
array[min] = temp; //
transfer2++; // инкремент пересылок
}
}
}
#include <iostream>
#include <cstdlib>
#include <locale>
#include <conio.h>
#define SIZE 10
using namespace std;
int main()
{
setlocale(LC_ALL, "rus");
int array[SIZE];
int transfer = 0;
int comparison = 0;
int min, temp;
cout << "\nсортировка методом прямого выбора" << endl;
for (int i = 0; i < SIZE-1; i++)
{
min = i; // индекс минимального элемента
for (int j = i+1; j < SIZE; j++)
{
comparison++; // инкремент сравнений
if (array[j] < array[min]) // если текущий элемент меньше минимального
{
min = j; // запоминаю его индекс
}
temp = array[i]; //
array[i] = array[min]; // меняю их местами
array[min] = temp; //
transfer++; // инкремент пересылок
}
}
cout << "сравнений " << comparison << endl;
cout << "пересылок " << transfer << endl;
getch();
return 0;
}
Answer the question
In order to leave comments, you need to log in
For the umpteenth time I see your identical posts, like there were answers, but it does not seem to help.
1. You have an implementation error. Need like this:
for (int i = 0; i < SIZE-1; i++)
{
min = i; // индекс минимального элемента
for (int j = i+1; j < SIZE; j++)
{
comparison++; // инкремент сравнений
if (array[j] < array[min]) // если текущий элемент меньше минимального
{
min = j; // запоминаю его индекс
}
}
temp = array[i]; //
array[i] = array[min]; // меняю их местами
array[min] = temp; //
transfer++; // инкремент пересылок
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question