PP Assignment 1 Report
PP Assignment 1 Report
a) Insertion sort –
b) Bubble sort –
i. Default algorithm
algorithm.
List-1: 1 16 12 26 25 35 33 58 45 42 56 67 83 75 74 86 81 88 99 95
List-2: 1 17 21 42 24 27 32 35 45 47 57 23 66 69 70 76 87 85 95 99
List-3: 22 20 81 38 95 84 99 12 79 44 26 87 96 10 48 80 1 31 16 92
Write the main () function to order these lists using the above-
stated subroutines.
#include <iostream>
#include <cstdlib>
#include <ctime>
void printCommand()
// Insertion Sort
int comparisonCount = 0;
int swapCount = 0;
int j = i - 1;
while (j >= 0)
comparisonCount++;
arr[j + 1] = arr[j];
j--;
swapCount++;
else
{
break;
arr[j + 1] = key;
int comparisonCount = 0;
int swapCount = 0;
comparisonCount++;
{
arr[j] = arr[j - 1];
swapCount++;
else
arr[j] = temp;
swapped = true;
break;
arr[0] = temp;
swapCount++;
// Bubble Sort
int comparisonCount = 0;
int swapCount = 0;
int bubbleIteration = 0;
int temp;
bubbleIteration++;
comparisonCount++;
temp = a[j];
a[j + 1] = temp;
swapCount++;
int comparisonCount = 0;
int swapCount = 0;
int iteration = 0;
iteration++;
comparisonCount++;
arr[j - 1] = arr[j];
swapCount++;
else
arr[j - 1] = max;
max = arr[j];
swapCount++;
arr[i] = max;
}
// Flagged Bubble Sort
int comparisonCount = 0;
int swapCount = 0;
int iterationCount = 0;
iterationCount++;
comparisonCount++;
arr[j - 1] = arr[j];
sorted = false;
swapCount++;
else
arr[j - 1] = max;
max = arr[j];
swapCount++;
arr[i] = max;
if (sorted)
break;
int comparisonCount = 0;
int swapCount = 0;
int lastSwap = 0;
{
comparisonCount++;
arr[j + 1] = temp;
swapCount++;
lastSwap = j;
i = lastSwap;
// Merge Sort
int mergeCompareCount = 0;
int mergeSwapCount = 0;
int B[2000];
int i, j, k;
i = low;
j = mid + 1;
k = low;
mergeCompareCount++;
B[k] = A[i];
i++;
else
B[k] = A[j];
j++;
k++;
B[k] = A[i];
k++;
i++;
B[k] = A[j];
k++;
j++;
A[i] = B[i];
// Quick Sort
int quickComparisonCount = 0;
int quickSwapCount = 0;
int medianOfThree(int *array, int low, int high)
quickComparisonCount++;
array[low] = array[mid];
array[mid] = temp;
quickSwapCount++;
quickComparisonCount++;
array[low] = array[high];
array[high] = temp;
quickSwapCount++;
quickComparisonCount++;
array[mid] = array[high];
array[high] = temp;
quickSwapCount++;
}
return array[mid];
int i = low - 1;
int j = high + 1;
while (true)
do
i++;
quickComparisonCount++;
do
j--;
quickComparisonCount++;
if (i >= j)
return j;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
quickSwapCount++;
int main()
int list1[] = {1, 16, 12, 26, 25, 35, 33, 58, 45, 42, 56, 67, 83, 75, 74, 86,
81, 88, 99, 95};
int list2[] = {1, 17, 21, 42, 24, 27, 32, 35, 45, 47, 57, 23, 66, 69, 70, 76,
87, 85, 95, 99};
int list3[] = {22, 20, 81, 38, 95, 84, 99, 12, 79, 44, 26, 87, 96, 10, 48, 80,
1, 31, 16, 92};
int n = 20;
int listNo;
int algo;
cout << "Enter the list you want to sort: " << endl
switch (listNo)
case 1:
printCommand();
switch (algo)
case 1:
insertionSort(list1, n);
printArray(list1, n);
break;
case 2:
insertionWithouSwap(list1, n);
printArray(list1, n);
break;
case 3:
bubblesort(list1, n);
printArray(list1, n);
break;
case 4:
bubbleSortAvoidingSwap(list1, n);
printArray(list1, n);
break;
case 5:
flaggedBubbleSort(list1, n);
printArray(list1, n);
break;
case 6:
rangeLimitingBubbleSort(list1, n);
printArray(list1, n);
break;
case 7:
mergeSort(list1, 0, n - 1);
printArray(list1, n);
break;
case 8:
quickSort(list1, 0, n - 1);
printArray(list1, n);
break;
default:
break;
break;
case 2:
printCommand();
switch (algo)
case 1:
insertionSort(list2, n);
printArray(list2, n);
break;
case 2:
insertionWithouSwap(list2, n);
printArray(list2, n);
break;
case 3:
bubblesort(list2, n);
printArray(list2, n);
break;
case 4:
bubbleSortAvoidingSwap(list2, n);
printArray(list2, n);
break;
case 5:
flaggedBubbleSort(list2, n);
printArray(list2, n);
break;
case 6:
rangeLimitingBubbleSort(list2, n);
printArray(list2, n);
break;
case 7:
mergeSort(list2, 0, n - 1);
printArray(list2, n);
break;
case 8:
quickSort(list2, 0, n - 1);
printArray(list2, n);
break;
default:
break;
break;
case 3:
printCommand();
switch (algo)
case 1:
insertionSort(list3, n);
printArray(list3, n);
break;
case 2:
insertionWithouSwap(list3, n);
printArray(list3, n);
break;
case 3:
bubblesort(list3, n);
printArray(list3, n);
break;
case 4:
bubbleSortAvoidingSwap(list3, n);
printArray(list3, n);
break;
case 5:
flaggedBubbleSort(list3, n);
printArray(list3, n);
break;
case 6:
rangeLimitingBubbleSort(list3, n);
printArray(list3, n);
break;
case 7:
mergeSort(list3, 0, n - 1);
printArray(list3, n);
break;
case 8:
quickSort(list3, 0, n - 1);
printArray(list3, n);
break;
default:
break;
default:
break;
cout << "\nThe program took " << time_taken << " seconds to execute."
<< endl;
return 0;
Analysis:
1. Insertion Sort
Time Complexity: O(n^2) in the worst case, O(n) in the best case.
Time Complexity: Just like the default insertion sort, O(n^2) in the
worst case and O(n) in the best case.
3. Bubble Sort
Efficiency: Bubble Sort is not efficient for large data of elements due
to its O(n^2)complexity.
Time Complexity: O(n^2) in the worst case, but if the array becomes
sorted earlier before completion of full iteration than it will terminate.
7. Merge Sort
Time Complexity: O(n log n) in both the worst and best cases.
Efficiency: Merge Sort is much more efficient than the algorithms with
O(n^2) complexity, mostly for large datasets.
8. Quick Sort
Conclusion:
Insertion and Bubble Sort are not most used algorithms mostly
for large data sets, these are generally used for smaller data sets
of elements.
Quick Sort Algorithm and Merge Sort Algorithm are used for large
data sets of elements. Quick Sort is highly efficient but in worst
case it can be costly. A better pivot selection is needed in Quick
Sort.
2. Create a list of 10K integer elements stored in an array. Then,
perform sorting
Please run at least five times using the same sorting technique and
take an
Finally, compare and analyze the time taken for the respective
sorting
techniques.
#include <iostream>
#include <cstdlib>
#include <ctime>
int comparisonCount = 0;
int swapCount = 0;
comparisonCount++;
swapCount++;
else
arr[j] = temp;
swapped = true;
break;
arr[0] = temp;
swapCount++;
int comparisonCount = 0;
int shiftCount = 0;
int lastSwap = 0;
comparisonCount++;
arr[j + 1] = temp;
shiftCount++;
lastSwap = j;
i = lastSwap;
}
// Merge Sort
int mergeCompareCount = 0;
int mergeSwapCount = 0;
int B[10001];
int i, j, k;
i = low;
j = mid + 1;
k = low;
mergeCompareCount++;
B[k] = A[i];
i++;
else
B[k] = A[j];
j++;
k++;
}
B[k] = A[i];
k++;
i++;
B[k] = A[j];
k++;
j++;
A[i] = B[i];
// Quick Sort
int quickComparisonCount = 0;
int quickSwapCount = 0;
quickComparisonCount++;
array[low] = array[mid];
array[mid] = temp;
quickSwapCount++;
quickComparisonCount++;
array[low] = array[high];
array[high] = temp;
quickSwapCount++;
quickComparisonCount++;
array[mid] = array[high];
array[high] = temp;
quickSwapCount++;
return array[mid];
int i = low - 1;
int j = high + 1;
while (true)
do
i++;
quickComparisonCount++;
} while (array[i] < pivot);
do
j--;
quickComparisonCount++;
if (i >= j)
return j;
array[i] = array[j];
array[j] = temp;
quickSwapCount++;
}
// Random Number Generator
srand(time(0));
int main()
int min = 1;
int count;
cout << "Enter the size of the array of random numbers: ";
int arr[count];
printArray(arr, count);
int num;
switch (num)
case 1:
insertionWithouSwap(arr, count);
printArray(arr, count);
break;
case 2:
rangeLimitingBubbleSort(arr, count);
printArray(arr, count);
break;
case 3:
mergeSort(arr, 0,count-1);
printArray(arr, count);
break;
case 4:
printArray(arr, count);
break;
default:
break;
cout << "\nThe program took " << time_taken << " seconds to execute."
<< endl;
return 0;
}
Analysis:
Insertion Sorting execution time for 10k random integers:
1. 7.644 seconds
2. 6.252 seconds
3. 9.975 seconds
4. 8.499 seconds
5. 6.542 seconds
Average: 7.7824 seconds
Bubble Sort execution time for 10k random integers:
1. 5.709 seconds
2. 6.026 seconds
3. 5.305 seconds
4. 5.257 seconds
5. 5.48 seconds
Average: 5.5554 seconds
Merge Sort execution time for 10k random integers:
1. 5.443 seconds
2. 5.286 seconds
3. 5.349 seconds
4. 5.611 seconds
5. 5.239 seconds
Average: 5.3856 seconds
Quick Sort execution time for 10k random integers:
1. 6.845 seconds
2. 5.22 seconds
3. 6.631 seconds
4. 5.57 seconds
5. 5.132 seconds
Average: 5.8796 seconds
Conclusion: Hence, Merge Sort, Quick Sort and Bubble Sort are
less time taken algorithms, Insertion Sort is not efficient for large
data sets like 10k elements.