Professional Documents
Culture Documents
Practical Complexity
Code:
#include <iostream>
using namespace std;
int comparisons = 0;
int main()
{
int n;
cout << "Enter the number of elements in Array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of Array: " << endl;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int* sortedArr = bubbleSort(arr, n);
cout << "Sorted Array: " << endl;
Output:
Best Case:
Worst case:
30
25
20
15
10
5
0
1 2 3 4 5 6 7 8 9
40
30
20
10
0
1 2 3 4 5 6 7 8 9 10
Practical Complexity
Code:
#include <iostream>
using namespace std;
int comparisons = 0;
int main()
{
int n;
cout << "Enter the number of elements in Array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of Array: " << endl;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int* sortedArr = selectionSort(arr, n);
cout << "Sorted Array: " << endl;
Output:
Best Case:
Worst case:
Practical Complexity
Code:
#include <iostream>
using namespace std;
int comparisons = 0;
int main()
{
int n;
cout << "Enter the number of elements in Array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of Array: " << endl;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int* sortedArr = insertionSort(arr, n);
cout << "Sorted Array: " << endl;
for (int i = 0; i < n; i++)
{
cout << sortedArr[i] << " ";
}
cout << endl << "Number of Comparisons: " << comparisons << endl;
return 0;
}
Output:
Best Case:
Worst case:
60
y = 0.5043x2 + 0.7234x - 0.1429
50
40
30
20
10
0
1 2 3 4 5 6 7 8 9 10
40
30
20
10
0
1 2 3 4 5 6 7 8 9