Selection Sort - Detailed Version
itsLu
c_cpp
2 years ago
990 B
14
Indexable
#include <iostream>
using namespace std;
void Selection_Sort (int arr[], int arraySize)
{
int minIndex, temp, p, c, cCounter = 0, swapCounter = 0;
for (p = 0 ; p < arraySize - 1 ; p++)
{
minIndex = p;
for (c = p + 1 ; c < arraySize ; c++)
{
cCounter++;
if (arr[c] < arr[minIndex])
{
minIndex = c;
}
}
if (minIndex != p)
{
temp = arr[p];
arr[p] = arr[minIndex];
arr[minIndex] = temp;
swapCounter++;
}
}
cout << endl << "Number of Comparisons: " << cCounter << "\tNumber of Swaps: " << swapCounter << endl;
}
int main()
{
int arr[5] = {5, 4, 3, 2 , 1};
Selection_Sort(arr, 5);
//اللوب الجاية دي عشان تطبع الأراي بعد الترتيب ممكن تكرفله عادي
for (int k = 0 ; k < 5 ; k++)
cout << arr[k] << "\t";
}Editor is loading...
Leave a Comment