You are on page 1of 9

Student ID: 20CE120 Subject Code: CE355

Practical 2.1 (Bubble Sort)

Practical Complexity
Code:

#include <iostream>
using namespace std;

int comparisons = 0;

int* bubbleSort(int arr[], int n)


{
int exchange = 0;
for (int i = 0; i < n - 1; i++) // Will execute n times
{
for (int j = 0; j < n - i - 1; j++) // will execute n-i times
{
comparisons++;
if (arr[j] > arr[j + 1])
{
int temp = arr[j]; // will execute n-i-1 times
arr[j] = arr[j + 1]; // will execute n-i-1 times
arr[j + 1] = temp; // will execute n-i-1 times
exchange = 1; // will execute n-i-1 times
}
}
if (exchange == 0)
{
break;
}
}
return arr;
}

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;

Chandubhai S. Patel Institute of Technology 1


Student ID: 20CE120 Subject Code: CE355

for (int i = 0; i < n; i++)


{
cout << sortedArr[i] << " ";
}
cout << endl << "Number of Comparisons: " << comparisons << endl;
return 0;
}

Output:
Best Case:

Worst case:

Analysis (2.1 Bubble Sort)


n Count (Best Case) Count (Average Case) Count (Worst Case)
2 1 1 1
3 2 2 3
4 3 4 6
5 4 7 10
6 5 10 15
7 6 13 21
8 7 19 28
9 8 26 36
10 9 38 45

Chandubhai S. Patel Institute of Technology 2


Student ID: 20CE120 Subject Code: CE355

Bubble Sort (Best Case)


10
9 y=x
8
7
6
5
4
3
2
1
0
2 3 4 5 6 7 8 9 10

Bubble Sort (Average Case)


40
35 y = 0.6104x2 - 1.8372x + 3.1905

30
25
20
15
10
5
0
1 2 3 4 5 6 7 8 9

Bubble Sort (Worst Case)


60
y = 0.5x2 + 0.5x - 1E-13
50

40

30

20

10

0
1 2 3 4 5 6 7 8 9 10

Count (Worst Case) Poly. (Count (Worst Case))

Chandubhai S. Patel Institute of Technology 3


Student ID: 20CE120 Subject Code: CE355

Practical 2.2 (Selection Sort)

Practical Complexity
Code:

#include <iostream>
using namespace std;

int comparisons = 0;

int* selectionSort(int arr[], int n)


{
int minIndex;
for (int i = 0; i < n - 1; i++) // will execute n times
{
minIndex = i;
for (int j = i + 1; j < n; j++) // will execute n-i times
{
comparisons++; // will execute n-i times
if (arr[j] < arr[minIndex]) // will execute n-i times
{
minIndex = j;
}
}
if (minIndex != i) // will execute n-1 times
{
int temp = arr[i]; // will execute n-1 times
arr[i] = arr[minIndex]; // will execute n-1 times
arr[minIndex] = temp; // will execute n-1 times
}
}
return arr;
}

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;

Chandubhai S. Patel Institute of Technology 4


Student ID: 20CE120 Subject Code: CE355

for (int i = 0; i < n; i++)


{
cout << sortedArr[i] << " ";
}
cout << endl << "Number of Comparisons: " << comparisons << endl;
return 0;
}

Output:
Best Case:

Worst case:

Analysis (2.2 Selection Sort)

n Count (Best Case) Count (Average Case) Count (Worst Case)


2 1 1 1
3 3 3 3
4 6 6 6
5 10 10 10
6 15 15 15
7 21 21 21
8 28 28 28
9 36 36 36
10 45 45 45

Chandubhai S. Patel Institute of Technology 5


Student ID: 20CE120 Subject Code: CE355

Selection Sort (Best Case)


50
45 y = 0.5x2 + 0.5x - 1E-13
40
35
30
25
20
15
10
5
0
1 2 3 4 5 6 7 8 9

Count (Best Case) Poly. (Count (Best Case))

Selection Sort (Average Case)


50
45 y = 0.5x2 + 0.5x - 1E-13
40
35
30
25
20
15
10
5
0
1 2 3 4 5 6 7 8 9

Selection Sort (Worst Case)


50
45 y = 0.5x2 + 0.5x - 1E-13
40
35
30
25
20
15
10
5
0
1 2 3 4 5 6 7 8 9

Chandubhai S. Patel Institute of Technology 6


Student ID: 20CE120 Subject Code: CE355

Practical 2.3 (Insertion Sort)

Practical Complexity
Code:

#include <iostream>
using namespace std;

int comparisons = 0;

int* insertionSort(int arr[], int n)


{
int i, j, temp;
for (i = 1; i < n; i++)
{
temp = arr[i];
for (j = i - 1; j >= 0 && arr[j] > temp; j--)
{
arr[j + 1] = arr[j];
comparisons++;
}
arr[j + 1] = temp;
comparisons++;
}
return arr;
}

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;
}

Chandubhai S. Patel Institute of Technology 7


Student ID: 20CE120 Subject Code: CE355

Output:
Best Case:

Worst case:

Analysis (2.2 Selection Sort)

n Count (Best Case) Count (Average Case) Count (Worst Case)


2 1 1 2
3 2 3 5
4 3 7 9
5 4 11 14
6 5 16 20
7 6 23 27
8 7 29 35
9 8 37 44
10 9 48 54

Insertion Sort (Best Case)


10
9 y=x
8
7
6
5
4
3
2
1
0
2 3 4 5 6 7 8 9 10

Chandubhai S. Patel Institute of Technology 8


Student ID: 20CE120 Subject Code: CE355

Insertion Sort (Average Case)


70

60
y = 0.5043x2 + 0.7234x - 0.1429
50

40

30

20

10

0
1 2 3 4 5 6 7 8 9 10

Insertion Sort (Worst Case)


60
y = 0.5x2 + 1.5x
50

40

30

20

10

0
1 2 3 4 5 6 7 8 9

Chandubhai S. Patel Institute of Technology 9

You might also like