0% found this document useful (0 votes)
10 views8 pages

2210061 Lab 8

The document is a lab report from Rajshahi University of Engineering & Technology detailing the implementation of four sorting algorithms: insertion sort, selection sort, quick sort, and merge sort, each accompanied by theory, code, output, and discussion. Each algorithm is explained with its respective C/C++ code, demonstrating how they sort arrays of integers. The report emphasizes the efficiency and use cases of each sorting method, highlighting their strengths and weaknesses.

Uploaded by

marmasunmoon9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

2210061 Lab 8

The document is a lab report from Rajshahi University of Engineering & Technology detailing the implementation of four sorting algorithms: insertion sort, selection sort, quick sort, and merge sort, each accompanied by theory, code, output, and discussion. Each algorithm is explained with its respective C/C++ code, demonstrating how they sort arrays of integers. The report emphasizes the efficiency and use cases of each sorting method, highlighting their strengths and weaknesses.

Uploaded by

marmasunmoon9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Heaven’s Light is Our Guide

Rajshahi University of Engineering &


Technology
Department of Electrical & Computer Engineering
LAB REPORT

❖ Course title(Sessional) : Data Structure and Algorithms


❖ Course Code : ECE-2104
❖ Date of submission : 13/04/2025

Submitted By : Submitted To :

Name : Sanumong Marma Md. Omaer Faruq Goni

Roll : 2210061 Assistant Professor

Reg no. : 1115 Department of ECE,

Series : 22 RUET

Department of ECE, RUET


Problem 01 : Write a C/C++ code to implement insertion sort.

Theory: insertion sort is a very simple method to sort numbers in an


ascending or descending order. This method follows the incremental method.
This is an in-place comparison-based sorting algorithm. Here, a sub-list is
maintained which is always sorted.

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.

Problem 02 : Write a C/C++ code to implement selection sort.


Theory: Selection sort is a simple sorting algorithm. This sorting algorithm,
like insertion sort, is an in-place comparison-based algorithm in which the list is
divided into two parts, the sorted part at the left end and the unsorted part at
the right end. Initially, the sorted part is empty and the unsorted part is the
entire list.

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.

Problem 03 : Write a C/C++ code to implement quick sort.

Theory: Quick sort is a highly efficient sorting algorithm and is based on


partitioning of array of data into smaller arrays. A large array is partitioned into
two arrays one of which holds values smaller than the specified value, say
pivot, based on which the partition is made and another array holds values
greater than the pivot value.

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.

Problem 04 : Write a C/C++ code to implement merge sort.


Theory: Merge sort is a sorting technique based on divide and conquer
technique. With worst-case time complexity being (n log n), it is one of the
most used and approached algorithms. Merge sort first divides the array into
equal halves and then combines them in a sorted manner.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy