2210061 Lab 8
2210061 Lab 8
Submitted By : Submitted To :
Series : 22 RUET
Code:
1. #include <iostream>
2. using namespace std;
3. void insertionsort(int arr[], int n)
4. {
5. for (int i = 1; i < n; i++)
6. {
7. int key = arr[i];
8. int j = i - 1;
9. while (j >= 0 && arr[j] > key)
10. {
11. arr[j + 1] = arr[j];
12. j--;
13. }
14. arr[j + 1] = key;
15. }
16. }
17. void print(int arr[], int n)
18. {
19. for (int i = 0; i < n; i++)
20. {
21. cout << arr[i] << " ";
22. }
23. cout << endl;
24. }
25. int main()
26. {
27. int arr[] = {42, 11, 13,65, 1, 9,90};
28. int n = sizeof(arr) / sizeof(arr[0]);
29. cout << "Original array : ";
30. print(arr, n);
31. insertionsort(arr, n);
32. cout << "Sorted array : ";
33. print(arr, n);
34. }
Output:
Original array : 42 11 13 65 1 9 90
Sorted array : 1 9 11 13 42 65 90
Algorithm:
Step 1 − If it is the first element, it is already sorted. return 1;
Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that is greater than the
value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted
Discussion:
Insertion sort is particularly efficient for small datasets or nearly sorted arrays,
where its performance approaches linear time. So, some preparation should be
made if one wanted to work with unsorted array.
The smallest element is selected from the unsorted array and swapped with
the leftmost element, and that element becomes a part of the sorted array.
This process continues moving unsorted array boundaries by one element to
the right.
Code:
1. #include <iostream>
2. using namespace std;
3. void selectionSortAlgo(int arr[], int n) {
4. for (int i = 0; i < n - 1; i++) {
5. int minIndex = i;
6. for (int j = i + 1; j < n; j++) {
7. if (arr[j] < arr[minIndex])
8. minIndex = j;
9. }
10. swap(arr[i], arr[minIndex]);
11. }
12. }
13. void printArray(int arr[], int n) {
14. for (int i = 0; i < n; i++)
15. cout << arr[i] << " ";
16. cout << endl;
17. }
18. int main() {
19. int arr[] = {64, 25, 12, 22, 11};
20. int n = sizeof(arr) / sizeof(arr[0]);
21. cout << "Original array: ";
22. printArray(arr, n);
23. selectionSortAlgo(arr, n);
24. cout << "Sorted array : ";
25. printArray(arr, n);
26. return 0;
27. }
Output:
Original array: 64 25 12 22 11
Sorted array : 11 12 22 25 64
Algorithm:
1. Set MIN to location 0.
2. Search the minimum element in the list.
3. Swap with value at location MIN.
4. Increment MIN to point to next element.
5. Repeat until the list is sorted.
Discussion:
In selection sort, equal elements may not maintain their original relative order
unless additional effort is made to preserve it. It doesn’t take advantage of
existing order in the array.
Code:
1. #include <iostream>
2. using namespace std;
3. int quickSortPartition(int arr[], int low, int high) {
4. int pivot = arr[high];
5. int i = low - 1;
6. for (int j = low; j < high; j++) {
7. if (arr[j] < pivot) {
8. i++;
9. swap(arr[i], arr[j]);
10. }
11. }
12. swap(arr[i + 1], arr[high]);
13. return i + 1;
14. }
15. void quickSortAlgo(int arr[], int low, int high) {
16. if (low < high) {
17. int pi = quickSortPartition(arr, low, high);
18. quickSortAlgo(arr, low, pi - 1);
19. quickSortAlgo(arr, pi + 1, high);
20. }
21. }
22. void printArray(int arr[], int n) {
23. for (int i = 0; i < n; i++)
24. cout << arr[i] << " ";
25. cout << endl;
26. }
27. int main() {
28. int arr[] = {10, 7, 8, 9, 1, 5};
29. int n = sizeof(arr) / sizeof(arr[0]);
30. cout << "Original array: ";
31. printArray(arr, n);
32. quickSortAlgo(arr, 0, n - 1);
33. cout << "Sorted array : ";
34. printArray(arr, n);
35. return 0;
36. }
Output:
Original array: 10 7 8 9 1 5
Sorted array : 1 5 7 8 9 10
Algorithm:
1. Make the right-most index value pivot
2. Partition the array using pivot value
3. Quicksort left partition recursively
4. Quicksort right partition recursively
Discussion:
Quick Sort is extremely efficient on average and widely used, but it needs
proper pivot selection and optimization to avoid worst-case scenarios. By using
techniques like random pivoting or median-of-three, and combining with
insertion sort for small arrays, Quick Sort becomes robust and efficient.
Code:
1. #include <iostream>
2. using namespace std;
3. void mergeAlgo(int arr[], int left, int mid, int right) {
4. int n1 = mid - left + 1;
5. int n2 = right - mid;
6. int L[n1], R[n2];
7. for (int i = 0; i < n1; i++)
8. L[i] = arr[left + i];
9. for (int j = 0; j < n2; j++)
10. R[j] = arr[mid + 1 + j];
11. int i = 0, j = 0, k = left;
12. while (i < n1 && j < n2) {
13. if (L[i] <= R[j])
14. arr[k++] = L[i++];
15. else
16. arr[k++] = R[j++];
17. }
18. while (i < n1)
19. arr[k++] = L[i++];
20. while (j < n2)
21. arr[k++] = R[j++];
22. }
23. void mergeSortAlgo(int arr[], int left, int right) {
24. if (left < right) {
25. int mid = left + (right - left) / 2;
26. mergeSortAlgo(arr, left, mid);
27. mergeSortAlgo(arr, mid + 1, right);
28. mergeAlgo(arr, left, mid, right);
29. }
30. }
31. void printArray(int arr[], int n) {
32. for (int i = 0; i < n; i++)
33. cout << arr[i] << " ";
34. cout << endl;
35. }
36. int main() {
37. int arr[] = {38, 27, 43, 3, 9, 82, 10};
38. int n = sizeof(arr) / sizeof(arr[0]);
39. cout << "Original array: ";
40. printArray(arr, n);
41. mergeSortAlgo(arr, 0, n - 1);
42. cout << "Sorted array : ";
43. printArray(arr, n);
44. return 0;
45. }
Output:
Original array: 38 27 43 3 9 82 10
Sorted array : 3 9 10 27 38 43 82
Algorithm:
Step 1: If it is only one element in the list, consider it already sorted, so return.
Step 2: Divide the list recursively into two halves until it can no more be
divided.
Step 3: Merge the smaller lists into new list in sorted order.
Discussion:
Merge Sort is a stable, reliable, and efficient sorting algorithm for large
datasets, especially when stability is required. However, its extra space usage
and non-adaptiveness make it less ideal for small or memory-sensitive
applications.