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